@cycjimmy/canvas-snow 1.0.5 → 3.0.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 cycjimmy
3
+ Copyright (c) 2018-2022 cycjimmy
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,57 +1,47 @@
1
1
  # Canvas Snow
2
2
 
3
- [![NPM version][npm-image]][npm-url]
4
- [![build status][travis-image]][travis-url]
5
- [![David deps][david-image]][david-url]
6
- [![devDependencies Status][david-dev-image]][david-dev-url]
7
- [![npm download][download-image]][download-url]
8
- [![jsdelivr][jsdelivr-image]][jsdelivr-url]
3
+ ![][workflows-badge-image]
4
+ [![libraries dependency status][libraries-status-image]][libraries-status-url]
5
+ [![libraries sourcerank][libraries-sourcerank-image]][libraries-sourcerank-url]
6
+ [![Coverage Status][coverage-image]][coverage-url]
7
+ [![Release date][release-date-image]][release-url]
8
+ [![rollup][rollup-image]][rollup-url]
9
+ [![semantic-release][semantic-image]][semantic-url]
10
+ [![jest][jest-image]][jest-url]
9
11
  [![npm license][license-image]][download-url]
10
12
 
11
- [npm-image]: https://img.shields.io/npm/v/@cycjimmy/canvas-snow.svg?style=flat-square
12
- [npm-url]: https://npmjs.org/package/@cycjimmy/canvas-snow
13
- [travis-image]: https://img.shields.io/travis/cycjimmy/canvas-snow.svg?style=flat-square
14
- [travis-url]: https://travis-ci.org/cycjimmy/canvas-snow
15
- [david-image]: https://img.shields.io/david/cycjimmy/canvas-snow.svg?style=flat-square
16
- [david-url]: https://david-dm.org/cycjimmy/canvas-snow
17
- [david-dev-image]: https://david-dm.org/cycjimmy/canvas-snow/dev-status.svg?style=flat-square
18
- [david-dev-url]: https://david-dm.org/cycjimmy/canvas-snow?type=dev
19
- [download-image]: https://img.shields.io/npm/dm/@cycjimmy/canvas-snow.svg?style=flat-square
20
- [download-url]: https://npmjs.org/package/@cycjimmy/canvas-snow
21
- [jsdelivr-image]: https://data.jsdelivr.com/v1/package/npm/@cycjimmy/canvas-snow/badge
22
- [jsdelivr-url]: https://www.jsdelivr.com/package/npm/@cycjimmy/canvas-snow
23
- [license-image]: https://img.shields.io/npm/l/@cycjimmy/canvas-snow.svg?style=flat-square
24
-
25
- Generates snow in canvas. ([Releases](https://github.com/cycjimmy/canvas-snow/releases) | [Demo](https://cycjimmy.github.io/canvas-snow/))
13
+ * Generates snow in canvas. ([Demo][github-pages-url])
26
14
 
27
15
  ## Install
16
+ [![NPM version][npm-image]][npm-url]
17
+ [![NPM bundle size][npm-bundle-size-image]][npm-url]
18
+ [![npm download][download-image]][download-url]
19
+
28
20
  ```shell
21
+ # via npm
29
22
  $ npm install @cycjimmy/canvas-snow --save
30
- # or
23
+
24
+ # or via yarn
31
25
  $ yarn add @cycjimmy/canvas-snow
32
26
  ```
33
27
 
34
- ## Use
35
- ```javascript
36
- import CanvasSnow from '@cycjimmy/canvas-snow';
37
- # OR
38
- const CanvasSnow = require('@cycjimmy/canvas-snow');
39
- ```
28
+ ## Usage
29
+ ```javascript
30
+ import CanvasSnow from '@cycjimmy/canvas-snow';
40
31
 
41
- ```javascript
42
- const canvasSnow = new CanvasSnow({
43
- [...options]
44
- }).init();
45
- canvasSnow.start();
46
- ```
32
+ const canvasSnow = new CanvasSnow({
33
+ ...options
34
+ }).init();
35
+ canvasSnow.start();
36
+ ```
47
37
 
48
- * options
38
+ * `options`
49
39
  * `context`: [Element|String] Context Wrapper Element.
50
40
  * `cell`: [Number] The maximum number of snow in the canvas.
51
41
  * `width`: [Number|String] Set canvas width with percentage and px units. Default `'100%'`.
52
42
  * `height`: [Number|String] Set canvas height with percentage and px units. Default `'100%'`.
53
43
 
54
- * function
44
+ * Methods:
55
45
  * `init()`: Initialize to generate random snow.
56
46
  * `start()`: Start snow animation.
57
47
  * `stop()`: Stop snow animation.
@@ -60,9 +50,9 @@ $ yarn add @cycjimmy/canvas-snow
60
50
  ### Use in browser
61
51
  ```html
62
52
  <div id="snowWrapper"></div>
63
- <script src="canvas-snow.min.js"></script>
53
+ <script src="canvas-snow.umd.min.js"></script>
64
54
  <script>
65
- var canvasSnow = new CanvasSnow(source, {
55
+ const canvasSnow = new CanvasSnow(source, {
66
56
  context: '#snowWrapper',
67
57
  [...options]
68
58
  }).init();
@@ -72,8 +62,46 @@ $ yarn add @cycjimmy/canvas-snow
72
62
  ```
73
63
 
74
64
  ## CDN
75
- To use via a CDN include this in your html:
65
+ [![jsdelivr][jsdelivr-image]][jsdelivr-url]
66
+
67
+ To use via a CDN include this in your HTML:
76
68
  ```text
77
- <script src="https://cdn.jsdelivr.net/npm/@cycjimmy/canvas-snow@1/build/canvas-snow.min.js"></script>
69
+ <script src="https://cdn.jsdelivr.net/npm/@cycjimmy/canvas-snow@3/dist/canvas-snow.umd.min.js"></script>
78
70
  ```
79
71
 
72
+ <!-- Links: -->
73
+ [npm-image]: https://img.shields.io/npm/v/@cycjimmy/canvas-snow
74
+ [npm-url]: https://npmjs.org/package/@cycjimmy/canvas-snow
75
+ [npm-bundle-size-image]: https://img.shields.io/bundlephobia/min/@cycjimmy/canvas-snow
76
+
77
+ [download-image]: https://img.shields.io/npm/dt/@cycjimmy/canvas-snow
78
+ [download-url]: https://npmjs.org/package/@cycjimmy/canvas-snow
79
+
80
+ [jsdelivr-image]: https://img.shields.io/jsdelivr/npm/hy/@cycjimmy/canvas-snow
81
+ [jsdelivr-url]: https://www.jsdelivr.com/package/npm/@cycjimmy/canvas-snow
82
+
83
+ [workflows-badge-image]: https://github.com/cycjimmy/canvas-snow/workflows/Test%20CI/badge.svg
84
+
85
+ [libraries-status-image]: https://img.shields.io/librariesio/release/npm/@cycjimmy/canvas-snow
86
+ [libraries-sourcerank-image]: https://img.shields.io/librariesio/sourcerank/npm/@cycjimmy/canvas-snow
87
+ [libraries-status-url]: https://libraries.io/github/cycjimmy/canvas-snow
88
+ [libraries-sourcerank-url]: https://libraries.io/npm/@cycjimmy%2Fcanvas-snow
89
+
90
+ [coverage-image]: https://img.shields.io/coveralls/github/cycjimmy/canvas-snow
91
+ [coverage-url]: https://coveralls.io/github/cycjimmy/canvas-snow
92
+
93
+ [release-date-image]: https://img.shields.io/github/release-date/cycjimmy/canvas-snow
94
+ [release-url]: https://github.com/cycjimmy/canvas-snow/releases
95
+
96
+ [rollup-image]: https://img.shields.io/github/package-json/dependency-version/cycjimmy/canvas-snow/dev/rollup
97
+ [rollup-url]: https://github.com/rollup/rollup
98
+
99
+ [semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
100
+ [semantic-url]: https://github.com/semantic-release/semantic-release
101
+
102
+ [jest-image]: https://img.shields.io/badge/tested_with-jest-99424f.svg
103
+ [jest-url]: https://github.com/facebook/jest
104
+
105
+ [license-image]: https://img.shields.io/npm/l/@cycjimmy/canvas-snow
106
+
107
+ [github-pages-url]: https://cycjimmy.github.io/canvas-snow/
@@ -0,0 +1,342 @@
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;