Skip to content

Commit 9c346c7

Browse files
committed
2 parents 022a708 + 45ddf03 commit 9c346c7

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

try_canvas.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@
4141
// 2.1 - PARTICLE INITIALIZATION
4242
// Generate random radius within configured range
4343
this.radius = randomInt(config.particleRadiusMin, config.particleRadiusMax);
44-
44+
4545
// Set random position ensuring particle is fully inside canvas bounds
4646
this.x = random(this.radius, canvasWidth - this.radius);
4747
this.y = random(this.radius, canvasHeight - this.radius);
48-
48+
4949
// Set random initial velocity with configured factor
5050
this.vx = random(-0.5, 0.5) * config.particleVelocityFactor;
5151
this.vy = random(-0.5, 0.5) * config.particleVelocityFactor;
52-
52+
5353
// Initialize push forces (for mouse interaction) to zero
5454
this.pushX = 0;
5555
this.pushY = 0;
56-
56+
5757
// Copy friction value from config
5858
this.friction = config.particlePushFriction;
5959
}
@@ -75,20 +75,20 @@
7575
this.x = this.radius; // Reposition to boundary
7676
this.vx *= -1; // Reverse velocity (bounce)
7777
this.pushX *= -0.5; // Reduce and reverse push force
78-
}
78+
}
7979
// Right boundary collision
8080
else if (this.x > canvasWidth - this.radius) {
8181
this.x = canvasWidth - this.radius;
8282
this.vx *= -1;
8383
this.pushX *= -0.5;
8484
}
85-
85+
8686
// Top boundary collision
8787
if (this.y < this.radius) {
8888
this.y = this.radius;
8989
this.vy *= -1;
9090
this.pushY *= -0.5;
91-
}
91+
}
9292
// Bottom boundary collision
9393
else if (this.y > canvasHeight - this.radius) {
9494
this.y = canvasHeight - this.radius;
@@ -123,13 +123,13 @@
123123
console.error("Canvas element not found:", canvasSelector);
124124
return;
125125
}
126-
126+
127127
// Get 2D rendering context
128128
this.ctx = this.canvas.getContext("2d");
129-
129+
130130
// Initialize empty particle array
131131
this.particles = [];
132-
132+
133133
// Initialize canvas dimensions (will be set properly in handleResize)
134134
this.canvasWidth = 0;
135135
this.canvasHeight = 0;
@@ -191,15 +191,15 @@
191191
applyGradientStyle() {
192192
// Create gradient from top-left to bottom-right
193193
const gradient = this.ctx.createLinearGradient(0, 0, this.canvasWidth, this.canvasHeight);
194-
194+
195195
// Calculate step size for evenly distributing colors
196196
const step = 1 / (config.gradientColors.length - 1);
197-
197+
198198
// Add each color at its calculated position
199199
config.gradientColors.forEach((color, index) => {
200200
gradient.addColorStop(Math.min(index * step, 1.0), color); // Ensure stop is <= 1
201201
});
202-
202+
203203
// Apply gradient to both stroke (lines) and fill (particles)
204204
this.ctx.strokeStyle = gradient;
205205
this.ctx.fillStyle = gradient;
@@ -208,13 +208,13 @@
208208
// ====================================================
209209
// STEP 4: PARTICLE MANAGEMENT
210210
// ====================================================
211-
211+
212212
// 4.1 - PARTICLE CREATION
213213
// Creates particles based on screen size
214214
createParticles() {
215215
// Clear existing particles
216216
this.particles = [];
217-
217+
218218
// Calculate particle count based on screen width
219219
const count = this.canvasWidth > config.smallScreenWidthThreshold
220220
? config.particleBaseCount * config.particleCountLargeScreenFactor
@@ -224,7 +224,7 @@
224224
for (let i = 0; i < count; i++) {
225225
this.particles.push(new Particle(this.canvasWidth, this.canvasHeight));
226226
}
227-
227+
228228
// Initialize mouse position off-screen until first mouse movement
229229
this.mouse.x = -config.mouseInteractionRadius * 2;
230230
this.mouse.y = -config.mouseInteractionRadius * 2;
@@ -241,7 +241,7 @@
241241
// Calculate vector from mouse to particle
242242
const dx = particle.x - this.mouse.x;
243243
const dy = particle.y - this.mouse.y;
244-
244+
245245
// Calculate distance using hypot (Pythagoras)
246246
const distance = Math.hypot(dx, dy);
247247

@@ -250,7 +250,7 @@
250250
// Calculate unit vector for force direction
251251
const forceDirectionX = dx / distance;
252252
const forceDirectionY = dy / distance;
253-
253+
254254
// Calculate force magnitude - stronger near mouse, weaker at edge
255255
const forceMagnitude = (1 - distance / this.mouse.radius);
256256

@@ -278,7 +278,7 @@
278278
if (connections >= config.connectionMaxPeers) break;
279279

280280
const p2 = this.particles[j];
281-
281+
282282
// Calculate squared distance between particles
283283
const dx = p1.x - p2.x;
284284
const dy = p1.y - p2.y;
@@ -287,10 +287,10 @@
287287
// If particles are close enough, draw connection
288288
if (distSq < maxDistSq) {
289289
this.ctx.save(); // Save current context state
290-
290+
291291
// Calculate actual distance for opacity
292292
const distance = Math.sqrt(distSq);
293-
293+
294294
// Calculate opacity based on distance - closer = more opaque
295295
const opacity = (1 - (distance / config.connectionMaxDistance)) * config.connectionOpacityFactor;
296296
this.ctx.globalAlpha = Math.max(0, Math.min(opacity, 1)); // Clamp to valid range
@@ -300,7 +300,7 @@
300300
this.ctx.moveTo(p1.x, p1.y);
301301
this.ctx.lineTo(p2.x, p2.y);
302302
this.ctx.stroke();
303-
303+
304304
this.ctx.restore(); // Restore context state
305305
connections++; // Increment connection count
306306
}

0 commit comments

Comments
 (0)