@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.
@@ -0,0 +1,35 @@
1
+ export type DistanceInput = number | string;
2
+ /**
3
+ * handleDistanceNum
4
+ * @param num A number, a numeric string, or a percentage string.
5
+ * @param totalDistance The container size the percentage refers to.
6
+ */
7
+ export declare function handleDistanceNum(num: DistanceInput, totalDistance: number): number;
8
+ /**
9
+ * Measures `el` on the requested axis. An unknown `tip` returns the whole rect,
10
+ * which is what the original implementation fell back to.
11
+ */
12
+ export declare function getTotalDistance(options: {
13
+ el: Element;
14
+ tip: 'horizontal';
15
+ }): number;
16
+ export declare function getTotalDistance(options: {
17
+ el: Element;
18
+ tip: 'vertical';
19
+ }): number;
20
+ export declare function getTotalDistance(options: {
21
+ el: Element;
22
+ tip?: string;
23
+ }): number | DOMRect;
24
+ /**
25
+ * getVerticalDistance
26
+ * @param el The measured container.
27
+ * @param num A number, a numeric string, or a percentage string.
28
+ */
29
+ export declare function getVerticalDistance(el: Element, num: DistanceInput): number;
30
+ /**
31
+ * getHorizontalDistance
32
+ * @param el The measured container.
33
+ * @param num A number, a numeric string, or a percentage string.
34
+ */
35
+ export declare function getHorizontalDistance(el: Element, num: DistanceInput): number;
package/package.json CHANGED
@@ -1,28 +1,44 @@
1
1
  {
2
2
  "name": "@cycjimmy/canvas-snow",
3
- "version": "3.0.3",
4
- "description": "Generates snow in canvas.",
5
3
  "type": "module",
6
- "main": "dist/canvas-snow.cjs",
7
- "module": "dist/canvas-snow.esm.js",
8
- "browser": "dist/canvas-snow.umd.min.js",
9
- "exports": {
10
- "require": "./dist/canvas-snow.cjs",
11
- "import": "./dist/canvas-snow.esm.js"
12
- },
4
+ "version": "4.0.0",
5
+ "description": "Generates snow in canvas.",
6
+ "author": "cycjimmy <cycjimmy@gmail.com> (https://github.com/cycjimmy)",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/cycjimmy/canvas-snow#readme",
13
9
  "repository": {
14
10
  "type": "git",
15
11
  "url": "git+https://github.com/cycjimmy/canvas-snow.git"
16
12
  },
13
+ "bugs": {
14
+ "url": "https://github.com/cycjimmy/canvas-snow/issues"
15
+ },
17
16
  "keywords": [
18
17
  "canvas",
19
18
  "generator",
20
19
  "snow"
21
20
  ],
22
- "author": "cycjimmy <cycjimmy@gmail.com> (https://github.com/cycjimmy)",
23
- "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/cycjimmy/canvas-snow/issues"
21
+ "browser": "dist/canvas-snow.umd.min.js",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/canvas-snow.esm.js",
26
+ "default": "./dist/canvas-snow.esm.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "main": "dist/canvas-snow.esm.js",
31
+ "module": "dist/canvas-snow.esm.js",
32
+ "unpkg": "dist/canvas-snow.umd.min.js",
33
+ "jsdelivr": "dist/canvas-snow.umd.min.js",
34
+ "types": "dist/index.d.ts",
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "engines": {
39
+ "node": "^22.18.0 || >=24.11.0"
26
40
  },
27
- "homepage": "https://github.com/cycjimmy/canvas-snow#readme"
41
+ "publishConfig": {
42
+ "access": "public"
43
+ }
28
44
  }
