Gravity Dots 2
d = document
b = d.body
b.style.margin = 0
b.style.background = 'black'
r = (v = 1) => Math.random() * v
with(
b.appendChild(
d.createElement`canvas`
).getContext`2d`) {
onresize = () => {
canvas.width = innerWidth
canvas.height = innerHeight
}
onresize()
fillStyle = 'red'
dot = (
x = r(innerWidth),
y = 0,
mass = 20, sr = r(2) + 2,
R = sr,
dx, dy,
col = '#005eb0',
dist, vx = .2, vy = 0) => {
return () => {
fillStyle = col
R = sr
if (r() < .001) {
vx = r() * 4 - 2
R = sr * 2
fillStyle = 'red'
col = `hsl(${r(360)}deg, 50%, 50%)`
}
dx = innerWidth / 2 - x
dy = innerHeight / 2 - y
dist = Math.sqrt(dx * dx + dy * dy)
dist *= dist
vx += dx / dist * mass
vy += dy / dist * mass
x += vx
y += vy
beginPath()
arc(x, y, R, 0, 6.29)
fill()
}
}
const dots = []
for (let i = 0; i < 100; i++) dots.push(dot())
loop = () => {
fillStyle = 'rgba(0, 0, 0, 0.05)'
fillRect(0, 0, canvas.width, canvas.height)
dots.map(d => d())
}
setInterval(loop, 16)
}
A variation on this recent post.