@cycjimmy/canvas-snow 3.0.2 → 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.
@@ -1,348 +1,246 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
- typeof define === 'function' && define.amd ? define(factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.CanvasSnow = factory());
5
- })(this, (function () { 'use strict';
6
-
7
- function _classCallCheck(instance, Constructor) {
8
- if (!(instance instanceof Constructor)) {
9
- throw new TypeError("Cannot call a class as a function");
10
- }
11
- }
12
-
13
- function _defineProperties(target, props) {
14
- for (var i = 0; i < props.length; i++) {
15
- var descriptor = props[i];
16
- descriptor.enumerable = descriptor.enumerable || false;
17
- descriptor.configurable = true;
18
- if ("value" in descriptor) descriptor.writable = true;
19
- Object.defineProperty(target, descriptor.key, descriptor);
20
- }
21
- }
22
-
23
- function _createClass(Constructor, protoProps, staticProps) {
24
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
25
- if (staticProps) _defineProperties(Constructor, staticProps);
26
- Object.defineProperty(Constructor, "prototype", {
27
- writable: false
28
- });
29
- return Constructor;
30
- }
31
-
32
- /**
33
- * determine a string type
34
- * @param str
35
- * @returns {boolean}
36
- */
37
- var isString = (str => typeof str === 'string' && str.constructor === String);
38
-
39
- var _default$2 = /*#__PURE__*/function () {
40
- function _default(x, y, radius, fn, canvasW, canvasH) {
41
- _classCallCheck(this, _default);
42
-
43
- this.x = x;
44
- this.y = y;
45
- this.r = radius;
46
- this.fn = fn;
47
- this.canvasW = canvasW;
48
- this.canvasH = canvasH;
49
- }
50
-
51
- _createClass(_default, [{
52
- key: "update",
53
- value: function update() {
54
- if (this.y > this.canvasH) {
55
- this.x = Math.random() * this.canvasW;
56
- this.y = 0;
57
- } else {
58
- this.x = this.fn.x(this.x, this.y);
59
- this.y = this.fn.y(this.y, this.y);
60
- }
61
- }
62
- }, {
63
- key: "draw",
64
- value: function draw(cxt) {
65
- var insideCxt = cxt;
66
- var grd = insideCxt.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.r);
67
- grd.addColorStop(0, "rgba(255, 255, 255, ".concat(this.r / 6, ")"));
68
- grd.addColorStop(0.5, "rgba(255, 255, 255, ".concat(this.r / 6 * 0.5, ")"));
69
- grd.addColorStop(1, 'rgba(255, 255, 255, 0)');
70
- insideCxt.fillStyle = grd;
71
- insideCxt.fillRect(this.x - this.r, this.y - this.r, this.r * 2, this.r * 2);
72
- }
73
- }]);
74
-
75
- return _default;
76
- }();
77
-
78
- var _default$1 = /*#__PURE__*/function () {
79
- function _default() {
80
- _classCallCheck(this, _default);
81
-
82
- this.list = [];
83
- }
84
-
85
- _createClass(_default, [{
86
- key: "update",
87
- value: function update() {
88
- for (var i = 0, len = this.size(); i < len; i += 1) {
89
- this.list[i].update();
90
- }
91
- }
92
- }, {
93
- key: "push",
94
- value: function push(snow) {
95
- this.list.push(snow);
96
- }
97
- }, {
98
- key: "draw",
99
- value: function draw(cxt) {
100
- for (var i = 0, len = this.size(); i < len; i += 1) {
101
- this.list[i].draw(cxt);
102
- }
103
- }
104
- }, {
105
- key: "get",
106
- value: function get(index) {
107
- return this.list[index];
108
- }
109
- }, {
110
- key: "size",
111
- value: function size() {
112
- return this.list.length;
113
- }
114
- }]);
115
-
116
- return _default;
117
- }();
118
-
119
- /**
120
- * handleDistanceNum
121
- * @param num
122
- * @param totalDistance
123
- * @returns {number|*|number}
124
- */
125
-
126
- var handleDistanceNum = function handleDistanceNum(num, totalDistance) {
127
- if (num.toString().indexOf('%') !== -1) {
128
- return num.split('%')[0] / 100 * totalDistance;
129
- }
130
-
131
- return isString(num) ? Number(num) : num;
132
- };
133
- /**
134
- * getTotalDistance
135
- * @param el
136
- * @param tip
137
- * @returns {number|DOMRect}
138
- */
139
-
140
- var getTotalDistance = function getTotalDistance(_ref) {
141
- var el = _ref.el,
142
- _ref$tip = _ref.tip,
143
- tip = _ref$tip === void 0 ? 'horizontal' : _ref$tip;
144
-
145
- switch (tip) {
146
- case 'horizontal':
147
- return el.getBoundingClientRect().width;
148
-
149
- case 'vertical':
150
- return el.getBoundingClientRect().height;
151
-
152
- default:
153
- return el.getBoundingClientRect();
154
- }
155
- };
156
- /**
157
- * getVerticalDistance
158
- * @param el
159
- * @param num
160
- * @returns {number|*}
161
- */
162
-
163
- var getVerticalDistance = function getVerticalDistance(el, num) {
164
- return handleDistanceNum(num, getTotalDistance({
165
- el: el,
166
- tip: 'vertical'
167
- }));
168
- };
169
- /**
170
- * getHorizontalDistance
171
- * @param el
172
- * @param num
173
- * @returns {number|*}
174
- */
175
-
176
- var getHorizontalDistance = function getHorizontalDistance(el, num) {
177
- return handleDistanceNum(num, getTotalDistance({
178
- el: el,
179
- tip: 'horizontal'
180
- }));
181
- };
182
-
183
- var _default = /*#__PURE__*/function () {
184
- function _default(_ref) {
185
- var context = _ref.context,
186
- _ref$width = _ref.width,
187
- width = _ref$width === void 0 ? '100%' : _ref$width,
188
- _ref$height = _ref.height,
189
- height = _ref$height === void 0 ? '100%' : _ref$height,
190
- cell = _ref.cell;
191
-
192
- _classCallCheck(this, _default);
193
-
194
- this.context = isString(context) ? document.querySelector(context) : context;
195
- this.width = getHorizontalDistance(this.context, width);
196
- this.height = getVerticalDistance(this.context, height);
197
- this.cell = cell;
198
- this.state = 'stop';
199
- this.snowList = null;
200
- this.canvas = null;
201
- this.cxt = null;
202
- this.interval = null;
203
- }
204
-
205
- _createClass(_default, [{
206
- key: "init",
207
- value: function init() {
208
- if (!this.canvas) {
209
- this._initCanvas();
210
- }
211
-
212
- if (!this.snowList) {
213
- this.snowList = new _default$1();
214
-
215
- this._newSnow();
216
- }
217
-
218
- return this;
219
- }
220
- }, {
221
- key: "_initCanvas",
222
- value: function _initCanvas() {
223
- this.canvas = document.createElement('canvas');
224
- this.context.appendChild(this.canvas);
225
- this.canvas.height = this.height;
226
- this.canvas.width = this.width;
227
- this.cxt = this.canvas.getContext('2d');
228
- }
229
- /**
230
- * animation start
231
- */
232
-
233
- }, {
234
- key: "start",
235
- value: function start() {
236
- var _this = this;
237
-
238
- if (!this.snowList) {
239
- this.init();
240
- }
241
-
242
- this.state = 'play';
243
- this.interval = setInterval(function () {
244
- _this.cxt.clearRect(0, 0, _this.canvas.width, _this.canvas.height);
245
-
246
- _this.snowList.update();
247
-
248
- _this.snowList.draw(_this.cxt);
249
- }, 13);
250
- }
251
- /**
252
- * animation stop
253
- */
254
-
255
- }, {
256
- key: "stop",
257
- value: function stop() {
258
- this.state = 'stop';
259
- clearInterval(this.interval);
260
- this.interval = null;
261
- this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
262
- }
263
- /**
264
- * clear
265
- */
266
-
267
- }, {
268
- key: "clear",
269
- value: function clear() {
270
- this.stop();
271
- this.snowList = null;
272
- }
273
- }, {
274
- key: "_newSnow",
275
- value: function _newSnow() {
276
- for (var i = 0; i < this.cell; i += 1) {
277
- var randomX = this._getRandom('x');
278
-
279
- var randomY = this._getRandom('y');
280
-
281
- var randomR = this._getRandom('r');
282
-
283
- var randomFnx = this._getRandom('fnx');
284
-
285
- var randomFny = this._getRandom('fny');
286
-
287
- var snow = new _default$2(randomX, randomY, randomR, {
288
- x: randomFnx,
289
- y: randomFny
290
- }, this.width, this.height);
291
- snow.draw(this.cxt);
292
- this.snowList.push(snow);
293
- }
294
- }
295
- /**
296
- * Generate random x-pos, y-pos or fn functions
297
- * @param {string} option x|y|fnx|fny
298
- * @return {int|Function}
299
- */
300
-
301
- }, {
302
- key: "_getRandom",
303
- value: function _getRandom(option) {
304
- var ret;
305
- var random;
306
-
307
- switch (option) {
308
- case 'x':
309
- ret = Math.random() * this.width;
310
- break;
311
-
312
- case 'y':
313
- ret = (Math.random() - 1) * this.height;
314
- break;
315
-
316
- case 'r':
317
- ret = 2 + Math.random() * 4;
318
- break;
319
-
320
- case 'fnx':
321
- random = 27 + Math.random() * 100;
322
-
323
- ret = function ret(x, y) {
324
- return x + 0.5 * Math.sin(y / random);
325
- };
326
-
327
- break;
328
-
329
- case 'fny':
330
- random = 0.4 + Math.random() * 1.4;
331
-
332
- ret = function ret(x, y) {
333
- return y + random;
334
- };
335
-
336
- break;
337
- }
338
-
339
- return ret;
340
- }
341
- }]);
342
-
343
- return _default;
344
- }();
345
-
346
- return _default;
347
-
348
- }));
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 v3.0.2
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,(function(){"use strict";function t(t,i){if(!(t instanceof i))throw new TypeError("Cannot call a class as a function")}function i(t,i){for(var n=0;n<i.length;n++){var e=i[n];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}function n(t,n,e){return n&&i(t.prototype,n),e&&i(t,e),Object.defineProperty(t,"prototype",{writable:!1}),t}var e=t=>"string"==typeof t&&t.constructor===String,s=function(){function i(n,e,s,a,h,r){t(this,i),this.x=n,this.y=e,this.r=s,this.fn=a,this.canvasW=h,this.canvasH=r}return n(i,[{key:"update",value:function(){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))}},{key:"draw",value:function(t){var i=t,n=i.createRadialGradient(this.x,this.y,0,this.x,this.y,this.r);n.addColorStop(0,"rgba(255, 255, 255, ".concat(this.r/6,")")),n.addColorStop(.5,"rgba(255, 255, 255, ".concat(this.r/6*.5,")")),n.addColorStop(1,"rgba(255, 255, 255, 0)"),i.fillStyle=n,i.fillRect(this.x-this.r,this.y-this.r,2*this.r,2*this.r)}}]),i}(),a=function(){function i(){t(this,i),this.list=[]}return n(i,[{key:"update",value:function(){for(var t=0,i=this.size();t<i;t+=1)this.list[t].update()}},{key:"push",value:function(t){this.list.push(t)}},{key:"draw",value:function(t){for(var i=0,n=this.size();i<n;i+=1)this.list[i].draw(t)}},{key:"get",value:function(t){return this.list[t]}},{key:"size",value:function(){return this.list.length}}]),i}(),h=function(t,i){return-1!==t.toString().indexOf("%")?t.split("%")[0]/100*i:e(t)?Number(t):t},r=function(t){var i=t.el,n=t.tip;switch(void 0===n?"horizontal":n){case"horizontal":return i.getBoundingClientRect().width;case"vertical":return i.getBoundingClientRect().height;default:return i.getBoundingClientRect()}};return function(){function i(n){var s,a=n.context,o=n.width,c=void 0===o?"100%":o,u=n.height,l=void 0===u?"100%":u,f=n.cell;t(this,i),this.context=e(a)?document.querySelector(a):a,this.width=(s=this.context,h(c,r({el:s,tip:"horizontal"}))),this.height=function(t,i){return h(i,r({el:t,tip:"vertical"}))}(this.context,l),this.cell=f,this.state="stop",this.snowList=null,this.canvas=null,this.cxt=null,this.interval=null}return n(i,[{key:"init",value:function(){return this.canvas||this._initCanvas(),this.snowList||(this.snowList=new a,this._newSnow()),this}},{key:"_initCanvas",value:function(){this.canvas=document.createElement("canvas"),this.context.appendChild(this.canvas),this.canvas.height=this.height,this.canvas.width=this.width,this.cxt=this.canvas.getContext("2d")}},{key:"start",value:function(){var t=this;this.snowList||this.init(),this.state="play",this.interval=setInterval((function(){t.cxt.clearRect(0,0,t.canvas.width,t.canvas.height),t.snowList.update(),t.snowList.draw(t.cxt)}),13)}},{key:"stop",value:function(){this.state="stop",clearInterval(this.interval),this.interval=null,this.cxt.clearRect(0,0,this.canvas.width,this.canvas.height)}},{key:"clear",value:function(){this.stop(),this.snowList=null}},{key:"_newSnow",value:function(){for(var t=0;t<this.cell;t+=1){var i=this._getRandom("x"),n=this._getRandom("y"),e=this._getRandom("r"),a=this._getRandom("fnx"),h=this._getRandom("fny"),r=new s(i,n,e,{x:a,y:h},this.width,this.height);r.draw(this.cxt),this.snowList.push(r)}}},{key:"_getRandom",value:function(t){var i,n;switch(t){case"x":i=Math.random()*this.width;break;case"y":i=(Math.random()-1)*this.height;break;case"r":i=2+4*Math.random();break;case"fnx":n=27+100*Math.random(),i=function(t,i){return t+.5*Math.sin(i/n)};break;case"fny":n=.4+1.4*Math.random(),i=function(t,i){return i+n}}return i}}]),i}()}));
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}}}}});