Transition: Roll up
The old image rolls up from the corner into a tube that grows, crosses the frame and leaves. A flat, folded-over flap only reads as a shrinking triangle; the roll is what makes it read as paper. The surface comes from a `warpGrid` mesh, the roundness from canvas gradients.
A
B
Animation
JavaScript
// Roll up — one transition between two photos, exported as a looping animated WebP.
//
// Everything specific sits in transition(from, to, T): it takes two images it may change and a
// progress T from 0 to 1, and returns one new frame. Video tools can call exactly that for each
// frame of a cut; the rest of this script only builds the animation.
//!INPUT: A, B
//!OUTPUT: OUT
//!PARAM: STEPS:integer=18,min=2,max=120
//!PARAM: DELAY_MS:integer=50,min=10,max=2000
//!PARAM: HOLD_MS:integer=700,min=0,max=5000
//!PARAM: RADIUS:number=34,min=8,max=300,step=2
//!PARAM: PAPER:string=auto
function transition(from, to, T) {
if (T <= 0 || T >= 1) {
(T <= 0 ? to : from).free();
return T <= 0 ? from : to;
}
const w = to.width, h = to.height;
// Corner at the bottom right; n points from it to the centre, s is the distance along n.
const ox = w, oy = h;
const nl = Math.hypot(w / 2 - ox, h / 2 - oy);
const nx = (w / 2 - ox) / nl, ny = (h / 2 - oy) / nl;
const smax = w * Math.abs(nx) + h * Math.abs(ny);
const start = RADIUS * 0.25;
// f: where the roll touches. It starts fully outside and rolls fully out at the end.
const f = -start + (T * T * (3 - 2 * T)) * (smax + RADIUS + start);
// The more is rolled up, the thicker the roll: Its area grows with the length.
const rolled = Math.min(1, Math.max(0, f) / smax);
const r = Math.sqrt(start * start + (RADIUS * RADIUS - start * start) * rolled);
const L = 2 * (w + h), tx = -ny, ty = nx;
const at = (s, t) => `${ox + nx * s + tx * t},${oy + ny * s + ty * t}`;
const bandPath = (a, b) => `M ${at(a, -L)} L ${at(a, L)} L ${at(b, L)} L ${at(b, -L)} Z`;
const point = (s) => px(ox + nx * s, oy + ny * s);
// White where a <= s <= b.
const band = (a, b) => {
const cv = Engine.createCanvas(w, h);
cv.fill("#000").drawPathStr(`M 0,0 L ${w},0 L ${w},${h} L 0,${h} Z`);
cv.fill("#fff").drawPathStr(bandPath(a, b));
const mask = cv.toImage();
cv.free();
return mask;
};
// A gradient across the roll, laid over what is there.
const gradient = (a, b, colors, stops) => {
const cv = Engine.createCanvas(w, h);
cv.linearGradient(point(a), point(b), colors, stops);
cv.drawPathStr(bandPath(a, b));
const image = cv.toImage();
cv.free();
to.blendAt(image, px(0, 0), 1, Blend.Over);
image.free();
};
// The part of the old image still lying flat.
const flatMask = band(f, 1e5);
const flat = from.clone().applyMask(flatMask);
to.blendAt(flat, px(0, 0), 1, Blend.Over);
flat.free();
flatMask.free();
// Shadow of the roll on both images.
gradient(f - r * 1.6, f - r * 0.8, ["#00000000", "#00000080"], [0, 1]);
gradient(f + r * 0.8, f + r * 1.4, ["#00000080", "#00000000"], [0, 1]);
// Paper to suit the image: A white sheet glares on a dark one.
const mean = from.clone().resize(1, 1);
const c = mean.getPixel(px(0, 0));
mean.free();
const isDark = 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b < 0.35;
const paper = PAPER !== "auto" ? PAPER : isDark ? "#2b3038" : "#f4f4f2";
// The roll shows the back of the sheet. What lies on top came round the arc from the point of
// contact, so the pattern travels with the movement.
const STEP = 6;
const cols = Math.ceil(w / STEP) + 1, rows = Math.ceil(h / STEP) + 1;
const nodes = [];
for (let i = 0; i < rows; i++) {
const row = [];
for (let j = 0; j < cols; j++) {
const x = (j / (cols - 1)) * w, y = (i / (rows - 1)) * h;
const s = (x - ox) * nx + (y - oy) * ny;
const angle = Math.asin(Math.min(1, Math.max(-1, (s - f) / r)));
const across = s - (f - r * (Math.PI + angle));
row.push((x - nx * across) / w, (y - ny * across) / h);
}
nodes.push(row);
}
const skin = from.warpGrid({ rows, cols, nodes, border: WarpBorder.Transparent });
skin.tint(paper, 0.82);
const roll = Engine.createColoredImage(w, h, paper);
roll.blendAt(skin, px(0, 0), 1, Blend.Over);
const rollMask = band(f - r, f + r);
roll.applyMask(rollMask);
to.blendAt(roll, px(0, 0), 1, Blend.Over);
roll.free();
rollMask.free();
// Light and roundness: dark edges, the highlight a little towards the light.
gradient(
f - r,
f + r,
["#00000099", "#00000033", "#ffffff2e", "#00000026", "#000000a6"],
[0, 0.18, 0.42, 0.72, 1],
);
from.free();
return to;
}
const imgA = Engine.loadImage(A);
const imgB = Engine.loadImage(B);
if (imgB.width !== imgA.width || imgB.height !== imgA.height) {
imgB.resize(imgA.width, imgA.height);
}
const anim = Engine.animation(imgA.width, imgA.height, { loop: 0 });
const p = Engine.progress("Transition frames", { total: STEPS * 2 });
// There and back: the loop closes without a jump, and both directions show the effect.
for (const [first, second] of [[imgA, imgB], [imgB, imgA]]) {
for (let i = 0; i < STEPS; i++) {
const T = i / (STEPS - 1);
const frame = transition(first.clone(), second.clone(), T);
anim.addFrame(frame, { delay: i === STEPS - 1 ? HOLD_MS : DELAY_MS });
frame.free();
p.step(1, `frame ${i + 1}/${STEPS}`);
}
}
anim.saveWebP(OUT, { lossy: true, quality: 62 });
anim.free();
imgA.free();
imgB.free();
// © 2026 Michael Lechner · mlc OpticScript · https://mlcgo.eu · Elastic License 2.0