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