Dividing Squares
d = document
b = d.body
S = 300
with(
b.appendChild(Object.assign(
d.createElement`canvas`, { width: S, height: S })
).getContext`2d`) {
seed = Math.random();
rando = () => {
seed = seed * 16807 % 0x7fffffff
return (seed - 1) / 0x7ffffffe
}
squares = {}
rot = (
cx, cy, X, Y, ang,
cos = Math.cos(-ang),
sin = Math.sin(-ang),
x = cos * (X - cx) + sin * (Y - cy) + cx,
y = cos * (Y - cy) - sin * (X - cx) + cy) => ({ x, y })
inc = 0
breaks = 0
delay = 0
ticks = 0
square = (x = S / 2, y = S / 2,
size = S * .7,
r = 0,
rad = .3,
hSize = size / 2,
t = rando() * 7,
vx = rando() * rad * Math.cos(t),
vy = rando() * rad * Math.sin(t),
vr = rando() * .02 - .01,
i, j,
o = {
id:inc++,
sr(R) {
r = R
},
divide(num = 2, cSize = size / num,
chSize = cSize / 2,
sx = x - hSize + chSize,
sy = y - hSize + chSize) {
save()
translate(-size * .1, -size * .1)
scale(1.1, 1.1)
o.render('rgba(255, 255, 255, .7)')
restore()
for (i = 0; i < num; i++) {
for (j = 0; j < num; j++) {
l = rot(x, y, sx + i * cSize, sy + j * cSize, r)
square(l.x, l.y, cSize, r, rad + 2)
}
}
},
render(fill = '#000') {
save()
translate(x, y)
rotate(r)
fillStyle = fill
strokeStyle = '#fff'
lineWidth = 2
fillRect(-hSize,-hSize, size, size)
strokeRect(-hSize,-hSize, size, size)
restore()
},
draw() {
x += vx
y += vy
r += o.id === 0 ? .01 : vr
o.render()
if (rando() < .05 && breaks < 10 &&
delay > 30 && ticks > 200) {
o.divide(~~(rando() * 3) + 2)
delete squares[o.id]
breaks++
delay = 0
}
}
}) => (squares[o.id] = o)
square()
C = Object.assign(
d.createElement`canvas`, { width: S, height: S }
).getContext`2d`
fillStyle = 'rgba(82, 82, 82, 1)'
fillRect(0, 0, S, S)
loop = _ => {
ticks++
fillStyle = 'rgba(82, 82, 82, 0.1)'
fillRect(0, 0, S, S)
globalAlpha = .4
drawImage(C.canvas, -1, 5, S + 1, S + 5)
globalAlpha = 1
save()
translate(20, 50)
scale(.9, .7)
for (i in squares) {
squares[i].draw()
}
restore()
delay++
C.drawImage(canvas, 0, 0)
requestAnimationFrame(loop)
}
loop()
}
Another thing for #genuary2022… destroy a square