1- export default function background ( canvas ) {
2- class Particle {
3- constructor ( radius , x , y , dx , dy , color ) {
4- this . radius = radius ;
5- this . x = x ;
6- this . y = y ;
7- this . dx = dx ;
8- this . dy = dy ;
9- this . color = color ;
10- }
11-
12- draw ( ctx ) {
13- ctx . beginPath ( ) ;
14- ctx . arc ( this . x , this . y , this . radius , 0 , 2 * Math . PI ) ;
15- ctx . fillStyle = this . color ;
16- ctx . fill ( ) ;
17- }
1+ class Particle {
2+ /** * @param {HTMLCanvasElement } canvas
3+ * @param {HTMLCanvasElement } canvas
4+ * @param {number } radius
5+ * @param {number } x
6+ * @param {number } y
7+ * @param {number } dx
8+ * @param {number } dy
9+ * @param {string } color
10+ */
11+ constructor ( canvas , radius , x , y , dx , dy , color ) {
12+ this . radius = radius ;
13+ this . x = x ;
14+ this . y = y ;
15+ this . dx = dx ;
16+ this . dy = dy ;
17+ this . color = color ;
18+ this . canvas = canvas ;
19+ }
1820
19- update ( ) {
20- if ( this . x < this . radius || this . x > canvas . width - this . radius ) {
21- this . dx *= - 1 ;
22- }
21+ draw ( ctx ) {
22+ ctx . beginPath ( ) ;
23+ ctx . arc ( this . x , this . y , this . radius , 0 , 2 * Math . PI ) ;
24+ ctx . fillStyle = this . color ;
25+ ctx . fill ( ) ;
26+ }
2327
24- if ( this . y < this . radius || this . y > canvas . height - this . radius ) {
25- this . dy *= - 1 ;
26- }
28+ update ( ) {
29+ if ( this . x < this . radius || this . x > this . canvas . width - this . radius ) {
30+ this . dx *= - 1 ;
31+ }
2732
28- this . x += this . dx ;
29- this . y += this . dy ;
33+ if ( this . y < this . radius || this . y > this . canvas . height - this . radius ) {
34+ this . dy *= - 1 ;
3035 }
36+
37+ this . x += this . dx ;
38+ this . y += this . dy ;
3139 }
40+ }
3241
42+ /**
43+ * Initializes the background animation with particles and connections.
44+ * @param {HTMLCanvasElement } canvas
45+ */
46+ export default function background ( canvas ) {
3347 canvas . width = window . innerWidth * 2 ;
3448 canvas . height = window . innerHeight * 2 ;
3549
3650 const ctx = canvas . getContext ( '2d' ) ;
3751
3852 let particles = null ;
3953 let reqId = null ;
54+ let wasConnected = false ;
4055
4156 init ( ) ;
4257
@@ -59,7 +74,7 @@ export default function background(canvas) {
5974 const dx = ( Math . random ( ) > 0.5 ? 1 : - 1 ) * Math . random ( ) * 1.5 ;
6075 const dy = ( Math . random ( ) > 0.5 ? 1 : - 1 ) * Math . random ( ) * 1.5 ;
6176
62- particles . push ( new Particle ( r , x , y , dx , dy , '#606060' ) ) ;
77+ particles . push ( new Particle ( canvas , r , x , y , dx , dy , '#606060' ) ) ;
6378 }
6479
6580 if ( reqId != null ) cancelAnimationFrame ( reqId ) ;
@@ -68,6 +83,13 @@ export default function background(canvas) {
6883 }
6984
7085 function animate ( ) {
86+ if ( wasConnected && ! canvas . isConnected ) {
87+ particles = null ;
88+ cancelAnimationFrame ( reqId ) ;
89+ return ;
90+ }
91+
92+ wasConnected = canvas . isConnected ;
7193 ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
7294
7395 for ( const p of particles ) {
0 commit comments