INNER CODE UNIT · JavaScript
Ball
slashhuang/blog · essays/tutorial/jrg_1_OOP/canvas/main.js:15
function Ball() {
this.x = random(0, width);
this.y = random(0, height);
this.velX = random(-7, 7);
this.velY = random(-7, 7);
this.color = 'rgb(' + random(0, 255) + ',' + random(0, 255) + ',' + random(0, 255) +')';
this.size = random(10, 20);
}
Ball.prototype.draw = function() {
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fill();
}
Ball.prototype.update = function() {
if ((this.x + this.size) >= width) {
this.velX = -(this.velX);
}