@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.
@@ -0,0 +1,45 @@
1
+ import SnowList from './SnowList.js';
2
+ export interface CanvasSnowOptions {
3
+ /** The wrapper element, or a selector matching it. */
4
+ context: Element | string;
5
+ /** Canvas width as a number, a numeric string, or a percentage string. */
6
+ width?: number | string;
7
+ /** Canvas height as a number, a numeric string, or a percentage string. */
8
+ height?: number | string;
9
+ /** The number of snowflakes to generate. */
10
+ cell: number;
11
+ }
12
+ export type CanvasSnowState = 'stop' | 'play';
13
+ export default class CanvasSnow {
14
+ context: Element;
15
+ width: number;
16
+ height: number;
17
+ cell: number;
18
+ state: CanvasSnowState;
19
+ snowList: SnowList | null;
20
+ canvas: HTMLCanvasElement | null;
21
+ cxt: CanvasRenderingContext2D | null;
22
+ interval: ReturnType<typeof setInterval> | null;
23
+ constructor({ context, width, height, cell }: CanvasSnowOptions);
24
+ init(): this;
25
+ /**
26
+ * animation start
27
+ */
28
+ start(): void;
29
+ /**
30
+ * animation stop
31
+ */
32
+ stop(): void;
33
+ /**
34
+ * clear
35
+ */
36
+ clear(): void;
37
+ private initCanvas;
38
+ private newSnow;
39
+ /**
40
+ * Generate a random x-position, y-position, radius or motion function.
41
+ * The parameter type is the union of every call site, so the switch is
42
+ * exhaustive and nothing else can be passed in.
43
+ */
44
+ private getRandom;
45
+ }
@@ -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.2",
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,342 +0,0 @@
1
- 'use strict';
2
-
3
- function _classCallCheck(instance, Constructor) {
4
- if (!(instance instanceof Constructor)) {
5
- throw new TypeError("Cannot call a class as a function");
6
- }
7
- }
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);
16
- }
17
- }
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;
26
- }
27
-
28
- /**
29
- * determine a string type
30
- * @param str
31
- * @returns {boolean}
32
- */
33
- var isString = (str => typeof str === 'string' && str.constructor === String);
34
-
35
- var _default$2 = /*#__PURE__*/function () {
36
- function _default(x, y, radius, fn, canvasW, canvasH) {
37
- _classCallCheck(this, _default);
38
-
39
- this.x = x;
40
- this.y = y;
41
- this.r = radius;
42
- this.fn = fn;
43
- this.canvasW = canvasW;
44
- this.canvasH = canvasH;
45
- }
46
-
47
- _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
- return _default;
72
- }();
73
-
74
- var _default$1 = /*#__PURE__*/function () {
75
- function _default() {
76
- _classCallCheck(this, _default);
77
-
78
- this.list = [];
79
- }
80
-
81
- _createClass(_default, [{
82
- key: "update",
83
- value: function update() {
84
- for (var i = 0, len = this.size(); i < len; i += 1) {
85
- this.list[i].update();
86
- }
87
- }
88
- }, {
89
- key: "push",
90
- value: function push(snow) {
91
- this.list.push(snow);
92
- }
93
- }, {
94
- key: "draw",
95
- value: function draw(cxt) {
96
- for (var i = 0, len = this.size(); i < len; i += 1) {
97
- this.list[i].draw(cxt);
98
- }
99
- }
100
- }, {
101
- key: "get",
102
- value: function get(index) {
103
- return this.list[index];
104
- }
105
- }, {
106
- key: "size",
107
- value: function size() {
108
- return this.list.length;
109
- }
110
- }]);
111
-
112
- return _default;
113
- }();
114
-
115
- /**
116
- * handleDistanceNum
117
- * @param num
118
- * @param totalDistance
119
- * @returns {number|*|number}
120
- */
121
-
122
- var handleDistanceNum = function handleDistanceNum(num, totalDistance) {
123
- if (num.toString().indexOf('%') !== -1) {
124
- return num.split('%')[0] / 100 * totalDistance;
125
- }
126
-
127
- return isString(num) ? Number(num) : num;
128
- };
129
- /**
130
- * getTotalDistance
131
- * @param el
132
- * @param tip
133
- * @returns {number|DOMRect}
134
- */
135
-
136
- var getTotalDistance = function getTotalDistance(_ref) {
137
- var el = _ref.el,
138
- _ref$tip = _ref.tip,
139
- tip = _ref$tip === void 0 ? 'horizontal' : _ref$tip;
140
-
141
- switch (tip) {
142
- case 'horizontal':
143
- return el.getBoundingClientRect().width;
144
-
145
- case 'vertical':
146
- return el.getBoundingClientRect().height;
147
-
148
- default:
149
- return el.getBoundingClientRect();
150
- }
151
- };
152
- /**
153
- * getVerticalDistance
154
- * @param el
155
- * @param num
156
- * @returns {number|*}
157
- */
158
-
159
- var getVerticalDistance = function getVerticalDistance(el, num) {
160
- return handleDistanceNum(num, getTotalDistance({
161
- el: el,
162
- tip: 'vertical'
163
- }));
164
- };
165
- /**
166
- * getHorizontalDistance
167
- * @param el
168
- * @param num
169
- * @returns {number|*}
170
- */
171
-
172
- var getHorizontalDistance = function getHorizontalDistance(el, num) {
173
- return handleDistanceNum(num, getTotalDistance({
174
- el: el,
175
- tip: 'horizontal'
176
- }));
177
- };
178
-
179
- var _default = /*#__PURE__*/function () {
180
- function _default(_ref) {
181
- 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
-
188
- _classCallCheck(this, _default);
189
-
190
- this.context = isString(context) ? document.querySelector(context) : context;
191
- this.width = getHorizontalDistance(this.context, width);
192
- this.height = getVerticalDistance(this.context, height);
193
- this.cell = cell;
194
- this.state = 'stop';
195
- this.snowList = null;
196
- this.canvas = null;
197
- this.cxt = null;
198
- this.interval = null;
199
- }
200
-
201
- _createClass(_default, [{
202
- key: "init",
203
- value: function init() {
204
- if (!this.canvas) {
205
- this._initCanvas();
206
- }
207
-
208
- if (!this.snowList) {
209
- this.snowList = new _default$1();
210
-
211
- this._newSnow();
212
- }
213
-
214
- return this;
215
- }
216
- }, {
217
- key: "_initCanvas",
218
- value: function _initCanvas() {
219
- this.canvas = document.createElement('canvas');
220
- this.context.appendChild(this.canvas);
221
- this.canvas.height = this.height;
222
- this.canvas.width = this.width;
223
- this.cxt = this.canvas.getContext('2d');
224
- }
225
- /**
226
- * animation start
227
- */
228
-
229
- }, {
230
- key: "start",
231
- value: function start() {
232
- var _this = this;
233
-
234
- if (!this.snowList) {
235
- this.init();
236
- }
237
-
238
- this.state = 'play';
239
- this.interval = setInterval(function () {
240
- _this.cxt.clearRect(0, 0, _this.canvas.width, _this.canvas.height);
241
-
242
- _this.snowList.update();
243
-
244
- _this.snowList.draw(_this.cxt);
245
- }, 13);
246
- }
247
- /**
248
- * animation stop
249
- */
250
-
251
- }, {
252
- key: "stop",
253
- value: function stop() {
254
- this.state = 'stop';
255
- clearInterval(this.interval);
256
- this.interval = null;
257
- this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
258
- }
259
- /**
260
- * clear
261
- */
262
-
263
- }, {
264
- key: "clear",
265
- value: function clear() {
266
- this.stop();
267
- this.snowList = null;
268
- }
269
- }, {
270
- key: "_newSnow",
271
- value: function _newSnow() {
272
- for (var i = 0; i < this.cell; i += 1) {
273
- var randomX = this._getRandom('x');
274
-
275
- var randomY = this._getRandom('y');
276
-
277
- var randomR = this._getRandom('r');
278
-
279
- var randomFnx = this._getRandom('fnx');
280
-
281
- var randomFny = this._getRandom('fny');
282
-
283
- var snow = new _default$2(randomX, randomY, randomR, {
284
- x: randomFnx,
285
- y: randomFny
286
- }, this.width, this.height);
287
- snow.draw(this.cxt);
288
- this.snowList.push(snow);
289
- }
290
- }
291
- /**
292
- * Generate random x-pos, y-pos or fn functions
293
- * @param {string} option x|y|fnx|fny
294
- * @return {int|Function}
295
- */
296
-
297
- }, {
298
- key: "_getRandom",
299
- value: function _getRandom(option) {
300
- var ret;
301
- var random;
302
-
303
- switch (option) {
304
- case 'x':
305
- ret = Math.random() * this.width;
306
- break;
307
-
308
- case 'y':
309
- ret = (Math.random() - 1) * this.height;
310
- break;
311
-
312
- case 'r':
313
- ret = 2 + Math.random() * 4;
314
- break;
315
-
316
- case 'fnx':
317
- random = 27 + Math.random() * 100;
318
-
319
- ret = function ret(x, y) {
320
- return x + 0.5 * Math.sin(y / random);
321
- };
322
-
323
- break;
324
-
325
- case 'fny':
326
- random = 0.4 + Math.random() * 1.4;
327
-
328
- ret = function ret(x, y) {
329
- return y + random;
330
- };
331
-
332
- break;
333
- }
334
-
335
- return ret;
336
- }
337
- }]);
338
-
339
- return _default;
340
- }();
341
-
342
- module.exports = _default;