canvasparticles-js 3.2.10 → 3.2.12

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  <span class="badge-npmversion"><a href="https://npmjs.org/package/canvasparticles-js" title="View this project on NPM"><img src="https://img.shields.io/npm/v/canvasparticles-js.svg" alt="NPM version" /></a></span>
4
4
  <span class="badge-npmdownloads"><a href="https://npmjs.org/package/canvasparticles-js" title="View this project on NPM"><img src="https://img.shields.io/npm/dm/canvasparticles-js.svg" alt="NPM downloads" /></a></span>
5
- [![](https://data.jsdelivr.com/v1/package/npm/canvasparticles-js/badge?style=rounded)](https://www.jsdelivr.com/package/npm/canvasparticles-js)
5
+ <span><a href="https://www.jsdelivr.com/package/npm/canvasparticles-js" title="View this project on jsDelivr"><img src="https://data.jsdelivr.com/v1/package/npm/canvasparticles-js/badge?style=rounded" alt="jsDelivr hits" /></a></span>
6
6
 
7
7
  ## Description
8
8
 
@@ -133,19 +133,23 @@ particles.stop()
133
133
  const particles = new CanvasParticles(selector, options)
134
134
  particles.options.particles.color = 'blue'
135
135
 
136
- // Required usage of setter for options.background
136
+ // Required usage of setter for options.background and options.particles.color
137
137
  particles.setBackground('red')
138
+ particles.setParticleColor('hsl(149, 100%, 50%)')
138
139
 
139
- // Changing options.particles.ppm or options.particles.max requires a reset or resize
140
+ // Changing options.particles.ppm or options.particles.max requires a reset
140
141
  particles.options.particles.ppm = 100
141
142
  particles.options.particles.max = 300
142
- particles.newParticles() // reset
143
+ particles.newParticles() // required reset
144
+
145
+ // All other options work on the fly
146
+ particles.options.gravity.repulsive = 1
143
147
  ```
144
148
 
145
149
  ## Options
146
150
 
147
151
  Configuration options for the particles and their behavior.<br>
148
- Play around with these values: [Playground](http://kylehoeckman.great-site.net/canvas-particles/#playground)
152
+ Play around with these values: [Sandbox](http://kylehoeckman.great-site.net/canvas-particles/#sandbox)
149
153
 
150
154
  <details>
151
155
  <summary><h3>Options structure</h3></summary>
@@ -5,10 +5,10 @@
5
5
  * Canvas Particles JS
6
6
  *
7
7
  * @class CanvasParticles
8
- * @version 3.2.10
8
+ * @version 3.2.12
9
9
  */
10
10
  class CanvasParticles {
11
- static version = '3.2.10'
11
+ static version = '3.2.12'
12
12
  animating = false
13
13
 
14
14
  /**
@@ -73,30 +73,12 @@ class CanvasParticles {
73
73
  if (isNaN(this.options.gravity.friction)) this.options.gravity.friction = 0.9
74
74
 
75
75
  this.setBackground(this.options.background)
76
+ this.setParticleColor(this.options.particles.color)
76
77
 
77
78
  // Transform distance multiplier to absolute distance
78
79
  this.options.mouse.connectDist = this.options.particles.connectDist * this.options.mouse.connectDistMult
79
80
  delete this.options.mouse.connectDistMult
80
81
 
81
- // Format particle color and opacity
82
- this.ctx.fillStyle = this.options.particles.color
83
-
84
- if (this.ctx.fillStyle[0] === '#') {
85
- this.options.particles.opacity = { value: 255, hex: 'ff' }
86
- this.options.particles.color = this.ctx.fillStyle
87
- } else {
88
- // Example: extract 0.25 from rgba(136, 244, 255, 0.25) and convert to range 0x00 to 0xff and store as a 2 char string
89
- const value = ~~(this.ctx.fillStyle.split(',').at(-1).slice(1, -1) * 255)
90
- this.options.particles.opacity = {
91
- value: value,
92
- hex: value.toString(16),
93
- }
94
-
95
- // Example: extract 136, 244 and 255 from rgba(136, 244, 255, 0.25) and convert to '#001122' format
96
- this.ctx.fillStyle = this.ctx.fillStyle.split(',').slice(0, -1).join(',') + ', 1)'
97
- this.options.particles.color = this.ctx.fillStyle
98
- }
99
-
100
82
  // Event handling
101
83
  window.addEventListener('resize', this.resizeCanvas)
102
84
  this.resizeCanvas()
@@ -411,10 +393,35 @@ class CanvasParticles {
411
393
  setBackground = background => {
412
394
  if (typeof background === 'string') this.canvas.style.background = this.options.background = background
413
395
  }
396
+
397
+ /**
398
+ * Format particle color and opacity
399
+ * @param {string} color - The color of the particles and their connections. Can be any CSS supported color format.
400
+ */
401
+ setParticleColor = color => {
402
+ this.ctx.fillStyle = color
403
+
404
+ if (this.ctx.fillStyle[0] === '#') {
405
+ this.options.particles.opacity = { value: 255, hex: 'ff' }
406
+ this.options.particles.color = this.ctx.fillStyle
407
+ } else {
408
+ // Example: extract 0.25 from rgba(136, 244, 255, 0.25) and convert to range 0x00 to 0xff and store as a 2 char string
409
+ const value = ~~(this.ctx.fillStyle.split(',').at(-1).slice(1, -1) * 255)
410
+ this.options.particles.opacity = {
411
+ value: value,
412
+ hex: value.toString(16),
413
+ }
414
+
415
+ // Example: extract 136, 244 and 255 from rgba(136, 244, 255, 0.25) and convert to '#001122' format
416
+ this.ctx.fillStyle = this.ctx.fillStyle.split(',').slice(0, -1).join(',') + ', 1)'
417
+ this.options.particles.color = this.ctx.fillStyle
418
+ }
419
+ }
414
420
  }
415
421
 
416
- if (typeof module !== undefined) {
417
- try {
418
- module.exports = CanvasParticles
419
- } catch (err) {}
420
- } else globalThis.CanvasParticles = CanvasParticles
422
+ try {
423
+ if (typeof module !== undefined) module.exports = CanvasParticles
424
+ else globalThis.CanvasParticles = CanvasParticles
425
+ } catch (err) {
426
+ console.log('Error while exporting canvasparticles-js:', err)
427
+ }
@@ -5,10 +5,10 @@
5
5
  * Canvas Particles JS
6
6
  *
7
7
  * @module CanvasParticles
8
- * @version 3.2.10
8
+ * @version 3.2.12
9
9
  */
10
10
  export default class CanvasParticles {
11
- static version = '3.2.10'
11
+ static version = '3.2.12'
12
12
  animating = false
13
13
 
14
14
  /**
@@ -73,30 +73,12 @@ export default class CanvasParticles {
73
73
  if (isNaN(this.options.gravity.friction)) this.options.gravity.friction = 0.9
74
74
 
75
75
  this.setBackground(this.options.background)
76
+ this.setParticleColor(this.options.particles.color)
76
77
 
77
78
  // Transform distance multiplier to absolute distance
78
79
  this.options.mouse.connectDist = this.options.particles.connectDist * this.options.mouse.connectDistMult
79
80
  delete this.options.mouse.connectDistMult
80
81
 
81
- // Format particle color and opacity
82
- this.ctx.fillStyle = this.options.particles.color
83
-
84
- if (this.ctx.fillStyle[0] === '#') {
85
- this.options.particles.opacity = { value: 255, hex: 'ff' }
86
- this.options.particles.color = this.ctx.fillStyle
87
- } else {
88
- // Example: extract 0.25 from rgba(136, 244, 255, 0.25) and convert to range 0x00 to 0xff and store as a 2 char string
89
- const value = ~~(this.ctx.fillStyle.split(',').at(-1).slice(1, -1) * 255)
90
- this.options.particles.opacity = {
91
- value: value,
92
- hex: value.toString(16),
93
- }
94
-
95
- // Example: extract 136, 244 and 255 from rgba(136, 244, 255, 0.25) and convert to '#001122' format
96
- this.ctx.fillStyle = this.ctx.fillStyle.split(',').slice(0, -1).join(',') + ', 1)'
97
- this.options.particles.color = this.ctx.fillStyle
98
- }
99
-
100
82
  // Event handling
101
83
  window.addEventListener('resize', this.resizeCanvas)
102
84
  this.resizeCanvas()
@@ -411,4 +393,28 @@ export default class CanvasParticles {
411
393
  setBackground = background => {
412
394
  if (typeof background === 'string') this.canvas.style.background = this.options.background = background
413
395
  }
396
+
397
+ /**
398
+ * Format particle color and opacity
399
+ * @param {string} color - The color of the particles and their connections. Can be any CSS supported color format.
400
+ */
401
+ setParticleColor = color => {
402
+ this.ctx.fillStyle = color
403
+
404
+ if (this.ctx.fillStyle[0] === '#') {
405
+ this.options.particles.opacity = { value: 255, hex: 'ff' }
406
+ this.options.particles.color = this.ctx.fillStyle
407
+ } else {
408
+ // Example: extract 0.25 from rgba(136, 244, 255, 0.25) and convert to range 0x00 to 0xff and store as a 2 char string
409
+ const value = ~~(this.ctx.fillStyle.split(',').at(-1).slice(1, -1) * 255)
410
+ this.options.particles.opacity = {
411
+ value: value,
412
+ hex: value.toString(16),
413
+ }
414
+
415
+ // Example: extract 136, 244 and 255 from rgba(136, 244, 255, 0.25) and convert to '#001122' format
416
+ this.ctx.fillStyle = this.ctx.fillStyle.split(',').slice(0, -1).join(',') + ', 1)'
417
+ this.options.particles.color = this.ctx.fillStyle
418
+ }
419
+ }
414
420
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvasparticles-js",
3
- "version": "3.2.10",
3
+ "version": "3.2.12",
4
4
  "description": "In an HTML canvas, a bunch of floating particles connected with lines when they approach eachother. Creating a fun and interactive background.",
5
5
  "main": "canvasParticles.js",
6
6
  "type": "module",