canvasparticles-js 3.3.7 → 3.3.8

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.
@@ -2,7 +2,7 @@
2
2
  // https://github.com/Khoeckman/canvasParticles/blob/main/LICENSE
3
3
 
4
4
  class CanvasParticles {
5
- static version = '3.3.7'
5
+ static version = '3.3.8'
6
6
 
7
7
  animating = false
8
8
  particles = []
@@ -171,10 +171,10 @@ class CanvasParticles {
171
171
  }
172
172
 
173
173
  /**
174
- * Calculates the properties of each particle on the next frame.
174
+ * Calculates the gravity properties of each particle on the next frame.
175
175
  * Is executed once every 'options.framesPerUpdate' frames.
176
176
  * */
177
- update = () => {
177
+ updateGravity = () => {
178
178
  const len = this.particleCount
179
179
  const enabledRepulsive = this.options.gravity.repulsive !== 0
180
180
  const enabledPulling = this.options.gravity.pulling !== 0
@@ -187,11 +187,17 @@ class CanvasParticles {
187
187
  // Code in this scope runs [particles ** 2 / 2] times!
188
188
  const particleA = this.particles[i]
189
189
  const particleB = this.particles[j]
190
- const dist = Math.hypot(particleA.posX - particleB.posX, particleA.posY - particleB.posY)
191
- const angle = Math.atan2(particleB.posY - particleA.posY, particleB.posX - particleA.posX)
190
+
191
+ const distX = particleA.posX - particleB.posX
192
+ const distY = particleA.posY - particleB.posY
193
+
194
+ const dist = Math.sqrt(distX * distX + distY * distY)
195
+
196
+ // const dist = Math.hypot(particleA.posX - particleB.posX, particleA.posY - particleB.posY)
192
197
 
193
198
  if (dist < this.options.particles.connectDist / 2) {
194
199
  // Apply repulsive force on all particles close together
200
+ const angle = Math.atan2(particleB.posY - particleA.posY, particleB.posX - particleA.posX)
195
201
  const grav = (1 / Math.max(dist, 10)) ** 1.8 * gravRepulsiveMult
196
202
  const gravX = Math.cos(angle) * grav
197
203
  const gravY = Math.sin(angle) * grav
@@ -201,6 +207,7 @@ class CanvasParticles {
201
207
  particleB.velY += gravY
202
208
  } else if (enabledPulling) {
203
209
  // Apply pulling force on all particles not close together
210
+ const angle = Math.atan2(particleB.posY - particleA.posY, particleB.posX - particleA.posX)
204
211
  const grav = (1 / Math.max(dist, 10)) ** 1.8 * gravPullingMult
205
212
  const gravX = Math.cos(angle) * grav
206
213
  const gravY = Math.sin(angle) * grav
@@ -212,7 +219,13 @@ class CanvasParticles {
212
219
  }
213
220
  }
214
221
  }
222
+ }
215
223
 
224
+ /**
225
+ * Calculates the properties of each particle on the next frame.
226
+ * Is executed once every 'options.framesPerUpdate' frames.
227
+ * */
228
+ updateParticles = () => {
216
229
  for (let particle of this.particles) {
217
230
  // Moving the particle
218
231
  particle.dir = (particle.dir + Math.random() * this.options.particles.rotationSpeed * 2 - this.options.particles.rotationSpeed) % (2 * Math.PI)
@@ -324,15 +337,7 @@ class CanvasParticles {
324
337
  return table
325
338
  }
326
339
 
327
- /**
328
- * Renders the particles and their connections onto the canvas.
329
- * Connects particles with lines if they are within the connection distance.
330
- */
331
- render = () => {
332
- this.canvas.width = this.canvas.width
333
- this.ctx.fillStyle = this.options.particles.colorWithAlpha
334
- this.ctx.lineWidth = 1
335
-
340
+ renderParticles = () => {
336
341
  for (let particle of this.particles) {
337
342
  if (particle.isVisible) {
338
343
  // Draw the particle as a square if the size is smaller than 1 pixel (±183% faster than drawing only circles, using default settings)
@@ -348,7 +353,12 @@ class CanvasParticles {
348
353
  }
349
354
  }
350
355
  }
356
+ }
351
357
 
358
+ /**
359
+ * Connects particles with lines if they are within the connection distance.
360
+ */
361
+ renderConnections = () => {
352
362
  const len = this.particleCount
353
363
  const drawAll = this.options.particles.connectDist >= Math.min(this.canvas.width, this.canvas.height)
354
364
 
@@ -395,6 +405,18 @@ class CanvasParticles {
395
405
  }
396
406
  }
397
407
 
408
+ /**
409
+ * Clear the canvas and render the particles and their connections onto the canvas.
410
+ */
411
+ render = () => {
412
+ this.canvas.width = this.canvas.width
413
+ this.ctx.fillStyle = this.options.particles.colorWithAlpha
414
+ this.ctx.lineWidth = 1
415
+
416
+ this.renderParticles()
417
+ this.renderConnections()
418
+ }
419
+
398
420
  /**
399
421
  * Main animation loop that updates and renders the particles.
400
422
  * Runs recursively using `requestAnimationFrame`.
@@ -406,7 +428,8 @@ class CanvasParticles {
406
428
 
407
429
  if (++this.updateCount >= this.options.framesPerUpdate) {
408
430
  this.updateCount = 0
409
- this.update()
431
+ this.updateGravity()
432
+ this.updateParticles()
410
433
  this.render()
411
434
  }
412
435
  }
@@ -2,7 +2,7 @@
2
2
  // https://github.com/Khoeckman/canvasParticles/blob/main/LICENSE
3
3
 
4
4
  export default class CanvasParticles {
5
- static version = '3.3.7'
5
+ static version = '3.3.8'
6
6
 
7
7
  animating = false
8
8
  particles = []
@@ -171,10 +171,10 @@ export default class CanvasParticles {
171
171
  }
172
172
 
173
173
  /**
174
- * Calculates the properties of each particle on the next frame.
174
+ * Calculates the gravity properties of each particle on the next frame.
175
175
  * Is executed once every 'options.framesPerUpdate' frames.
176
176
  * */
177
- update = () => {
177
+ updateGravity = () => {
178
178
  const len = this.particleCount
179
179
  const enabledRepulsive = this.options.gravity.repulsive !== 0
180
180
  const enabledPulling = this.options.gravity.pulling !== 0
@@ -187,11 +187,17 @@ export default class CanvasParticles {
187
187
  // Code in this scope runs [particles ** 2 / 2] times!
188
188
  const particleA = this.particles[i]
189
189
  const particleB = this.particles[j]
190
- const dist = Math.hypot(particleA.posX - particleB.posX, particleA.posY - particleB.posY)
191
- const angle = Math.atan2(particleB.posY - particleA.posY, particleB.posX - particleA.posX)
190
+
191
+ const distX = particleA.posX - particleB.posX
192
+ const distY = particleA.posY - particleB.posY
193
+
194
+ const dist = Math.sqrt(distX * distX + distY * distY)
195
+
196
+ // const dist = Math.hypot(particleA.posX - particleB.posX, particleA.posY - particleB.posY)
192
197
 
193
198
  if (dist < this.options.particles.connectDist / 2) {
194
199
  // Apply repulsive force on all particles close together
200
+ const angle = Math.atan2(particleB.posY - particleA.posY, particleB.posX - particleA.posX)
195
201
  const grav = (1 / Math.max(dist, 10)) ** 1.8 * gravRepulsiveMult
196
202
  const gravX = Math.cos(angle) * grav
197
203
  const gravY = Math.sin(angle) * grav
@@ -201,6 +207,7 @@ export default class CanvasParticles {
201
207
  particleB.velY += gravY
202
208
  } else if (enabledPulling) {
203
209
  // Apply pulling force on all particles not close together
210
+ const angle = Math.atan2(particleB.posY - particleA.posY, particleB.posX - particleA.posX)
204
211
  const grav = (1 / Math.max(dist, 10)) ** 1.8 * gravPullingMult
205
212
  const gravX = Math.cos(angle) * grav
206
213
  const gravY = Math.sin(angle) * grav
@@ -212,7 +219,13 @@ export default class CanvasParticles {
212
219
  }
213
220
  }
214
221
  }
222
+ }
215
223
 
224
+ /**
225
+ * Calculates the properties of each particle on the next frame.
226
+ * Is executed once every 'options.framesPerUpdate' frames.
227
+ * */
228
+ updateParticles = () => {
216
229
  for (let particle of this.particles) {
217
230
  // Moving the particle
218
231
  particle.dir = (particle.dir + Math.random() * this.options.particles.rotationSpeed * 2 - this.options.particles.rotationSpeed) % (2 * Math.PI)
@@ -324,15 +337,7 @@ export default class CanvasParticles {
324
337
  return table
325
338
  }
326
339
 
327
- /**
328
- * Renders the particles and their connections onto the canvas.
329
- * Connects particles with lines if they are within the connection distance.
330
- */
331
- render = () => {
332
- this.canvas.width = this.canvas.width
333
- this.ctx.fillStyle = this.options.particles.colorWithAlpha
334
- this.ctx.lineWidth = 1
335
-
340
+ renderParticles = () => {
336
341
  for (let particle of this.particles) {
337
342
  if (particle.isVisible) {
338
343
  // Draw the particle as a square if the size is smaller than 1 pixel (±183% faster than drawing only circles, using default settings)
@@ -348,7 +353,12 @@ export default class CanvasParticles {
348
353
  }
349
354
  }
350
355
  }
356
+ }
351
357
 
358
+ /**
359
+ * Connects particles with lines if they are within the connection distance.
360
+ */
361
+ renderConnections = () => {
352
362
  const len = this.particleCount
353
363
  const drawAll = this.options.particles.connectDist >= Math.min(this.canvas.width, this.canvas.height)
354
364
 
@@ -395,6 +405,18 @@ export default class CanvasParticles {
395
405
  }
396
406
  }
397
407
 
408
+ /**
409
+ * Clear the canvas and render the particles and their connections onto the canvas.
410
+ */
411
+ render = () => {
412
+ this.canvas.width = this.canvas.width
413
+ this.ctx.fillStyle = this.options.particles.colorWithAlpha
414
+ this.ctx.lineWidth = 1
415
+
416
+ this.renderParticles()
417
+ this.renderConnections()
418
+ }
419
+
398
420
  /**
399
421
  * Main animation loop that updates and renders the particles.
400
422
  * Runs recursively using `requestAnimationFrame`.
@@ -406,7 +428,8 @@ export default class CanvasParticles {
406
428
 
407
429
  if (++this.updateCount >= this.options.framesPerUpdate) {
408
430
  this.updateCount = 0
409
- this.update()
431
+ this.updateGravity()
432
+ this.updateParticles()
410
433
  this.render()
411
434
  }
412
435
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvasparticles-js",
3
- "version": "3.3.7",
3
+ "version": "3.3.8",
4
4
  "description": "In an HTML canvas, a bunch of interactive particles connected with lines when they approach eachother.",
5
5
  "main": "canvasParticles.js",
6
6
  "type": "module",