I tried to "drow" several "boubles" on the canvas. I wrote this little script, but after running it, only one circle is showing up. It's seems like it is drawing tha same "bouble" 100 times. Why?
This is my app.js:
var canvas = document.getElementById("cv");
let width = window.innerWidth * 0.98;
let height = window.innerHeight * 0.97;
canvas.width = width;
canvas.height = height;
var context = canvas.getContext("2d");
var boubles = [];
var createBouble = function() {
this.x = width * Math.random();
this.y = height * Math.random();
this.color = getColor();
this.radius = 30 + (Math.random() * 50);
// console.log(this);
return this;
}
var getColor = function() {
return 'rgba(' + (Math.random() * 255) + ', ' + (Math.random() * 255) + ', ' + (Math.random() * 255) + ', ' + 0.3 + ')';
}
var generateBoubles = function(amount) {
for (let i = 0; i < amount; i++) {
boubles.push(createBouble());
}
drowBoules();
}
var drowBoules = function() {
context.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < boubles.length; i++) {
drowBouble(i);
}
}
var drowBouble = function(index) {
context.fillStyle = boubles[index].color;
context.arc(boubles[index].x, boubles[index].y, boubles[index].radius, 0, 2 * Math.PI);
context.fill();
}
generateBoubles(10);
<canvas id="cv"></canvas>