class Circle {
constructor (x, y, size, color) {
this.x = x
this.y = y
this.size = size
this.color = color
this.speed = {
x: speed,
y: speed
}
this.colorFactors = {
r: random(),
g: random(),
b: random(),
a: random()
}
}
move (vx = this.speed.x, vy = this.speed.y) {
this.x += vx
this.y += vy
if (this.x + this.size > width || this.x - this.size < 0) {
this.speed.x = - this.speed.x
}
if (this.y + this.size > height || this.y - this.size < 0) {
this.speed.y = - this.speed.y
}
}
draw (ctx) {
ctx.beginPath()
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2)
const { r, g, b, a } = this.color
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${a})`
ctx.fill()
}
randomizeMovement () {
this.speed = {
x: random() * speed,
y: random() * speed
}
setTimeout(this.randomizeMovement.bind(this), Math.random() * 10000)
}
changeColorRandomly () {
this.colorFactors = {
r: this.colorFactors.r + random(),
g: this.colorFactors.g + random(),
b: this.colorFactors.b + random(),
a: this.colorFactors.a + random()
}
this.color = {
r: this.color.r + this.colorFactors.r,
g: this.color.g + this.colorFactors.g,
b: this.color.b + this.colorFactors.b,
a: this.color.a + this.colorFactors.a
}
}
changeColorByPosition () {
const scaleX = d3.scaleLinear().domain([0, width]).range([0, Math.PI * 2])
const scaleY = d3.scaleLinear().domain([0, height]).range([0, Math.PI])
this.color = {
r: Math.sin(scaleX(this.x)) * 255,
g: 0,
b: Math.sin(scaleY(this.y)) * 255,
a: 1
}
}
}