@@ -1,306 +0,0 @@
1
- 'use strict';
2
-
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);
10
- }
11
- }
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.");
24
- }
25
- return (String )(t);
26
- }
27
- function _toPropertyKey(t) {
28
- var i = _toPrimitive(t, "string");
29
- return "symbol" == typeof i ? i : i + "";
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
- this.x = x;
43
- this.y = y;
44
- this.r = radius;
45
- this.fn = fn;
46
- this.canvasW = canvasW;
47
- this.canvasH = canvasH;
48
- }
49
- return _createClass(_default, [{
50
- key: "update",
51
- value: function update() {
52
- if (this.y > this.canvasH) {
53
- this.x = Math.random() * this.canvasW;
54
- this.y = 0;
55
- } else {
56
- this.x = this.fn.x(this.x, this.y);
57
- this.y = this.fn.y(this.y, this.y);
58
- }
59
- }
60
- }, {
61
- key: "draw",
62
- value: function draw(cxt) {
63
- var insideCxt = cxt;
64
- var grd = insideCxt.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.r);
65
- grd.addColorStop(0, "rgba(255, 255, 255, ".concat(this.r / 6, ")"));
66
- grd.addColorStop(0.5, "rgba(255, 255, 255, ".concat(this.r / 6 * 0.5, ")"));
67
- grd.addColorStop(1, 'rgba(255, 255, 255, 0)');
68
- insideCxt.fillStyle = grd;
69
- insideCxt.fillRect(this.x - this.r, this.y - this.r, this.r * 2, this.r * 2);
70
- }
71
- }]);
72
- }();
73
-
74
- var _default$1 = /*#__PURE__*/function () {
75
- function _default() {
76
- _classCallCheck(this, _default);
77
- this.list = [];
78
- }
79
- return _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
-
111
- /**
112
- * handleDistanceNum
113
- * @param num
114
- * @param totalDistance
115
- * @returns {number|*|number}
116
- */
117
- var handleDistanceNum = function handleDistanceNum(num, totalDistance) {
118
- if (num.toString().indexOf('%') !== -1) {
119
- return num.split('%')[0] / 100 * totalDistance;
120
- }
121
- return isString(num) ? Number(num) : num;
122
- };
123
-
124
- /**
125
- * getTotalDistance
126
- * @param el
127
- * @param tip
128
- * @returns {number|DOMRect}
129
- */
130
- var getTotalDistance = function getTotalDistance(_ref) {
131
- var el = _ref.el,
132
- _ref$tip = _ref.tip,
133
- tip = _ref$tip === void 0 ? 'horizontal' : _ref$tip;
134
- switch (tip) {
135
- case 'horizontal':
136
- return el.getBoundingClientRect().width;
137
- case 'vertical':
138
- return el.getBoundingClientRect().height;
139
- default:
140
- return el.getBoundingClientRect();
141
- }
142
- };
143
-
144
- /**
145
- * getVerticalDistance
146
- * @param el
147
- * @param num
148
- * @returns {number|*}
149
- */
150
- var getVerticalDistance = function getVerticalDistance(el, num) {
151
- return handleDistanceNum(num, getTotalDistance({
152
- el: el,
153
- tip: 'vertical'
154
- }));
155
- };
156
-
157
- /**
158
- * getHorizontalDistance
159
- * @param el
160
- * @param num
161
- * @returns {number|*}
162
- */
163
- var getHorizontalDistance = function getHorizontalDistance(el, num) {
164
- return handleDistanceNum(num, getTotalDistance({
165
- el: el,
166
- tip: 'horizontal'
167
- }));
168
- };
169
-
170
- var _default = /*#__PURE__*/function () {
171
- function _default(_ref) {
172
- var context = _ref.context,
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;
178
- _classCallCheck(this, _default);
179
- this.context = isString(context) ? document.querySelector(context) : context;
180
- this.width = getHorizontalDistance(this.context, width);
181
- this.height = getVerticalDistance(this.context, height);
182
- this.cell = cell;
183
- this.state = 'stop';
184
- this.snowList = null;
185
- this.canvas = null;
186
- this.cxt = null;
187
- this.interval = null;
188
- }
189
- return _createClass(_default, [{
190
- key: "init",
191
- value: function init() {
192
- if (!this.canvas) {
193
- this._initCanvas();
194
- }
195
- if (!this.snowList) {
196
- this.snowList = new _default$1();
197
- this._newSnow();
198
- }
199
- return this;
200
- }
201
- }, {
202
- key: "_initCanvas",
203
- value: function _initCanvas() {
204
- this.canvas = document.createElement('canvas');
205
- this.context.appendChild(this.canvas);
206
- this.canvas.height = this.height;
207
- this.canvas.width = this.width;
208
- this.cxt = this.canvas.getContext('2d');
209
- }
210
-
211
- /**
212
- * animation start
213
- */
214
- }, {
215
- key: "start",
216
- value: function start() {
217
- var _this = this;
218
- if (!this.snowList) {
219
- this.init();
220
- }
221
- this.state = 'play';
222
- this.interval = setInterval(function () {
223
- _this.cxt.clearRect(0, 0, _this.canvas.width, _this.canvas.height);
224
- _this.snowList.update();
225
- _this.snowList.draw(_this.cxt);
226
- }, 13);
227
- }
228
-
229
- /**
230
- * animation stop
231
- */
232
- }, {
233
- key: "stop",
234
- value: function stop() {
235
- this.state = 'stop';
236
- clearInterval(this.interval);
237
- this.interval = null;
238
- this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
239
- }
240
-
241
- /**
242
- * clear
243
- */
244
- }, {
245
- key: "clear",
246
- value: function clear() {
247
- this.stop();
248
- this.snowList = null;
249
- }
250
- }, {
251
- key: "_newSnow",
252
- value: function _newSnow() {
253
- for (var i = 0; i < this.cell; i += 1) {
254
- var randomX = this._getRandom('x');
255
- var randomY = this._getRandom('y');
256
- var randomR = this._getRandom('r');
257
- var randomFnx = this._getRandom('fnx');
258
- var randomFny = this._getRandom('fny');
259
- var snow = new _default$2(randomX, randomY, randomR, {
260
- x: randomFnx,
261
- y: randomFny
262
- }, this.width, this.height);
263
- snow.draw(this.cxt);
264
- this.snowList.push(snow);
265
- }
266
- }
267
-
268
- /**
269
- * Generate random x-pos, y-pos or fn functions
270
- * @param {string} option x|y|fnx|fny
271
- * @return {int|Function}
272
- */
273
- }, {
274
- key: "_getRandom",
275
- value: function _getRandom(option) {
276
- var ret;
277
- var random;
278
- switch (option) {
279
- case 'x':
280
- ret = Math.random() * this.width;
281
- break;
282
- case 'y':
283
- ret = (Math.random() - 1) * this.height;
284
- break;
285
- case 'r':
286
- ret = 2 + Math.random() * 4;
287
- break;
288
- case 'fnx':
289
- random = 27 + Math.random() * 100;
290
- ret = function ret(x, y) {
291
- return x + 0.5 * Math.sin(y / random);
292
- };
293
- break;
294
- case 'fny':
295
- random = 0.4 + Math.random() * 1.4;
296
- ret = function ret(x, y) {
297
- return y + random;
298
- };
299
- break;
300
- }
301
- return ret;
302
- }
303
- }]);
304
- }();
305
-
306
- module.exports = _default;