@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,340 +1,242 @@
1
- function _classCallCheck(instance, Constructor) {
2
- if (!(instance instanceof Constructor)) {
3
- throw new TypeError("Cannot call a class as a function");
4
- }
1
+ // node_modules/@cycjimmy/awesome-js-funcs/dist/judgeBasic/isString.js
2
+ /**
3
+ * determine a string type
4
+ * @param str
5
+ * @returns {boolean}
6
+ */
7
+ var isString_default = (str) => typeof str === "string" && str.constructor === String;
8
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/typeof.js
9
+ function _typeof(o) {
10
+ "@babel/helpers - typeof";
11
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
12
+ return typeof o;
13
+ } : function(o) {
14
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
15
+ }, _typeof(o);
5
16
  }
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
- }
17
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPrimitive.js
18
+ function toPrimitive(t, r) {
19
+ if ("object" != _typeof(t) || !t) return t;
20
+ var e = t[Symbol.toPrimitive];
21
+ if (void 0 !== e) {
22
+ var i = e.call(t, r || "default");
23
+ if ("object" != _typeof(i)) return i;
24
+ throw new TypeError("@@toPrimitive must return a primitive value.");
25
+ }
26
+ return ("string" === r ? String : Number)(t);
15
27
  }
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;
28
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPropertyKey.js
29
+ function toPropertyKey(t) {
30
+ var i = toPrimitive(t, "string");
31
+ return "symbol" == _typeof(i) ? i : i + "";
24
32
  }
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;
33
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/defineProperty.js
34
+ function _defineProperty(e, r, t) {
35
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
36
+ value: t,
37
+ enumerable: !0,
38
+ configurable: !0,
39
+ writable: !0
40
+ }) : e[r] = t, e;
41
+ }
42
+ // src/Snow.ts
43
+ var Snow = class {
44
+ constructor(x, y, radius, fn, canvasW, canvasH) {
45
+ _defineProperty(this, "x", void 0);
46
+ _defineProperty(this, "y", void 0);
47
+ _defineProperty(this, "r", void 0);
48
+ _defineProperty(this, "fn", void 0);
49
+ _defineProperty(this, "canvasW", void 0);
50
+ _defineProperty(this, "canvasH", void 0);
51
+ this.x = x;
52
+ this.y = y;
53
+ this.r = radius;
54
+ this.fn = fn;
55
+ this.canvasW = canvasW;
56
+ this.canvasH = canvasH;
57
+ }
58
+ update() {
59
+ if (this.y > this.canvasH) {
60
+ this.x = Math.random() * this.canvasW;
61
+ this.y = 0;
62
+ } else {
63
+ this.x = this.fn.x(this.x, this.y);
64
+ this.y = this.fn.y(this.y, this.y);
65
+ }
66
+ }
67
+ draw(cxt) {
68
+ const grd = cxt.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.r);
69
+ grd.addColorStop(0, `rgba(255, 255, 255, ${this.r / 6})`);
70
+ grd.addColorStop(.5, `rgba(255, 255, 255, ${this.r / 6 * .5})`);
71
+ grd.addColorStop(1, "rgba(255, 255, 255, 0)");
72
+ cxt.fillStyle = grd;
73
+ cxt.fillRect(this.x - this.r, this.y - this.r, this.r * 2, this.r * 2);
74
+ }
126
75
  };
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
- }
76
+ // src/SnowList.ts
77
+ var SnowList = class {
78
+ constructor() {
79
+ _defineProperty(this, "list", []);
80
+ }
81
+ update() {
82
+ for (let i = 0, len = this.size(); i < len; i += 1) this.list[i].update();
83
+ }
84
+ push(snow) {
85
+ this.list.push(snow);
86
+ }
87
+ draw(cxt) {
88
+ for (let i = 0, len = this.size(); i < len; i += 1) this.list[i].draw(cxt);
89
+ }
90
+ get(index) {
91
+ return this.list[index];
92
+ }
93
+ size() {
94
+ return this.list.length;
95
+ }
149
96
  };
