@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.
@@ -1,312 +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(a, n) {
8
- if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
9
- }
10
- function _defineProperties(e, r) {
11
- for (var t = 0; t < r.length; t++) {
12
- var o = r[t];
13
- o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
14
- }
15
- }
16
- function _createClass(e, r, t) {
17
- return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
18
- writable: !1
19
- }), e;
20
- }
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 );
26
- if ("object" != typeof i) return i;
27
- throw new TypeError("@@toPrimitive must return a primitive value.");
28
- }
29
- return (String )(t);
30
- }
31
- function _toPropertyKey(t) {
32
- var i = _toPrimitive(t, "string");
33
- return "symbol" == typeof i ? i : i + "";
34
- }
35
-
36
- /**
37
- * determine a string type
38
- * @param str
39
- * @returns {boolean}
40
- */
41
- var isString = (str => typeof str === 'string' && str.constructor === String);
42
-
43
- var _default$2 = /*#__PURE__*/function () {
44
- function _default(x, y, radius, fn, canvasW, canvasH) {
45
- _classCallCheck(this, _default);
46
- this.x = x;
47
- this.y = y;
48
- this.r = radius;
49
- this.fn = fn;
50
- this.canvasW = canvasW;
51
- this.canvasH = canvasH;
52
- }
53
- return _createClass(_default, [{
54
- key: "update",
55
- value: function update() {
56
- if (this.y > this.canvasH) {
57
- this.x = Math.random() * this.canvasW;
58
- this.y = 0;
59
- } else {
60
- this.x = this.fn.x(this.x, this.y);
61
- this.y = this.fn.y(this.y, this.y);
62
- }
63
- }
64
- }, {
65
- key: "draw",
66
- value: function draw(cxt) {
67
- var insideCxt = cxt;
68
- var grd = insideCxt.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.r);
69
- grd.addColorStop(0, "rgba(255, 255, 255, ".concat(this.r / 6, ")"));
70
- grd.addColorStop(0.5, "rgba(255, 255, 255, ".concat(this.r / 6 * 0.5, ")"));
71
- grd.addColorStop(1, 'rgba(255, 255, 255, 0)');
72
- insideCxt.fillStyle = grd;
73
- insideCxt.fillRect(this.x - this.r, this.y - this.r, this.r * 2, this.r * 2);
74
- }
75
- }]);
76
- }();
77
-
78
- var _default$1 = /*#__PURE__*/function () {
79
- function _default() {
80
- _classCallCheck(this, _default);
81
- this.list = [];
82
- }
83
- return _createClass(_default, [{
84
- key: "update",
85
- value: function update() {
86
- for (var i = 0, len = this.size(); i < len; i += 1) {
87
- this.list[i].update();
88
- }
89
- }
90
- }, {
91
- key: "push",
92
- value: function push(snow) {
93
- this.list.push(snow);
94
- }
95
- }, {
96
- key: "draw",
97
- value: function draw(cxt) {
98
- for (var i = 0, len = this.size(); i < len; i += 1) {
99
- this.list[i].draw(cxt);
100
- }
101
- }
102
- }, {
103
- key: "get",
104
- value: function get(index) {
105
- return this.list[index];
106
- }
107
- }, {
108
- key: "size",
109
- value: function size() {
110
- return this.list.length;
111
- }
112
- }]);
113
- }();
114
-
115
- /**
116
- * handleDistanceNum
117
- * @param num
118
- * @param totalDistance
119
- * @returns {number|*|number}
120
- */
121
- var handleDistanceNum = function handleDistanceNum(num, totalDistance) {
122
- if (num.toString().indexOf('%') !== -1) {
123
- return num.split('%')[0] / 100 * totalDistance;
124
- }
125
- return isString(num) ? Number(num) : num;
126
- };
127
-
128
- /**
129
- * getTotalDistance
130
- * @param el
131
- * @param tip
132
- * @returns {number|DOMRect}
133
- */
134
- var getTotalDistance = function getTotalDistance(_ref) {
135
- var el = _ref.el,
136
- _ref$tip = _ref.tip,
137
- tip = _ref$tip === void 0 ? 'horizontal' : _ref$tip;
138
- switch (tip) {
139
- case 'horizontal':
140
- return el.getBoundingClientRect().width;
141
- case 'vertical':
142
- return el.getBoundingClientRect().height;
143
- default:
144
- return el.getBoundingClientRect();
145
- }
146
- };
147
-
148
- /**
149
- * getVerticalDistance
150
- * @param el
151
- * @param num
152
- * @returns {number|*}
153
- */
154
- var getVerticalDistance = function getVerticalDistance(el, num) {
155
- return handleDistanceNum(num, getTotalDistance({
156
- el: el,
157
- tip: 'vertical'
158
- }));
159
- };
160
-
161
- /**
162
- * getHorizontalDistance
163
- * @param el
164
- * @param num
165
- * @returns {number|*}
166
- */
167
- var getHorizontalDistance = function getHorizontalDistance(el, num) {
168
- return handleDistanceNum(num, getTotalDistance({
169
- el: el,
170
- tip: 'horizontal'
171
- }));
172
- };
173
-
174
- var _default = /*#__PURE__*/function () {
175
- function _default(_ref) {
176
- var context = _ref.context,
177
- _ref$width = _ref.width,
178
- width = _ref$width === void 0 ? '100%' : _ref$width,
179
- _ref$height = _ref.height,
180
- height = _ref$height === void 0 ? '100%' : _ref$height,
181
- cell = _ref.cell;
182
- _classCallCheck(this, _default);
183
- this.context = isString(context) ? document.querySelector(context) : context;
184
- this.width = getHorizontalDistance(this.context, width);
185
- this.height = getVerticalDistance(this.context, height);
186
- this.cell = cell;
187
- this.state = 'stop';
188
- this.snowList = null;
189
- this.canvas = null;
190
- this.cxt = null;
191
- this.interval = null;
192
- }
193
- return _createClass(_default, [{
194
- key: "init",
195
- value: function init() {
196
- if (!this.canvas) {
197
- this._initCanvas();
198
- }
199
- if (!this.snowList) {
200
- this.snowList = new _default$1();
201
- this._newSnow();
202
- }
203
- return this;
204
- }
205
- }, {
206
- key: "_initCanvas",
207
- value: function _initCanvas() {
208
- this.canvas = document.createElement('canvas');
209
- this.context.appendChild(this.canvas);
210
- this.canvas.height = this.height;
211
- this.canvas.width = this.width;
212
- this.cxt = this.canvas.getContext('2d');
213
- }
214
-
215
- /**
216
- * animation start
217
- */
218
- }, {
219
- key: "start",
220
- value: function start() {
221
- var _this = this;
222
- if (!this.snowList) {
223
- this.init();
224
- }
225
- this.state = 'play';
226
- this.interval = setInterval(function () {
227
- _this.cxt.clearRect(0, 0, _this.canvas.width, _this.canvas.height);
228
- _this.snowList.update();
229
- _this.snowList.draw(_this.cxt);
230
- }, 13);
231
- }
232
-
233
- /**
234
- * animation stop
235
- */
236
- }, {
237
- key: "stop",
238
- value: function stop() {
239
- this.state = 'stop';
240
- clearInterval(this.interval);
241
- this.interval = null;
242
- this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
243
- }
244
-
245
- /**
246
- * clear
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 v3.0.3
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,e){return i&&function(t,i){for(var e=0;e<i.length;e++){var s=i[e];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,n(s.key),s)}}(t.prototype,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function n(t){var i=function(t,i){if("object"!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var e=n.call(t,i);if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t,"string");return"symbol"==typeof i?i:i+""}var e=t=>"string"==typeof t&&t.constructor===String,s=function(){return i((function i(n,e,s,a,r,h){t(this,i),this.x=n,this.y=e,this.r=s,this.fn=a,this.canvasW=r,this.canvasH=h}),[{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)}}])}(),a=function(){return i((function i(){t(this,i),this.list=[]}),[{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}}])}(),r=function(t,i){return-1!==t.toString().indexOf("%")?t.split("%")[0]/100*i:e(t)?Number(t):t},h=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(){return i((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,r(c,h({el:s,tip:"horizontal"}))),this.height=function(t,i){return r(i,h({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}),[{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"),r=this._getRandom("fny"),h=new s(i,n,e,{x:a,y:r},this.width,this.height);h.draw(this.cxt),this.snowList.push(h)}}},{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}}])}()}));
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}}}}});
@@ -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
+ }