x-star-design 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ export interface ImgShowProps {
2
+ /**
3
+ * @description 缩放比例
4
+ */
5
+ scale?: number;
6
+ offsetX?: number;
7
+ offsetY?: number;
8
+ }
9
+ export interface titleShowProps {
10
+ offsetX?: number;
11
+ offsetY?: number;
12
+ fontSize?: number;
13
+ fontFamily?: string;
14
+ shadowColor?: string;
15
+ color?: string;
16
+ }
17
+ export interface AcAnimationProps {
18
+ /**
19
+ * @description 文本内容
20
+ */
21
+ title?: string;
22
+ /**
23
+ * @description 图片地址
24
+ */
25
+ imgUrl?: string;
26
+ /**
27
+ * @default scale: 1, offsetX: 0, offsetY: 0 相对canvas水平垂直居中
28
+ */
29
+ imgSizeAndPosition?: ImgShowProps;
30
+ /**
31
+ * @default offsetX: 0, offsetY: 0, fontSize: 36, fontFamily: 'Arial', shadowColor: 'rgba(2,38,121,0.2)', color: '#022679' 相对canvas水平垂直居中
32
+ */
33
+ titleStyle?: titleShowProps;
34
+ /**
35
+ * @description canvas卸载时的回调
36
+ */
37
+ onFinish?: () => void;
38
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ import { AcAnimationProps } from './define';
3
+ declare const AcAnimation: React.FC<AcAnimationProps>;
4
+ export default AcAnimation;
@@ -0,0 +1,184 @@
1
+ import React, { useEffect } from 'react';
2
+ var AcAnimation = function AcAnimation(_ref) {
3
+ var title = _ref.title,
4
+ imgUrl = _ref.imgUrl,
5
+ _ref$imgSizeAndPositi = _ref.imgSizeAndPosition,
6
+ imgSizeAndPosition = _ref$imgSizeAndPositi === void 0 ? {
7
+ scale: 1,
8
+ offsetX: 0,
9
+ offsetY: 0
10
+ } : _ref$imgSizeAndPositi,
11
+ titleStyle = _ref.titleStyle,
12
+ onFinish = _ref.onFinish;
13
+ var canvas = document.createElement('canvas');
14
+ var ctx = canvas.getContext('2d');
15
+ var AnimationID = -1;
16
+ var confetti = [];
17
+ var confettiCount = 300;
18
+ var gravity = 0.5;
19
+ var terminalVelocity = 5;
20
+ var drag = 0.075;
21
+ var colors = [{
22
+ front: 'red',
23
+ back: 'darkred'
24
+ }, {
25
+ front: 'green',
26
+ back: 'darkgreen'
27
+ }, {
28
+ front: 'blue',
29
+ back: 'darkblue'
30
+ }, {
31
+ front: 'yellow',
32
+ back: 'darkyellow'
33
+ }, {
34
+ front: 'orange',
35
+ back: 'darkorange'
36
+ }, {
37
+ front: 'pink',
38
+ back: 'darkpink'
39
+ }, {
40
+ front: 'purple',
41
+ back: 'darkpurple'
42
+ }, {
43
+ front: 'turquoise',
44
+ back: 'darkturquoise'
45
+ }];
46
+ var img = new Image();
47
+ img.src = imgUrl || '';
48
+ useEffect(function () {
49
+ if (canvas) {
50
+ canvas.style.zIndex = '9999';
51
+ canvas.style.width = '60vw';
52
+ canvas.style.height = '60vh';
53
+ canvas.style.position = 'absolute';
54
+ canvas.style.top = '50%';
55
+ canvas.style.left = '50%';
56
+ canvas.style.transform = 'translate(-50%, -50%)';
57
+ canvas.width = window.innerWidth;
58
+ canvas.height = window.innerHeight;
59
+ }
60
+ }, [canvas]);
61
+ var resizeCanvas = function resizeCanvas() {
62
+ canvas.width = window.innerWidth;
63
+ canvas.height = window.innerHeight;
64
+ };
65
+ var renderImg = function renderImg(imgSizeAndPosition) {
66
+ ctx.save();
67
+ ctx.translate(canvas.width / 2, canvas.height / 2);
68
+ ctx.drawImage(img, imgSizeAndPosition.offsetX || 0 - img.width * (imgSizeAndPosition.scale || 1) / 2, imgSizeAndPosition.offsetY || 0 - img.height * (imgSizeAndPosition.scale || 1) / 2, img.width * (imgSizeAndPosition.scale || 1), img.height * (imgSizeAndPosition.scale || 1));
69
+ ctx.restore();
70
+ };
71
+ var renderText = function renderText(titleStyle) {
72
+ ctx.save();
73
+ ctx.shadowOffsetX = 4;
74
+ ctx.shadowOffsetY = 2;
75
+ ctx.shadowBlur = 4;
76
+ ctx.textAlign = 'center';
77
+ ctx.textBaseline = 'middle';
78
+ ctx.font = "bold \n\t\t".concat((titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.fontSize) || 36, "px \n\t\t").concat((titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.fontFamily) || 'Arial');
79
+ ctx.fillStyle = (titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.color) || '#022679';
80
+ ctx.shadowColor = (titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.fontFamily) || 'rgba(2,38,121,0.2)';
81
+ ctx.fillText(title || '', canvas.width / 2 + ((titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.offsetX) || 0), canvas.height / 2 + ((titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.offsetY) || 0));
82
+ ctx.restore();
83
+ };
84
+ var randomRange = function randomRange(min, max) {
85
+ return Math.random() * (max - min) + min;
86
+ };
87
+ var initConfetti = function initConfetti() {
88
+ for (var i = 0; i < confettiCount; i++) {
89
+ confetti.push({
90
+ color: colors[Math.floor(randomRange(0, colors.length))],
91
+ dimensions: {
92
+ x: randomRange(10, 20),
93
+ y: randomRange(10, 30)
94
+ },
95
+ position: {
96
+ x: randomRange(0, canvas.width),
97
+ y: canvas.height - 1
98
+ },
99
+ rotation: randomRange(0, 2 * Math.PI),
100
+ scale: {
101
+ x: 1,
102
+ y: 1
103
+ },
104
+ velocity: {
105
+ x: randomRange(-25, 25),
106
+ y: randomRange(0, -50)
107
+ }
108
+ });
109
+ }
110
+ };
111
+ var shutDownCanvas = function shutDownCanvas() {
112
+ canvas.remove();
113
+ window.cancelAnimationFrame(AnimationID);
114
+ onFinish === null || onFinish === void 0 ? void 0 : onFinish();
115
+ };
116
+
117
+ //---------Render-----------
118
+ var render = function render() {
119
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
120
+ if (title) {
121
+ renderText(titleStyle);
122
+ }
123
+ if (imgUrl) {
124
+ renderImg(imgSizeAndPosition);
125
+ }
126
+ confetti.forEach(function (confetto, index) {
127
+ var width = confetto.dimensions.x * confetto.scale.x;
128
+ var height = confetto.dimensions.y * confetto.scale.y;
129
+
130
+ // Move canvas to position and rotate
131
+ ctx.translate(confetto.position.x, confetto.position.y);
132
+ ctx.rotate(confetto.rotation);
133
+
134
+ // Apply forces to velocity
135
+ confetto.velocity.x -= confetto.velocity.x * drag;
136
+ confetto.velocity.y = Math.min(confetto.velocity.y + gravity, terminalVelocity);
137
+ confetto.velocity.x += Math.random() > 0.5 ? Math.random() : -Math.random();
138
+
139
+ // Set position
140
+ confetto.position.x += confetto.velocity.x;
141
+ confetto.position.y += confetto.velocity.y;
142
+
143
+ // Delete confetti when out of frame
144
+ if (confetto.position.y >= canvas.height) confetti.splice(index, 1);
145
+
146
+ // Loop confetto x position
147
+ if (confetto.position.x > canvas.width) confetto.position.x = 0;
148
+ if (confetto.position.x < 0) confetto.position.x = canvas.width;
149
+
150
+ // Spin confetto by scaling y
151
+ confetto.scale.y = Math.cos(confetto.position.y * 0.1);
152
+ ctx.fillStyle = confetto.scale.y > 0 ? confetto.color.front : confetto.color.back;
153
+
154
+ // Draw confetti
155
+ ctx.fillRect(-width / 2, -height / 2, width, height);
156
+
157
+ // Reset transform matrix
158
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
159
+ });
160
+ if (confetti.length <= 10) {
161
+ shutDownCanvas();
162
+ return;
163
+ }
164
+ AnimationID = window.requestAnimationFrame(render);
165
+ };
166
+
167
+ //----------Resize----------
168
+ window.addEventListener('resize', function () {
169
+ resizeCanvas();
170
+ });
171
+ useEffect(function () {
172
+ var _document$getElementB;
173
+ initConfetti();
174
+ render();
175
+ (_document$getElementB = document.getElementById('ac-canvas')) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.appendChild(canvas);
176
+ }, []);
177
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
178
+ id: "ac-canvas",
179
+ onClick: function onClick() {
180
+ shutDownCanvas();
181
+ }
182
+ }));
183
+ };
184
+ export default AcAnimation;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { default as AcAnimation } from './ac-animation';
1
2
  export { default as CodeMirrorWrapper } from './code-mirror-wrapper';
2
3
  export { LangId, Language, Theme } from './code-mirror-wrapper/define';
3
4
  export { default as DraggableLayout } from './draggable-layout';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ export { default as AcAnimation } from "./ac-animation";
1
2
  export { default as CodeMirrorWrapper } from "./code-mirror-wrapper";
2
3
  export { LangId, Language, Theme } from "./code-mirror-wrapper/define";
3
4
  export { default as DraggableLayout } from "./draggable-layout";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x-star-design",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "A react component library developed by turingstar",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",