97
+ // src/tools.ts
150
98
  /**
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
- };
99
+ * handleDistanceNum
100
+ * @param num A number, a numeric string, or a percentage string.
101
+ * @param totalDistance The container size the percentage refers to.
102
+ */
103
+ function handleDistanceNum(num, totalDistance) {
104
+ if (isString_default(num) && num.includes("%")) return Number(num.split("%")[0]) / 100 * totalDistance;
105
+ return isString_default(num) ? Number(num) : num;
106
+ }
107
+ function getTotalDistance({ el, tip = "horizontal" }) {
108
+ switch (tip) {
109
+ case "horizontal": return el.getBoundingClientRect().width;
110
+ case "vertical": return el.getBoundingClientRect().height;
111
+ default: return el.getBoundingClientRect();
112
+ }
113
+ }
163
114
  /**
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
- }));
115
+ * getVerticalDistance
116
+ * @param el The measured container.
117
+ * @param num A number, a numeric string, or a percentage string.
118
+ */
119
+ function getVerticalDistance(el, num) {
120
+ return handleDistanceNum(num, getTotalDistance({
121
+ el,
122
+ tip: "vertical"
123
+ }));
124
+ }
125
+ /**
126
+ * getHorizontalDistance
127
+ * @param el The measured container.
128
+ * @param num A number, a numeric string, or a percentage string.
129
+ */
130
+ function getHorizontalDistance(el, num) {
131
+ return handleDistanceNum(num, getTotalDistance({
132
+ el,
133
+ tip: "horizontal"
134
+ }));
135
+ }
136
+ // src/index.ts
137
+ var CanvasSnow = class {
138
+ constructor({ context, width = "100%", height = "100%", cell }) {
139
+ _defineProperty(this, "context", void 0);
140
+ _defineProperty(this, "width", void 0);
141
+ _defineProperty(this, "height", void 0);
142
+ _defineProperty(this, "cell", void 0);
143
+ _defineProperty(this, "state", void 0);
144
+ _defineProperty(this, "snowList", void 0);
145
+ _defineProperty(this, "canvas", void 0);
146
+ _defineProperty(this, "cxt", void 0);
147
+ _defineProperty(this, "interval", void 0);
148
+ const target = isString_default(context) ? document.querySelector(context) : context;
149
+ if (!target) throw new Error(`CanvasSnow could not find the element matching "${String(context)}".`);
150
+ this.context = target;
151
+ this.width = getHorizontalDistance(this.context, width);
152
+ this.height = getVerticalDistance(this.context, height);
153
+ this.cell = cell;
154
+ this.state = "stop";
155
+ this.snowList = null;
156
+ this.canvas = null;
157
+ this.cxt = null;
158
+ this.interval = null;
159
+ }
160
+ init() {
161
+ if (!this.canvas) this.initCanvas();
162
+ if (!this.snowList) {
163
+ this.snowList = new SnowList();
164
+ this.newSnow();
165
+ }
166
+ return this;
167
+ }
168
+ /**
169
+ * animation start
170
+ */
171
+ start() {
172
+ if (!this.snowList) this.init();
173
+ const { cxt, canvas, snowList } = this;
174
+ /* v8 ignore next 3 */
175
+ if (!cxt || !canvas || !snowList) throw new Error("CanvasSnow must be initialized before it can start.");
176
+ this.state = "play";
177
+ this.interval = setInterval(() => {
178
+ cxt.clearRect(0, 0, canvas.width, canvas.height);
179
+ snowList.update();
180
+ snowList.draw(cxt);
181
+ }, 13);
182
+ }
183
+ /**
184
+ * animation stop
185
+ */
186
+ stop() {
187
+ this.state = "stop";
188
+ if (this.interval !== null) {
189
+ clearInterval(this.interval);
190
+ this.interval = null;
191
+ }
192
+ const { cxt, canvas } = this;
193
+ if (!cxt || !canvas) throw new Error("CanvasSnow must be initialized before it can stop.");
194
+ cxt.clearRect(0, 0, canvas.width, canvas.height);
195
+ }
196
+ /**
197
+ * clear
198
+ */
199
+ clear() {
200
+ this.stop();
201
+ this.snowList = null;
202
+ }
203
+ initCanvas() {
204
+ const canvas = document.createElement("canvas");
205
+ this.canvas = canvas;
206
+ this.context.appendChild(canvas);
207
+ canvas.height = this.height;
208
+ canvas.width = this.width;
209
+ const cxt = canvas.getContext("2d");
210
+ if (!cxt) throw new Error("CanvasSnow could not acquire a 2D canvas context.");
211
+ this.cxt = cxt;
212
+ }
213
+ newSnow() {
214
+ const { snowList, cxt } = this;
215
+ /* v8 ignore next 3 */
216
+ if (!snowList || !cxt) throw new Error("CanvasSnow must be initialized before snow can be generated.");
217
+ for (let i = 0; i < this.cell; i += 1) {
218
+ const snow = new Snow(this.getRandom("x"), this.getRandom("y"), this.getRandom("r"), {
219
+ x: this.getRandom("fnx"),
220
+ y: this.getRandom("fny")
221
+ }, this.width, this.height);
222
+ snow.draw(cxt);
223
+ snowList.push(snow);
224
+ }
225
+ }
226
+ getRandom(option) {
227
+ switch (option) {
228
+ case "x": return Math.random() * this.width;
229
+ case "y": return (Math.random() - 1) * this.height;
230
+ case "r": return 2 + Math.random() * 4;
231
+ case "fnx": {
232
+ const random = 27 + Math.random() * 100;
233
+ return (x, y) => x + .5 * Math.sin(y / random);
234
+ }
235
+ case "fny": {
236
+ const random = .4 + Math.random() * 1.4;
237
+ return (_x, y) => y + random;
238
+ }
239
+ }
240
+ }
175
241
  };
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 };
242
+ export { CanvasSnow as default };