@cycjimmy/canvas-snow 3.0.2 → 3.0.3

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