{
const { width, height } = imgdata;
const d = ((width * height) / 10000) ** 0.5;
const ctx = DOM.context2d(width, height, 1);
const radius = (x, y) => ((1 - luminance[~~x + ~~y * width]) * d) / 2;
const draw = primitives(ctx, d, prim);
const f = animation.length ? d3.easeBounceInOut : (t) => 1;
let displacements = {};
for (let x = 0; x < width; x += d)
displacements[x] = Math.random() * height * 2 - height;
const nframes = animation.length ? 100 : 1;
let newimg = loadImage(await randomImage(1024, 768, "butterfly"));
for (let frame = 0; frame <= nframes; frame++) {
const t = frame / nframes;
ctx.clearRect(0, 0, width, height);
let delta = 1 - f(t);
for (let x = 0; x < width; x += d) {
let disp = displacements[x];
for (let y = 0; y < height; y += d) {
let r = radius(x, y);
draw(x, y + delta * disp, r * (1 - delta));
}
}
yield ctx.canvas;
}
await Promises.delay(animation.length ? 2000 : 10000);
for (let frame = 0; frame <= nframes; frame++) {
const t = frame / nframes;
ctx.clearRect(0, 0, width, height);
let delta = f(t);
for (let x = 0; x < width; x += d) {
let disp = displacements[x];
for (let y = 0; y < height; y += d) {
let r = radius(x, y);
draw(x, y + delta * disp, r * (1 - delta));
}
}
yield ctx.canvas;
}
await Promises.delay(500);
mutable image = await newimg;
}