11// set up canvas
22
3- const canvas = document . querySelector ( ' canvas' ) ;
4- const ctx = canvas . getContext ( '2d' ) ;
3+ const canvas = document . querySelector ( " canvas" ) ;
4+ const ctx = canvas . getContext ( "2d" ) ;
55
6- const width = canvas . width = window . innerWidth ;
7- const height = canvas . height = window . innerHeight ;
6+ const width = ( canvas . width = window . innerWidth ) ;
7+ const height = ( canvas . height = window . innerHeight ) ;
88
99// function to generate random number
1010
@@ -19,89 +19,87 @@ function randomRGB() {
1919}
2020
2121class Ball {
22-
23- constructor ( x , y , velX , velY , color , size ) {
24- this . x = x ;
25- this . y = y ;
26- this . velX = velX ;
27- this . velY = velY ;
28- this . color = color ;
29- this . size = size ;
30- }
31-
32- draw ( ) {
33- ctx . beginPath ( ) ;
34- ctx . fillStyle = this . color ;
35- ctx . arc ( this . x , this . y , this . size , 0 , 2 * Math . PI ) ;
36- ctx . fill ( ) ;
37- }
38-
39- update ( ) {
40- if ( ( this . x + this . size ) >= width ) {
41- this . velX = - ( Math . abs ( this . velX ) ) ;
42- }
43-
44- if ( ( this . x - this . size ) <= 0 ) {
45- this . velX = Math . abs ( this . velX ) ;
46- }
47-
48- if ( ( this . y + this . size ) >= height ) {
49- this . velY = - ( Math . abs ( this . velY ) ) ;
22+ constructor ( x , y , velX , velY , color , size ) {
23+ this . x = x ;
24+ this . y = y ;
25+ this . velX = velX ;
26+ this . velY = velY ;
27+ this . color = color ;
28+ this . size = size ;
29+ }
30+
31+ draw ( ) {
32+ ctx . beginPath ( ) ;
33+ ctx . fillStyle = this . color ;
34+ ctx . arc ( this . x , this . y , this . size , 0 , 2 * Math . PI ) ;
35+ ctx . fill ( ) ;
36+ }
37+
38+ update ( ) {
39+ if ( this . x + this . size >= width ) {
40+ this . velX = - Math . abs ( this . velX ) ;
41+ }
42+
43+ if ( this . x - this . size <= 0 ) {
44+ this . velX = Math . abs ( this . velX ) ;
45+ }
46+
47+ if ( this . y + this . size >= height ) {
48+ this . velY = - Math . abs ( this . velY ) ;
49+ }
50+
51+ if ( this . y - this . size <= 0 ) {
52+ this . velY = Math . abs ( this . velY ) ;
53+ }
54+
55+ this . x += this . velX ;
56+ this . y += this . velY ;
57+ }
58+
59+ collisionDetect ( ) {
60+ for ( const ball of balls ) {
61+ if ( ! ( this === ball ) ) {
62+ const dx = this . x - ball . x ;
63+ const dy = this . y - ball . y ;
64+ const distance = Math . sqrt ( dx * dx + dy * dy ) ;
65+
66+ if ( distance < this . size + ball . size ) {
67+ ball . color = this . color = randomRGB ( ) ;
68+ }
5069 }
51-
52- if ( ( this . y - this . size ) <= 0 ) {
53- this . velY = Math . abs ( this . velY ) ;
54- }
55-
56- this . x += this . velX ;
57- this . y += this . velY ;
58- }
59-
60- collisionDetect ( ) {
61- for ( const ball of balls ) {
62- if ( ! ( this === ball ) ) {
63- const dx = this . x - ball . x ;
64- const dy = this . y - ball . y ;
65- const distance = Math . sqrt ( dx * dx + dy * dy ) ;
66-
67- if ( distance < this . size + ball . size ) {
68- ball . color = this . color = randomRGB ( ) ;
69- }
70- }
71- }
72- }
73-
70+ }
71+ }
7472}
7573
7674const balls = [ ] ;
7775
7876while ( balls . length < 25 ) {
79- const size = random ( 10 , 20 ) ;
80- const ball = new Ball (
81- // ball position always drawn at least one ball width
82- // away from the edge of the canvas, to avoid drawing errors
83- random ( 0 + size , width - size ) ,
84- random ( 0 + size , height - size ) ,
85- random ( - 7 , 7 ) ,
86- random ( - 7 , 7 ) ,
87- randomRGB ( ) ,
88- size
89- ) ;
77+ const size = random ( 10 , 20 ) ;
78+ const ball = new Ball (
79+ // ball position always drawn at least one ball width
80+ // away from the edge of the canvas, to avoid drawing errors
81+ random ( 0 + size , width - size ) ,
82+ random ( 0 + size , height - size ) ,
83+ random ( - 7 , 7 ) ,
84+ random ( - 7 , 7 ) ,
85+ randomRGB ( ) ,
86+ size
87+ ) ;
9088
9189 balls . push ( ball ) ;
9290}
9391
9492function loop ( ) {
95- ctx . fillStyle = ' rgba(0, 0, 0, 0.25)' ;
96- ctx . fillRect ( 0 , 0 , width , height ) ;
93+ ctx . fillStyle = " rgba(0, 0, 0, 0.25)" ;
94+ ctx . fillRect ( 0 , 0 , width , height ) ;
9795
98- for ( const ball of balls ) {
99- ball . draw ( ) ;
100- ball . update ( ) ;
101- ball . collisionDetect ( ) ;
102- }
96+ for ( const ball of balls ) {
97+ ball . draw ( ) ;
98+ ball . update ( ) ;
99+ ball . collisionDetect ( ) ;
100+ }
103101
104- requestAnimationFrame ( loop ) ;
102+ requestAnimationFrame ( loop ) ;
105103}
106104
107105loop ( ) ;
0 commit comments