11// setup 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
11- function random ( min , max ) {
12- return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
11+ function random ( min , max ) {
12+ return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
1313}
1414
1515// function to generate random color
@@ -21,17 +21,23 @@ function randomRGB() {
2121// define Ball constructor
2222
2323function Ball ( ) {
24- this . x = random ( 0 , width ) ;
25- this . y = random ( 0 , height ) ;
26- this . velX = random ( - 7 , 7 ) ;
27- this . velY = random ( - 7 , 7 ) ;
24+ this . size = random ( 10 , 20 ) ;
25+ this . x = random ( this . size * 2 , width ) ;
26+ this . y = random ( this . size * 2 , height ) ;
27+ this . velX = random ( - 7 , 7 ) ;
28+ this . velY = random ( - 7 , 7 ) ;
2829 this . color = randomRGB ( ) ;
29- this . size = random ( 10 , 20 ) ;
30+
31+ while ( this . velX == 0 && this . velY == 0 ) {
32+ var change = random ( 0 , 1 ) ; //decides whether to change x or y
33+ if ( change == 0 ) this . velX = random ( - 7 , 7 ) ;
34+ else this . velY = random ( - 7 , 7 ) ;
35+ }
3036}
3137
3238// define ball draw method
3339
34- Ball . prototype . draw = function ( ) {
40+ Ball . prototype . draw = function ( ) {
3541 ctx . beginPath ( ) ;
3642 ctx . fillStyle = this . color ;
3743 ctx . arc ( this . x , this . y , this . size , 0 , 2 * Math . PI ) ;
@@ -40,21 +46,21 @@ Ball.prototype.draw = function() {
4046
4147// define ball update method
4248
43- Ball . prototype . update = function ( ) {
44- if ( ( this . x + this . size ) >= width ) {
45- this . velX = - ( this . velX ) ;
49+ Ball . prototype . update = function ( ) {
50+ if ( this . x + this . size >= width ) {
51+ this . velX = - this . velX ;
4652 }
4753
48- if ( ( this . x - this . size ) <= 0 ) {
49- this . velX = - ( this . velX ) ;
54+ if ( this . x - this . size <= 0 ) {
55+ this . velX = - this . velX ;
5056 }
5157
52- if ( ( this . y + this . size ) >= height ) {
53- this . velY = - ( this . velY ) ;
58+ if ( this . y + this . size >= height ) {
59+ this . velY = - this . velY ;
5460 }
5561
56- if ( ( this . y - this . size ) <= 0 ) {
57- this . velY = - ( this . velY ) ;
62+ if ( this . y - this . size <= 0 ) {
63+ this . velY = - this . velY ;
5864 }
5965
6066 this . x += this . velX ;
@@ -63,15 +69,22 @@ Ball.prototype.update = function() {
6369
6470// define ball collision detection
6571
66- Ball . prototype . collisionDetect = function ( ) {
72+ Ball . prototype . collisionDetect = function ( ) {
6773 for ( const ball of balls ) {
68- if ( ( ! ( this . x === ball . x && this . y === ball . y && this . velX === ball . velX && this . velY === ball . velY ) ) ) {
74+ if (
75+ ! (
76+ this . x === ball . x &&
77+ this . y === ball . y &&
78+ this . velX === ball . velX &&
79+ this . velY === ball . velY
80+ )
81+ ) {
6982 const dx = this . x - ball . x ;
7083 const dy = this . y - ball . y ;
7184 const distance = Math . sqrt ( dx * dx + dy * dy ) ;
7285
7386 if ( distance < this . size + ball . size ) {
74- ball . color = this . color = randomRGB ( ) ;
87+ ball . color = this . color = randomRGB ( ) ;
7588 }
7689 }
7790 }
@@ -84,10 +97,10 @@ const balls = [];
8497// define loop that keeps drawing the scene constantly
8598
8699function loop ( ) {
87- ctx . fillStyle = ' rgba(0,0,0,0.25)' ;
88- ctx . fillRect ( 0 , 0 , width , height ) ;
100+ ctx . fillStyle = " rgba(0,0,0,0.25)" ;
101+ ctx . fillRect ( 0 , 0 , width , height ) ;
89102
90- while ( balls . length < 25 ) {
103+ while ( balls . length < 25 ) {
91104 const ball = new Ball ( ) ;
92105 balls . push ( ball ) ;
93106 }
@@ -101,5 +114,4 @@ function loop() {
101114 requestAnimationFrame ( loop ) ;
102115}
103116
104-
105117loop ( ) ;
0 commit comments