manyCirclesAnimation = {
const width = 1100
const height = 600
const ctx = DOM.context2d(width, height);
const numPoints = numCircles
const palette2 = utils.random.pick(palettes)
const dataStart = _.range(0, numPoints).map(i => ({
id: i,
x: Math.random()*width,
y: Math.random()*height,
r: Math.abs(utils.random.gaussian() * 5),
fill: utils.random.pick(palette3)
}))
const dataEnd = _.range(0, numPoints).map(i => ({
id: i,
x: Math.random()*width,
y: Math.random()*height,
r: Math.abs(utils.random.gaussian() * 5),
fill: utils.random.pick(palette2)
}))
const draw = (data) => {
ctx.fillStyle = 'white'
ctx.fillRect(0, 0, width, height)
for (const { x, y, r, fill } of data ) {
ctx.beginPath()
ctx.arc(x, y, r, 0, 2*Math.PI)
ctx.fillStyle = fill
ctx.fill()
}
}
const animation = gsap.to(dataStart, {
x: (index) => dataEnd[index].x,
y: (index) => dataEnd[index].y,
r: (index) => dataEnd[index].r,
fill: (index) => dataEnd[index].fill,
duration: 3,
ease: 'bounce',
onUpdate: () => draw(dataStart)
})
animation.pause()
const play = document.querySelector('#play-many')
const pause = document.querySelector('#pause-many')
const restart = document.querySelector('#restart-many')
const reverse = document.querySelector('#reverse-many')
play.onclick = () => { console.log('playing'); animation.play() }
restart.onclick = () => animation.restart()
pause.onclick = () => animation.pause()
reverse.onclick = () => animation.reverse()
draw(dataStart)
yield ctx.canvas
}