At 19998c2Copy the commit ID
import { createStage } from "./glove.js?monochrome";
import { createPosePlayer, pointLoopDuration } from "./poses.js?point-loop-curl";
import { fingerLoops, fingerLoopPose } from "./finger-loops.js";
// Flexion at the joints, followed by spread at the base.
// Digits run from the thumb to the outer finger.
const digits = {
curl: [
[28, 35, 18, -18], [51, 69, 42, 0], [64, 76, 44, 0], [66, 78, 44, 4], [70, 80, 43, 6],
],
notice: [
[18, 25, 15, -9], [1, 18, 12, 4], [40, 52, 27, -1], [49, 57, 29, 2], [54, 65, 32, 5],
],
reach: [
[19, 24, 13, -13], [-5, 2, 1, 7], [56, 65, 28, 0], [61, 68, 32, 3], [65, 73, 36, 6],
],
pinch: [
[26, 29, 12, -43], [36, 44, 23, 10], [52, 64, 39, -1], [59, 69, 41, 2], [64, 75, 42, 5],
],
reveal: [
[10, 12, 6, -3], [1, 7, 4, 5], [-5, 6, 3, 0], [0, 9, 5, -4], [7, 14, 8, -8],
],
};
const variants = [
{ name: "Sleight of Hand", note: "A careful pinch and a palm-up flourish.", length: 5.6, beats: [.07,.25,.43,.71], lift: .78, prop: "star" },
{ name: "Rubber Glove", note: "A big windup, a long stretch, and a spring back.", length: 4.75, beats: [.07,.28,.42,.69], lift: 1.0, prop: "star", elastic: true },
{ name: "Pocket Blob", note: "A longer pause in blob form before the fingers emerge.", length: 6.0, beats: [.07,.30,.48,.74], lift: .58, prop: "pearl", tucked: true },
{ name: "Paper Puppet", note: "An upright wrist and quick, theatrical turns.", length: 5.1, beats: [.07,.24,.39,.68], lift: .86, prop: "diamond", crisp: true },
{ name: "Ghost Glove", note: "A slow drift and a reveal that floats out of its grasp.", length: 6.65, beats: [.07,.25,.47,.70], lift: 1.06, prop: "moon" },
];
function frame(time, shape, { x = 0, y = 0, z = 0, tilt = 0, turn = 0, roll = 0, width = 1, height = 1, open = 1 } = {}) {
return { time, shape, x, y, z, tilt, turn, roll, width, height, open };
}
function timeline(variant) {
const [curl, notice, reach, reveal] = variant.beats;
const turn = variant.crisp ? 1.34 : 1;
const rest = { x: -.21, y: -.34, tilt: -6, turn: -12, roll: 4, open: 0 };
return [
frame(0, "curl", rest),
frame(curl, "curl", { ...rest, y: -.37 }),
frame(notice - (variant.tucked ? .057 : .095), "curl", {
...rest, x: -.27, y: -.29, roll: 12,
}),
frame(notice, "notice", {
x: -.35, y: -.01, tilt: -12, turn: -28 * turn, roll: -19, open: .79,
}),
frame(reach, "reach", {
x: .16, y: .06, tilt: 3, turn: -14, roll: -51, height: variant.elastic ? 1.24 : 1.02,
}),
frame(reveal - .14, "pinch", { x: .44, y: .085, z: .06, tilt: 9, turn: -3, roll: -61 }),
frame(reveal - .075, "pinch", { x: .15, y: -.09, tilt: 17, turn: 5, roll: -61, height: variant.elastic ? .88 : .98 }),
frame(reveal, "reveal", { x: -.06, y: -.055, tilt: -26, turn: 11 * turn, roll: -51 }),
frame(reveal + .078, "reveal", { x: -.15, y: -.015, tilt: -21, turn: 8, roll: -44 }),
frame(.90, "curl", { ...rest, y: -.41, width: 1.09, height: .90, open: .09 }),
frame(1, "curl", rest),
];
}
const mix = (a, b, t) => a + (b - a) * t;
const clamp = (number) => Math.min(1, Math.max(0, number));
const smooth = (t) => t * t * (3 - 2 * t);
const easeOut = (t) => 1 - (1 - t) ** 3;
function render(study) {
if (study.loop === "point") {
study.draw?.(study.posePlayer.pointLoopPose(study.progress));
study.scrubber.value = Math.round(study.progress * 1000);
return;
}
const fingerLoop = fingerLoops[study.loop];
if (fingerLoop) {
study.draw?.(fingerLoopPose(fingerLoop, study.progress));
study.scrubber.value = Math.round(study.progress * 1000);
return;
}
const { variant, frames, progress, buttons, scrubber, draw } = study;
const target = frames.findIndex((frame, index) => index > 0 && frame.time >= progress);
const from = frames[target - 1];
const to = frames[target];
const fraction = clamp((progress - from.time) / (to.time - from.time));
const blend = to.shape === "reveal" && from.shape === "pinch"
? easeOut(fraction)
: smooth(fraction);
const get = (key) => mix(from[key], to[key], blend);
const [, notice, reach, reveal] = variant.beats;
const release = clamp((progress - reveal + .075) / .21);
const rise = easeOut(release);
const fade = 1 - smooth(clamp((progress - reveal - .052) / (1 - reveal - .052)));
const beat = progress >= reveal + .10 ? 0
: progress >= reveal - .048 ? 3
: progress >= reach - .052 ? 2
: progress >= notice - .05 ? 1 : 0;
const open = get("open");
draw?.({
x: get("x"), y: get("y") + .045 * Math.sin(progress * Math.PI * 2), z: get("z"),
tilt: get("tilt"), turn: get("turn"), roll: get("roll"),
width: get("width"), height: get("height"), open,
fingers: digits[from.shape].map((digit, index) =>
digit.map((angle, joint) => mix(angle, digits[to.shape][index][joint], blend))),
spark: easeOut(clamp(release * 4.8)) * fade,
sparkX: 1.12 + rise * .33, sparkY: .41 + rise * variant.lift, rise,
cue: .75 * smooth(clamp((progress - notice + .11) / .052)) *
(1 - smooth(clamp((progress - reveal + .15) / .09))),
});
scrubber.value = Math.round(progress * 1000);
if (beat !== study.beat) {
buttons[study.beat]?.removeAttribute("aria-current");
buttons[beat].setAttribute("aria-current", "step");
study.beat = beat;
}
}
const motion = matchMedia("(prefers-reduced-motion: reduce)");
const switcher = document.querySelector("prototype-switcher");
const template = document.querySelector("#study");
let request = 0;
let lastFrame = 0;
const visible = new Set();
const studiesByStage = new Map();
function schedule() {
const needsFrame = (study) => study.loop ? study.draw && study.playing : study.posePlayer?.isMoving();
if (!request && !document.hidden && [...visible].some(needsFrame)) {
request = requestAnimationFrame(tick);
}
}
function tick(now) {
request = 0;
const delta = lastFrame ? Math.min(now - lastFrame, 64) : 0;
lastFrame = now;
for (const study of visible) {
if (!study.loop) {
study.posePlayer?.step(delta);
} else if (study.playing && study.draw) {
const duration = study.loop === "point"
? pointLoopDuration
: fingerLoops[study.loop]?.length ?? study.variant.length;
study.progress = (study.progress + delta / (duration * 1000)) % 1;
render(study);
}
}
schedule();
if (!request) lastFrame = 0;
}
function loadStage(study, element) {
study.loading = true;
createStage(element, study.variant).then((draw) => {
study.draw = (pose) => {
study.renderedPose = pose;
draw(pose);
};
const rest = { ...study.frames[0], fingers: digits.curl };
study.posePlayer = createPosePlayer(
rest, digits.reveal, study.draw, schedule, () => motion.matches,
);
element.querySelector(".status").remove();
if (study.loop) render(study);
else study.posePlayer.select(study.pose);
syncRotationPanel(study);
schedule();
}).catch((error) => {
console.error(error);
element.querySelector(".status").textContent = "The 3D preview could not be loaded.";
});
}
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
const study = studiesByStage.get(entry.target);
if (entry.isIntersecting) {
visible.add(study);
if (!study.loading) loadStage(study, entry.target);
} else {
visible.delete(study);
}
}
schedule();
});
function setPlaying(study, playing) {
study.playing = playing;
study.play.textContent = playing ? "Pause" : "Play";
schedule();
}
function seek(study, progress) {
setPlaying(study, false);
study.progress = progress;
render(study);
}
function selectPose(study, pose) {
study.pose = pose;
if (study.loop) setLoop(study, null);
else {
updatePoseButtons(study);
study.posePlayer?.select(pose, study.renderedPose);
syncRotationPanel(study);
}
}
function updatePoseButtons(study) {
for (const button of study.poseButtons) {
button.setAttribute("aria-pressed", String(!study.loop && button.dataset.pose === study.pose));
}
}
function syncRotationPanel(study) {
study.rotationPanel.hidden = study.loop || study.pose === "sphere";
study.rotationPanel.disabled = !study.posePlayer;
if (study.rotationPanel.hidden || !study.posePlayer) return;
const angles = study.posePlayer.rotation(study.pose);
for (const input of study.rotationInputs) input.value = angles[input.dataset.axis];
}
function setLoop(study, loop) {
study.loop = loop;
for (const button of study.loopButtons) {
button.setAttribute("aria-pressed", String(button.dataset.loop === loop));
}
study.loopControls.hidden = !loop;
study.transportBeats.hidden = loop !== "reveal";
updatePoseButtons(study);
if (loop) {
seek(study, 0);
setPlaying(study, !motion.matches);
} else {
setPlaying(study, false);
study.posePlayer?.select(study.pose, study.renderedPose);
}
syncRotationPanel(study);
}
const studies = variants.map((variant, index) => {
const section = document.createElement("section");
section.dataset.variant = variant.name;
section.dataset.note = variant.note;
section.append(template.content.cloneNode(true));
const stage = section.querySelector(".stage");
const study = {
variant, frames: timeline(variant), progress: 0, beat: -1, loop: null, pose: "sphere",
loopButtons: [...section.querySelectorAll("[data-loop]")],
transportBeats: section.querySelector(".beats"),
loopControls: section.querySelector(".loop-controls"),
rotationPanel: section.querySelector(".rotation-panel"),
rotationInputs: [...section.querySelectorAll("[data-axis]")],
poseButtons: [...section.querySelectorAll("[data-pose]")],
play: section.querySelector(".play"),
scrubber: section.querySelector(".transport input"),
buttons: [...section.querySelectorAll(".beats button")],
};
study.loopControls.id = `loop-controls-${index + 1}`;
for (const button of study.loopButtons) {
button.setAttribute("aria-controls", study.loopControls.id);
button.addEventListener("click", () => {
const loop = button.dataset.loop;
setLoop(study, study.loop === loop ? null : loop);
});
}
for (const button of study.poseButtons) {
button.addEventListener("click", () => selectPose(study, button.dataset.pose));
}
for (const input of study.rotationInputs) {
input.addEventListener("input", () => {
if (input.validity.valid) study.posePlayer?.rotate(study.pose, input.dataset.axis, input.valueAsNumber);
});
input.addEventListener("blur", () => syncRotationPanel(study));
}
section.querySelector(".reset-angles").addEventListener("click", () => {
study.posePlayer?.resetRotation(study.pose);
syncRotationPanel(study);
});
syncRotationPanel(study);
study.play.addEventListener("click", () => setPlaying(study, !study.playing));
study.scrubber.addEventListener("input", () => seek(study, study.scrubber.valueAsNumber / 1000));
study.buttons.forEach((button, index) => {
button.addEventListener("click", () => seek(study, variant.beats[index]));
});
render(study);
setPlaying(study, false);
studiesByStage.set(stage, study);
observer.observe(stage);
switcher.append(section);
return study;
});
motion.addEventListener("change", () => {
for (const study of studies) {
if (!study.loop) {
if (motion.matches) study.posePlayer?.step(Infinity);
} else if (motion.matches) {
setPlaying(study, false);
}
}
});
document.addEventListener("visibilitychange", () => {
lastFrame = 0;
schedule();
});