Übergang: Würfel

Beide Bilder liegen auf zwei Seiten eines Würfels, der sich dreht und kurz zurückweicht. Einfache 3D-Rechnung in JavaScript, projiziert mit `stampAt`. Eine abgewandte Seite wird einfach nicht gezeichnet; sonst läge sie spiegelverkehrt über der anderen.

A
A — Übergang: Würfel
B
B — Übergang: Würfel
Animation
Animation — Übergang: Würfel
JavaScript
// Cube turn — 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: DIRECTION:enum(left|right)=left
//!PARAM: BACKGROUND:color=#101418
//!PARAM: PERSPECTIVE:number=1.6,min=0.5,max=4,step=0.1
//!PARAM: RECEDE:number=0.45,min=0,max=1.5,step=0.05

function transition(from, to, T) {
  if (T <= 0 || T >= 1) {
    // At the seam exactly the image, without the soft edges of the projection.
    (T <= 0 ? to : from).free();
    return T <= 0 ? from : to;
  }
  const w = to.width, h = to.height;
  const focal = PERSPECTIVE * w;
  const turned = ((T * T * (3 - 2 * T)) * Math.PI) / 2;
  const sign = DIRECTION === "left" ? 1 : -1;
  const depth = RECEDE * w * Math.sin(Math.PI * T);

  // Corner (u, y) of a cube face turned by `angle` from the front.
  const corner = (u, y, angle) => {
    const x = u * Math.cos(angle) + (w / 2) * Math.sin(angle);
    const z = w / 2 + u * Math.sin(angle) - (w / 2) * Math.cos(angle) + depth;
    const k = focal / (focal + z);
    return px(w / 2 + x * k, h / 2 + (y - h / 2) * k);
  };
  const out = Engine.createColoredImage(w, h, BACKGROUND);
  const face = (image, angle) => {
    // A face turned away would land mirrored on the other one: A cube does not show it.
    if (corner(w / 2, 0, angle).x <= corner(-w / 2, 0, angle).x) return;
    image.brightness(Math.max(0.35, Math.cos(angle)));   // darker as it turns away
    out.stampAt(image, [
      corner(-w / 2, 0, angle),
      corner(w / 2, 0, angle),
      corner(w / 2, h, angle),
      corner(-w / 2, h, angle),
    ]);
  };
  face(to, sign * (Math.PI / 2 - turned));
  face(from, -sign * turned);
  to.free();
  from.free();
  return out;
}

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