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