@cycjimmy/canvas-snow 3.0.3 → 4.0.0
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.
- package/README.md +69 -25
- package/dist/Snow.d.ts +15 -0
- package/dist/SnowList.d.ts +9 -0
- package/dist/canvas-snow.esm.js +233 -295
- package/dist/canvas-snow.umd.js +246 -312
- package/dist/canvas-snow.umd.min.js +2 -2
- package/dist/index.d.ts +45 -0
- package/dist/tools.d.ts +35 -0
- package/package.json +30 -14
- package/dist/canvas-snow.cjs +0 -306
package/dist/canvas-snow.umd.js
CHANGED
|
@@ -1,312 +1,246 @@
|
|
|
1
|
-
(function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
*/
|
|
248
|
-
}, {
|
|
249
|
-
key: "clear",
|
|
250
|
-
value: function clear() {
|
|
251
|
-
this.stop();
|
|
252
|
-
this.snowList = null;
|
|
253
|
-
}
|
|
254
|
-
}, {
|
|
255
|
-
key: "_newSnow",
|
|
256
|
-
value: function _newSnow() {
|
|
257
|
-
for (var i = 0; i < this.cell; i += 1) {
|
|
258
|
-
var randomX = this._getRandom('x');
|
|
259
|
-
var randomY = this._getRandom('y');
|
|
260
|
-
var randomR = this._getRandom('r');
|
|
261
|
-
var randomFnx = this._getRandom('fnx');
|
|
262
|
-
var randomFny = this._getRandom('fny');
|
|
263
|
-
var snow = new _default$2(randomX, randomY, randomR, {
|
|
264
|
-
x: randomFnx,
|
|
265
|
-
y: randomFny
|
|
266
|
-
}, this.width, this.height);
|
|
267
|
-
snow.draw(this.cxt);
|
|
268
|
-
this.snowList.push(snow);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Generate random x-pos, y-pos or fn functions
|
|
274
|
-
* @param {string} option x|y|fnx|fny
|
|
275
|
-
* @return {int|Function}
|
|
276
|
-
*/
|
|
277
|
-
}, {
|
|
278
|
-
key: "_getRandom",
|
|
279
|
-
value: function _getRandom(option) {
|
|
280
|
-
var ret;
|
|
281
|
-
var random;
|
|
282
|
-
switch (option) {
|
|
283
|
-
case 'x':
|
|
284
|
-
ret = Math.random() * this.width;
|
|
285
|
-
break;
|
|
286
|
-
case 'y':
|
|
287
|
-
ret = (Math.random() - 1) * this.height;
|
|
288
|
-
break;
|
|
289
|
-
case 'r':
|
|
290
|
-
ret = 2 + Math.random() * 4;
|
|
291
|
-
break;
|
|
292
|
-
case 'fnx':
|
|
293
|
-
random = 27 + Math.random() * 100;
|
|
294
|
-
ret = function ret(x, y) {
|
|
295
|
-
return x + 0.5 * Math.sin(y / random);
|
|
296
|
-
};
|
|
297
|
-
break;
|
|
298
|
-
case 'fny':
|
|
299
|
-
random = 0.4 + Math.random() * 1.4;
|
|
300
|
-
ret = function ret(x, y) {
|
|
301
|
-
return y + random;
|
|
302
|
-
};
|
|
303
|
-
break;
|
|
304
|
-
}
|
|
305
|
-
return ret;
|
|
306
|
-
}
|
|
307
|
-
}]);
|
|
308
|
-
}();
|
|
309
|
-
|
|
310
|
-
return _default;
|
|
311
|
-
|
|
312
|
-
}));
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define([], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global.CanvasSnow = factory());
|
|
3
|
+
})(this, function() {
|
|
4
|
+
// node_modules/@cycjimmy/awesome-js-funcs/dist/judgeBasic/isString.js
|
|
5
|
+
/**
|
|
6
|
+
* determine a string type
|
|
7
|
+
* @param str
|
|
8
|
+
* @returns {boolean}
|
|
9
|
+
*/
|
|
10
|
+
var isString_default = (str) => typeof str === "string" && str.constructor === String;
|
|
11
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/typeof.js
|
|
12
|
+
function _typeof(o) {
|
|
13
|
+
"@babel/helpers - typeof";
|
|
14
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
15
|
+
return typeof o;
|
|
16
|
+
} : function(o) {
|
|
17
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
18
|
+
}, _typeof(o);
|
|
19
|
+
}
|
|
20
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/toPrimitive.js
|
|
21
|
+
function toPrimitive(t, r) {
|
|
22
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
23
|
+
var e = t[Symbol.toPrimitive];
|
|
24
|
+
if (void 0 !== e) {
|
|
25
|
+
var i = e.call(t, r || "default");
|
|
26
|
+
if ("object" != _typeof(i)) return i;
|
|
27
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
28
|
+
}
|
|
29
|
+
return ("string" === r ? String : Number)(t);
|
|
30
|
+
}
|
|
31
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/toPropertyKey.js
|
|
32
|
+
function toPropertyKey(t) {
|
|
33
|
+
var i = toPrimitive(t, "string");
|
|
34
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
35
|
+
}
|
|
36
|
+
// \0@oxc-project+runtime@0.156.0/helpers/esm/defineProperty.js
|
|
37
|
+
function _defineProperty(e, r, t) {
|
|
38
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
39
|
+
value: t,
|
|
40
|
+
enumerable: !0,
|
|
41
|
+
configurable: !0,
|
|
42
|
+
writable: !0
|
|
43
|
+
}) : e[r] = t, e;
|
|
44
|
+
}
|
|
45
|
+
// src/Snow.ts
|
|
46
|
+
var Snow = class {
|
|
47
|
+
constructor(x, y, radius, fn, canvasW, canvasH) {
|
|
48
|
+
_defineProperty(this, "x", void 0);
|
|
49
|
+
_defineProperty(this, "y", void 0);
|
|
50
|
+
_defineProperty(this, "r", void 0);
|
|
51
|
+
_defineProperty(this, "fn", void 0);
|
|
52
|
+
_defineProperty(this, "canvasW", void 0);
|
|
53
|
+
_defineProperty(this, "canvasH", void 0);
|
|
54
|
+
this.x = x;
|
|
55
|
+
this.y = y;
|
|
56
|
+
this.r = radius;
|
|
57
|
+
this.fn = fn;
|
|
58
|
+
this.canvasW = canvasW;
|
|
59
|
+
this.canvasH = canvasH;
|
|
60
|
+
}
|
|
61
|
+
update() {
|
|
62
|
+
if (this.y > this.canvasH) {
|
|
63
|
+
this.x = Math.random() * this.canvasW;
|
|
64
|
+
this.y = 0;
|
|
65
|
+
} else {
|
|
66
|
+
this.x = this.fn.x(this.x, this.y);
|
|
67
|
+
this.y = this.fn.y(this.y, this.y);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
draw(cxt) {
|
|
71
|
+
const grd = cxt.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.r);
|
|
72
|
+
grd.addColorStop(0, `rgba(255, 255, 255, ${this.r / 6})`);
|
|
73
|
+
grd.addColorStop(.5, `rgba(255, 255, 255, ${this.r / 6 * .5})`);
|
|
74
|
+
grd.addColorStop(1, "rgba(255, 255, 255, 0)");
|
|
75
|
+
cxt.fillStyle = grd;
|
|
76
|
+
cxt.fillRect(this.x - this.r, this.y - this.r, this.r * 2, this.r * 2);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
// src/SnowList.ts
|
|
80
|
+
var SnowList = class {
|
|
81
|
+
constructor() {
|
|
82
|
+
_defineProperty(this, "list", []);
|
|
83
|
+
}
|
|
84
|
+
update() {
|
|
85
|
+
for (let i = 0, len = this.size(); i < len; i += 1) this.list[i].update();
|
|
86
|
+
}
|
|
87
|
+
push(snow) {
|
|
88
|
+
this.list.push(snow);
|
|
89
|
+
}
|
|
90
|
+
draw(cxt) {
|
|
91
|
+
for (let i = 0, len = this.size(); i < len; i += 1) this.list[i].draw(cxt);
|
|
92
|
+
}
|
|
93
|
+
get(index) {
|
|
94
|
+
return this.list[index];
|
|
95
|
+
}
|
|
96
|
+
size() {
|
|
97
|
+
return this.list.length;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
// src/tools.ts
|
|
101
|
+
/**
|
|
102
|
+
* handleDistanceNum
|
|
103
|
+
* @param num A number, a numeric string, or a percentage string.
|
|
104
|
+
* @param totalDistance The container size the percentage refers to.
|
|
105
|
+
*/
|
|
106
|
+
function handleDistanceNum(num, totalDistance) {
|
|
107
|
+
if (isString_default(num) && num.includes("%")) return Number(num.split("%")[0]) / 100 * totalDistance;
|
|
108
|
+
return isString_default(num) ? Number(num) : num;
|
|
109
|
+
}
|
|
110
|
+
function getTotalDistance({ el, tip = "horizontal" }) {
|
|
111
|
+
switch (tip) {
|
|
112
|
+
case "horizontal": return el.getBoundingClientRect().width;
|
|
113
|
+
case "vertical": return el.getBoundingClientRect().height;
|
|
114
|
+
default: return el.getBoundingClientRect();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* getVerticalDistance
|
|
119
|
+
* @param el The measured container.
|
|
120
|
+
* @param num A number, a numeric string, or a percentage string.
|
|
121
|
+
*/
|
|
122
|
+
function getVerticalDistance(el, num) {
|
|
123
|
+
return handleDistanceNum(num, getTotalDistance({
|
|
124
|
+
el,
|
|
125
|
+
tip: "vertical"
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* getHorizontalDistance
|
|
130
|
+
* @param el The measured container.
|
|
131
|
+
* @param num A number, a numeric string, or a percentage string.
|
|
132
|
+
*/
|
|
133
|
+
function getHorizontalDistance(el, num) {
|
|
134
|
+
return handleDistanceNum(num, getTotalDistance({
|
|
135
|
+
el,
|
|
136
|
+
tip: "horizontal"
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
// src/index.ts
|
|
140
|
+
var CanvasSnow = class {
|
|
141
|
+
constructor({ context, width = "100%", height = "100%", cell }) {
|
|
142
|
+
_defineProperty(this, "context", void 0);
|
|
143
|
+
_defineProperty(this, "width", void 0);
|
|
144
|
+
_defineProperty(this, "height", void 0);
|
|
145
|
+
_defineProperty(this, "cell", void 0);
|
|
146
|
+
_defineProperty(this, "state", void 0);
|
|
147
|
+
_defineProperty(this, "snowList", void 0);
|
|
148
|
+
_defineProperty(this, "canvas", void 0);
|
|
149
|
+
_defineProperty(this, "cxt", void 0);
|
|
150
|
+
_defineProperty(this, "interval", void 0);
|
|
151
|
+
const target = isString_default(context) ? document.querySelector(context) : context;
|
|
152
|
+
if (!target) throw new Error(`CanvasSnow could not find the element matching "${String(context)}".`);
|
|
153
|
+
this.context = target;
|
|
154
|
+
this.width = getHorizontalDistance(this.context, width);
|
|
155
|
+
this.height = getVerticalDistance(this.context, height);
|
|
156
|
+
this.cell = cell;
|
|
157
|
+
this.state = "stop";
|
|
158
|
+
this.snowList = null;
|
|
159
|
+
this.canvas = null;
|
|
160
|
+
this.cxt = null;
|
|
161
|
+
this.interval = null;
|
|
162
|
+
}
|
|
163
|
+
init() {
|
|
164
|
+
if (!this.canvas) this.initCanvas();
|
|
165
|
+
if (!this.snowList) {
|
|
166
|
+
this.snowList = new SnowList();
|
|
167
|
+
this.newSnow();
|
|
168
|
+
}
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* animation start
|
|
173
|
+
*/
|
|
174
|
+
start() {
|
|
175
|
+
if (!this.snowList) this.init();
|
|
176
|
+
const { cxt, canvas, snowList } = this;
|
|
177
|
+
/* v8 ignore next 3 */
|
|
178
|
+
if (!cxt || !canvas || !snowList) throw new Error("CanvasSnow must be initialized before it can start.");
|
|
179
|
+
this.state = "play";
|
|
180
|
+
this.interval = setInterval(() => {
|
|
181
|
+
cxt.clearRect(0, 0, canvas.width, canvas.height);
|
|
182
|
+
snowList.update();
|
|
183
|
+
snowList.draw(cxt);
|
|
184
|
+
}, 13);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* animation stop
|
|
188
|
+
*/
|
|
189
|
+
stop() {
|
|
190
|
+
this.state = "stop";
|
|
191
|
+
if (this.interval !== null) {
|
|
192
|
+
clearInterval(this.interval);
|
|
193
|
+
this.interval = null;
|
|
194
|
+
}
|
|
195
|
+
const { cxt, canvas } = this;
|
|
196
|
+
if (!cxt || !canvas) throw new Error("CanvasSnow must be initialized before it can stop.");
|
|
197
|
+
cxt.clearRect(0, 0, canvas.width, canvas.height);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* clear
|
|
201
|
+
*/
|
|
202
|
+
clear() {
|
|
203
|
+
this.stop();
|
|
204
|
+
this.snowList = null;
|
|
205
|
+
}
|
|
206
|
+
initCanvas() {
|
|
207
|
+
const canvas = document.createElement("canvas");
|
|
208
|
+
this.canvas = canvas;
|
|
209
|
+
this.context.appendChild(canvas);
|
|
210
|
+
canvas.height = this.height;
|
|
211
|
+
canvas.width = this.width;
|
|
212
|
+
const cxt = canvas.getContext("2d");
|
|
213
|
+
if (!cxt) throw new Error("CanvasSnow could not acquire a 2D canvas context.");
|
|
214
|
+
this.cxt = cxt;
|
|
215
|
+
}
|
|
216
|
+
newSnow() {
|
|
217
|
+
const { snowList, cxt } = this;
|
|
218
|
+
/* v8 ignore next 3 */
|
|
219
|
+
if (!snowList || !cxt) throw new Error("CanvasSnow must be initialized before snow can be generated.");
|
|
220
|
+
for (let i = 0; i < this.cell; i += 1) {
|
|
221
|
+
const snow = new Snow(this.getRandom("x"), this.getRandom("y"), this.getRandom("r"), {
|
|
222
|
+
x: this.getRandom("fnx"),
|
|
223
|
+
y: this.getRandom("fny")
|
|
224
|
+
}, this.width, this.height);
|
|
225
|
+
snow.draw(cxt);
|
|
226
|
+
snowList.push(snow);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
getRandom(option) {
|
|
230
|
+
switch (option) {
|
|
231
|
+
case "x": return Math.random() * this.width;
|
|
232
|
+
case "y": return (Math.random() - 1) * this.height;
|
|
233
|
+
case "r": return 2 + Math.random() * 4;
|
|
234
|
+
case "fnx": {
|
|
235
|
+
const random = 27 + Math.random() * 100;
|
|
236
|
+
return (x, y) => x + .5 * Math.sin(y / random);
|
|
237
|
+
}
|
|
238
|
+
case "fny": {
|
|
239
|
+
const random = .4 + Math.random() * 1.4;
|
|
240
|
+
return (_x, y) => y + random;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
return CanvasSnow;
|
|
246
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @cycjimmy/canvas-snow
|
|
2
|
+
* @cycjimmy/canvas-snow v4.0.0
|
|
3
3
|
* Homepage: https://github.com/cycjimmy/canvas-snow#readme
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
|
-
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).CanvasSnow=i()}(this,
|
|
6
|
+
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define([],i):(t="undefined"!=typeof globalThis?globalThis:t||self).CanvasSnow=i()}(this,function(){var t=t=>"string"==typeof t&&t.constructor===String;function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}function s(t){var s=function(t,s){if("object"!=i(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var e=n.call(t,s||"default");if("object"!=i(e))return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===s?String:Number)(t)}(t,"string");return"symbol"==i(s)?s:s+""}function n(t,i,n){return(i=s(i))in t?Object.defineProperty(t,i,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[i]=n,t}var e=class{constructor(t,i,s,e,o,r){n(this,"x",void 0),n(this,"y",void 0),n(this,"r",void 0),n(this,"fn",void 0),n(this,"canvasW",void 0),n(this,"canvasH",void 0),this.x=t,this.y=i,this.r=s,this.fn=e,this.canvasW=o,this.canvasH=r}update(){this.y>this.canvasH?(this.x=Math.random()*this.canvasW,this.y=0):(this.x=this.fn.x(this.x,this.y),this.y=this.fn.y(this.y,this.y))}draw(t){const i=t.createRadialGradient(this.x,this.y,0,this.x,this.y,this.r);i.addColorStop(0,`rgba(255, 255, 255, ${this.r/6})`),i.addColorStop(.5,`rgba(255, 255, 255, ${this.r/6*.5})`),i.addColorStop(1,"rgba(255, 255, 255, 0)"),t.fillStyle=i,t.fillRect(this.x-this.r,this.y-this.r,2*this.r,2*this.r)}},o=class{constructor(){n(this,"list",[])}update(){for(let t=0,i=this.size();t<i;t+=1)this.list[t].update()}push(t){this.list.push(t)}draw(t){for(let i=0,s=this.size();i<s;i+=1)this.list[i].draw(t)}get(t){return this.list[t]}size(){return this.list.length}};function r(i,s){return t(i)&&i.includes("%")?Number(i.split("%")[0])/100*s:t(i)?Number(i):i}function h({el:t,tip:i="horizontal"}){switch(i){case"horizontal":return t.getBoundingClientRect().width;case"vertical":return t.getBoundingClientRect().height;default:return t.getBoundingClientRect()}}return class{constructor({context:i,width:s="100%",height:e="100%",cell:o}){n(this,"context",void 0),n(this,"width",void 0),n(this,"height",void 0),n(this,"cell",void 0),n(this,"state",void 0),n(this,"snowList",void 0),n(this,"canvas",void 0),n(this,"cxt",void 0),n(this,"interval",void 0);const a=t(i)?document.querySelector(i):i;if(!a)throw new Error(`CanvasSnow could not find the element matching "${String(i)}".`);var c;this.context=a,this.width=(c=this.context,r(s,h({el:c,tip:"horizontal"}))),this.height=function(t,i){return r(i,h({el:t,tip:"vertical"}))}(this.context,e),this.cell=o,this.state="stop",this.snowList=null,this.canvas=null,this.cxt=null,this.interval=null}init(){return this.canvas||this.initCanvas(),this.snowList||(this.snowList=new o,this.newSnow()),this}start(){this.snowList||this.init();const{cxt:t,canvas:i,snowList:s}=this;if(!t||!i||!s)throw new Error("CanvasSnow must be initialized before it can start.");this.state="play",this.interval=setInterval(()=>{t.clearRect(0,0,i.width,i.height),s.update(),s.draw(t)},13)}stop(){this.state="stop",null!==this.interval&&(clearInterval(this.interval),this.interval=null);const{cxt:t,canvas:i}=this;if(!t||!i)throw new Error("CanvasSnow must be initialized before it can stop.");t.clearRect(0,0,i.width,i.height)}clear(){this.stop(),this.snowList=null}initCanvas(){const t=document.createElement("canvas");this.canvas=t,this.context.appendChild(t),t.height=this.height,t.width=this.width;const i=t.getContext("2d");if(!i)throw new Error("CanvasSnow could not acquire a 2D canvas context.");this.cxt=i}newSnow(){const{snowList:t,cxt:i}=this;if(!t||!i)throw new Error("CanvasSnow must be initialized before snow can be generated.");for(let s=0;s<this.cell;s+=1){const s=this.getRandom("x"),n=this.getRandom("y"),o=this.getRandom("r"),r=this.getRandom("fnx"),h=this.getRandom("fny"),a=new e(s,n,o,{x:r,y:h},this.width,this.height);a.draw(i),t.push(a)}}getRandom(t){switch(t){case"x":return Math.random()*this.width;case"y":return(Math.random()-1)*this.height;case"r":return 2+4*Math.random();case"fnx":{const t=27+100*Math.random();return(i,s)=>i+.5*Math.sin(s/t)}case"fny":{const t=.4+1.4*Math.random();return(i,s)=>s+t}}}}});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import SnowList from './SnowList.js';
|
|
2
|
+
export interface CanvasSnowOptions {
|
|
3
|
+
/** The wrapper element, or a selector matching it. */
|
|
4
|
+
context: Element | string;
|
|
5
|
+
/** Canvas width as a number, a numeric string, or a percentage string. */
|
|
6
|
+
width?: number | string;
|
|
7
|
+
/** Canvas height as a number, a numeric string, or a percentage string. */
|
|
8
|
+
height?: number | string;
|
|
9
|
+
/** The number of snowflakes to generate. */
|
|
10
|
+
cell: number;
|
|
11
|
+
}
|
|
12
|
+
export type CanvasSnowState = 'stop' | 'play';
|
|
13
|
+
export default class CanvasSnow {
|
|
14
|
+
context: Element;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
cell: number;
|
|
18
|
+
state: CanvasSnowState;
|
|
19
|
+
snowList: SnowList | null;
|
|
20
|
+
canvas: HTMLCanvasElement | null;
|
|
21
|
+
cxt: CanvasRenderingContext2D | null;
|
|
22
|
+
interval: ReturnType<typeof setInterval> | null;
|
|
23
|
+
constructor({ context, width, height, cell }: CanvasSnowOptions);
|
|
24
|
+
init(): this;
|
|
25
|
+
/**
|
|
26
|
+
* animation start
|
|
27
|
+
*/
|
|
28
|
+
start(): void;
|
|
29
|
+
/**
|
|
30
|
+
* animation stop
|
|
31
|
+
*/
|
|
32
|
+
stop(): void;
|
|
33
|
+
/**
|
|
34
|
+
* clear
|
|
35
|
+
*/
|
|
36
|
+
clear(): void;
|
|
37
|
+
private initCanvas;
|
|
38
|
+
private newSnow;
|
|
39
|
+
/**
|
|
40
|
+
* Generate a random x-position, y-position, radius or motion function.
|
|
41
|
+
* The parameter type is the union of every call site, so the switch is
|
|
42
|
+
* exhaustive and nothing else can be passed in.
|
|
43
|
+
*/
|
|
44
|
+
private getRandom;
|
|
45
|
+
}
|