|
| 1 | +// setup canvas |
| 2 | + |
| 3 | +const canvas = document.querySelector("canvas"); |
| 4 | +const ctx = canvas.getContext("2d"); |
| 5 | + |
| 6 | +const width = (canvas.width = window.innerWidth); |
| 7 | +const height = (canvas.height = window.innerHeight); |
| 8 | + |
| 9 | +// function to generate random number |
| 10 | + |
| 11 | +function random(min, max) { |
| 12 | + const num = Math.floor(Math.random() * (max - min + 1)) + min; |
| 13 | + return num; |
| 14 | +} |
| 15 | + |
| 16 | +// define Ball constructor |
| 17 | + |
| 18 | +function Ball(x, y, velX, velY, color, size) { |
| 19 | + this.x = x; |
| 20 | + this.y = y; |
| 21 | + this.velX = velX; |
| 22 | + this.velY = velY; |
| 23 | + this.color = color; |
| 24 | + this.size = size; |
| 25 | +} |
| 26 | + |
| 27 | +// define ball draw method |
| 28 | + |
| 29 | +Ball.prototype.draw = function () { |
| 30 | + ctx.beginPath(); |
| 31 | + ctx.fillStyle = this.color; |
| 32 | + ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI); |
| 33 | + ctx.fill(); |
| 34 | +}; |
| 35 | + |
| 36 | +// define ball update method |
| 37 | + |
| 38 | +Ball.prototype.update = function () { |
| 39 | + if (this.x + this.size >= width) { |
| 40 | + this.velX = -this.velX; |
| 41 | + } |
| 42 | + |
| 43 | + if (this.x - this.size <= 0) { |
| 44 | + this.velX = -this.velX; |
| 45 | + } |
| 46 | + |
| 47 | + if (this.y + this.size >= height) { |
| 48 | + this.velY = -this.velY; |
| 49 | + } |
| 50 | + |
| 51 | + if (this.y - this.size <= 0) { |
| 52 | + this.velY = -this.velY; |
| 53 | + } |
| 54 | + |
| 55 | + this.x += this.velX; |
| 56 | + this.y += this.velY; |
| 57 | +}; |
| 58 | + |
| 59 | +// define ball collision detection |
| 60 | + |
| 61 | +Ball.prototype.collisionDetect = function () { |
| 62 | + for (let j = 0; j < balls.length; j++) { |
| 63 | + if (!(this === balls[j])) { |
| 64 | + const dx = this.x - balls[j].x; |
| 65 | + const dy = this.y - balls[j].y; |
| 66 | + const distance = Math.sqrt(dx * dx + dy * dy); |
| 67 | + |
| 68 | + if (distance < this.size + balls[j].size) { |
| 69 | + balls[j].color = this.color = |
| 70 | + "rgb(" + |
| 71 | + random(0, 255) + |
| 72 | + "," + |
| 73 | + random(0, 255) + |
| 74 | + "," + |
| 75 | + random(0, 255) + |
| 76 | + ")"; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +// define array to store balls and populate it |
| 83 | + |
| 84 | +let balls = []; |
| 85 | + |
| 86 | +while (balls.length < 25) { |
| 87 | + const size = random(10, 20); |
| 88 | + let ball = new Ball( |
| 89 | + // ball position always drawn at least one ball width |
| 90 | + // away from the adge of the canvas, to avoid drawing errors |
| 91 | + random(0 + size, width - size), |
| 92 | + random(0 + size, height - size), |
| 93 | + random(-7, 7), |
| 94 | + random(-7, 7), |
| 95 | + "rgb(" + random(0, 255) + "," + random(0, 255) + "," + random(0, 255) + ")", |
| 96 | + size |
| 97 | + ); |
| 98 | + balls.push(ball); |
| 99 | +} |
| 100 | + |
| 101 | +// define loop that keeps drawing the scene constantly |
| 102 | + |
| 103 | +function loop() { |
| 104 | + ctx.fillStyle = "rgba(0,0,0,0.50)"; |
| 105 | + ctx.fillRect(0, 0, width, height); |
| 106 | + |
| 107 | + for (let i = 0; i < balls.length; i++) { |
| 108 | + balls[i].draw(); |
| 109 | + balls[i].update(); |
| 110 | + balls[i].collisionDetect(); |
| 111 | + } |
| 112 | + |
| 113 | + requestAnimationFrame(loop); |
| 114 | +} |
| 115 | + |
| 116 | +loop(); |
0 commit comments