1
1
const canvas = document . getElementById ( 'networkCanvas' ) ;
2
2
const ctx = canvas . getContext ( '2d' ) ;
3
3
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
6
9
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 ( ) ;
9
21
10
22
class Particle {
11
23
constructor ( x , y ) {
12
24
this . x = x ;
13
25
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
18
30
}
19
31
20
32
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 ;
23
36
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
+ }
27
44
}
28
45
}
29
46
@@ -33,6 +50,11 @@ class Particle {
33
50
ctx . fillStyle = this . color ;
34
51
ctx . fill ( ) ;
35
52
}
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
+ }
36
58
}
37
59
38
60
function connectParticles ( ) {
@@ -69,5 +91,54 @@ function animate() {
69
91
requestAnimationFrame ( animate ) ;
70
92
}
71
93
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 ) ;
73
144
animate ( ) ;
0 commit comments