I attempt to push something to my array. But when i use console.log it return an empty array. As a beginner, I am wondering if I did anything wrong?
const canvas = document.getElementById('canvas1')
const ctx = canvas.getContext("2d")
canvas.width = innerWidth
canvas.height = innerHeight
let particlesArray
class Particles {
constructor(x, y, directionX, directionY) {
this.x = x
this.y = y
this.directionX = directionX
this.directionY = directionY
}
draw() {
ctx.beginPath()
ctx.fillStyle = "#c1dfdc"
ctx.arc(this.x, this.y, 20, 0, 2 * Math.PI)
ctx.closePath()
ctx.fill()
}
}
particlesArray = []
//calculate how many particles should render
numberOfParticles = window.innerHeight * window.innerWidth / 9000
function generateParticlesArray() {
for (let i; i < numberOfParticles; i++) {
let x = Math.random()
let y = Math.random()
let directionX = (Math.random() - 0.5) * 8
let directionY = (Math.random() - 0.5) * 8
particlesArray.push(new Particles(x, y, directionX, directionY))
}
console.log(particlesArray)
}
generateParticlesArray()
<canvas id="canvas1"></canvas>
numberOfParticles = window.innerHeight * window.innerWidth / 9000if you log it on the line right after?console.log(JSON.stringify(particlesArray);