Skip to content

Commit 0c84c7b

Browse files
authored
chore(ux): allow to create and drag dots in the animation (#3287)
Make the animation more interactive! Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 73c9b35 commit 0c84c7b

File tree

1 file changed

+85
-14
lines changed

1 file changed

+85
-14
lines changed

core/http/static/p2panimation.js

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
11
const canvas = document.getElementById('networkCanvas');
22
const ctx = canvas.getContext('2d');
33

4-
canvas.width = window.innerWidth;
5-
canvas.height = window.innerHeight;
4+
let particles = [];
5+
let isDragging = false;
6+
let dragParticle = null;
7+
const maxParticles = 100; // Maximum number of particles
8+
const dragAreaRadius = 10; // Increased area for easier dragging
69

7-
const particles = [];
8-
const lines = [];
10+
// Function to resize canvas based on aspect ratio
11+
function resizeCanvas() {
12+
canvas.width = window.innerWidth;
13+
canvas.height = Math.min(window.innerHeight, 400); // Maintain a max height of 400px
14+
}
15+
16+
// Adjust the canvas size when the window resizes
17+
window.addEventListener('resize', resizeCanvas);
18+
19+
// Initialize canvas size
20+
resizeCanvas();
921

1022
class Particle {
1123
constructor(x, y) {
1224
this.x = x;
1325
this.y = y;
14-
this.radius = 2 + Math.random() * 2;
15-
this.color = `rgba(0, 255, 204, ${Math.random()})`;
16-
this.speed = Math.random() * 2 + 1;
17-
this.angle = Math.random() * Math.PI * 2;
26+
this.radius = 4;
27+
this.color = `rgba(0, 255, 204, 1)`;
28+
this.speedX = (Math.random() - 0.5) * 2; // Random horizontal speed
29+
this.speedY = (Math.random() - 0.5) * 2; // Random vertical speed
1830
}
1931

2032
update() {
21-
this.x += Math.cos(this.angle) * this.speed;
22-
this.y += Math.sin(this.angle) * this.speed;
33+
if (!isDragging || dragParticle !== this) {
34+
this.x += this.speedX;
35+
this.y += this.speedY;
2336

24-
if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height) {
25-
this.x = Math.random() * canvas.width;
26-
this.y = Math.random() * canvas.height;
37+
// Bounce off the edges of the canvas
38+
if (this.x < 0 || this.x > canvas.width) {
39+
this.speedX *= -1;
40+
}
41+
if (this.y < 0 || this.y > canvas.height) {
42+
this.speedY *= -1;
43+
}
2744
}
2845
}
2946

@@ -33,6 +50,11 @@ class Particle {
3350
ctx.fillStyle = this.color;
3451
ctx.fill();
3552
}
53+
54+
isMouseOver(mouseX, mouseY) {
55+
// Increase the draggable area by checking if the mouse is within a larger radius
56+
return Math.hypot(mouseX - this.x, mouseY - this.y) < dragAreaRadius;
57+
}
3658
}
3759

3860
function connectParticles() {
@@ -69,5 +91,54 @@ function animate() {
6991
requestAnimationFrame(animate);
7092
}
7193

72-
initParticles(100);
94+
// Handle mouse click to create a new particle
95+
canvas.addEventListener('click', (e) => {
96+
const rect = canvas.getBoundingClientRect();
97+
const mouseX = e.clientX - rect.left;
98+
const mouseY = e.clientY - rect.top;
99+
100+
const newParticle = new Particle(mouseX, mouseY);
101+
particles.push(newParticle);
102+
103+
// Limit the number of particles to the maximum
104+
if (particles.length > maxParticles) {
105+
particles.shift(); // Remove the oldest particle
106+
}
107+
});
108+
109+
// Handle mouse down for dragging
110+
canvas.addEventListener('mousedown', (e) => {
111+
const rect = canvas.getBoundingClientRect();
112+
const mouseX = e.clientX - rect.left;
113+
const mouseY = e.clientY - rect.top;
114+
115+
for (let particle of particles) {
116+
if (particle.isMouseOver(mouseX, mouseY)) {
117+
isDragging = true;
118+
dragParticle = particle;
119+
break;
120+
}
121+
}
122+
});
123+
124+
// Handle mouse move for dragging
125+
canvas.addEventListener('mousemove', (e) => {
126+
if (isDragging && dragParticle) {
127+
const rect = canvas.getBoundingClientRect();
128+
const mouseX = e.clientX - rect.left;
129+
const mouseY = e.clientY - rect.top;
130+
131+
dragParticle.x = mouseX;
132+
dragParticle.y = mouseY;
133+
}
134+
});
135+
136+
// Handle mouse up to stop dragging
137+
canvas.addEventListener('mouseup', () => {
138+
isDragging = false;
139+
dragParticle = null;
140+
});
141+
142+
// Initialize and start the animation
143+
initParticles(maxParticles);
73144
animate();

0 commit comments

Comments
 (0)