• Kits
  • Sign in

poses.js

christophermagic-hand
mainHistory

Preview

Code5.1 KiB
const keys = ["x", "y", "z", "tilt", "turn", "roll", "width", "height", "open", "spark", "cue"];
const mix = (a, b, t) => a + (b - a) * t;
const smoothstep = (t) => t * t * (3 - 2 * t);
const ramp = (t, start, end) => smoothstep(Math.max(0, Math.min(1, (t - start) / (end - start))));
const rotationAxes = { x: "tilt", y: "turn", z: "roll" };
const pointOrder = ["pointUp", "pointRight", "pointDown", "pointLeft"];
const pointHold = .58;

export const pointLoopDuration = 4;

const curledFingers = [
  [32, 93, 47, 7],
  [35, 89, 49, 2],
  [38, 86, 52, -6],
  [40, 83, 50, -11],
];
const fist = [[34, 35, 24, -37], ...curledFingers];
const pointingIndex = [0, 0, 0, 7];
const turningIndex = [8, 25, 17, 7];
const thumbs = [[0, 0, 0, -2], ...curledFingers];

function interpolate(from, to, t) {
  const pose = { ...to };
  for (const key of keys) pose[key] = mix(from[key], to[key], t);
  const turn = (((to.roll - from.roll + 180) % 360) + 360) % 360 - 180;
  pose.roll = from.roll + turn * t;
  pose.shadowAccent = mix(from.shadowAccent ?? 0, to.shadowAccent ?? 0, t);
  pose.indexExtension = mix(from.indexExtension ?? 0, to.indexExtension ?? 0, t);
  pose.floorView = mix(from.floorView ?? 0, to.floorView ?? 0, t);
  pose.pointThumbWrap = mix(from.pointThumbWrap ?? 0, to.pointThumbWrap ?? 0, t);
  pose.fistThumbWrap = mix(from.fistThumbWrap ?? 0, to.fistThumbWrap ?? 0, t);
  pose.fingers = from.fingers.map((finger, index) =>
    finger.map((angle, joint) => mix(angle, to.fingers[index][joint], t)));
  return pose;
}

export function createPosePlayer(rest, openFingers, draw, requestFrame, reducedMotion) {
  const sphere = { ...rest, spark: 0, sparkX: 0, sparkY: 0, rise: 0, cue: 0 };
  const hand = { ...sphere, open: 1, x: -.14, y: -.10, roll: -12, turn: -6, tilt: -4 };
  const point = {
    ...hand, turn: 0, shadowAccent: 1, indexExtension: .19, pointThumbWrap: 1,
    fingers: [fist[0], pointingIndex, ...curledFingers.slice(1)],
  };
  const poses = {
    sphere,
    open: { ...hand, fingers: openFingers },
    fist: {
      ...hand, y: .04, tilt: -42, turn: -15, roll: -28,
      fingers: fist, fistThumbWrap: 1,
    },
    // These rolls align the straight index's 19° spread with the grid axes.
    pointLeft: { ...point, x: .36, tilt: 56, roll: 71 },
    pointRight: { ...point, x: -.36, tilt: -42, roll: -109 },
    pointUp: { ...point, x: .04, y: -.24, tilt: 0, turn: -46, roll: -19 },
    pointDown: { ...point, x: .04, y: .54, tilt: 0, turn: 48, roll: 161 },
    // The straight thumb is 54° off the local +Y axis. Rotate it onto ±Y in the scene.
    up: { ...hand, y: -.13, tilt: 0, turn: 32, roll: -54, fingers: thumbs, shadowAccent: 1 },
    down: { ...hand, y: .17, tilt: 0, turn: -34, roll: 126, fingers: thumbs, shadowAccent: 1 },
  };

  function pointLoopPose(progress) {
    const beat = (progress % 1) * pointOrder.length;
    const index = Math.floor(beat);
    const phase = beat - index;
    const from = poses[pointOrder[index]];
    if (phase <= pointHold) return from;

    const next = poses[pointOrder[(index + 1) % pointOrder.length]];
    const transition = (phase - pointHold) / (1 - pointHold);
    const turn = ramp(transition, .14, .88);
    const curl = ramp(transition, 0, .36) * (1 - ramp(transition, .46, 1));
    const pose = interpolate(from, next, turn);
    // Keep the thumb side in view as the pointing finger sweeps up from below.
    if (pointOrder[index] === "pointDown") pose.turn += 40 * turn * (1 - turn);
    // The finger leads the wrist into the turn and straightens as it settles.
    pose.fingers[1] = pointingIndex.map((angle, joint) => mix(angle, turningIndex[joint], curl));
    return pose;
  }

  const adjustedPoses = new Map();
  const getPose = (name) => adjustedPoses.get(name) ?? poses[name];
  let current;
  let target;
  let origin;
  let elapsed = 0;

  function select(name, from = current) {
    const next = getPose(name);
    if (next === target && from === current) return;
    target = next;
    origin = current = from;
    elapsed = 0;
    if (!origin || reducedMotion()) {
      current = target;
      draw(current);
    } else {
      requestFrame();
    }
  }

  function step(delta) {
    if (current === target) return;
    elapsed += delta;
    const progress = Math.min(1, elapsed / 270);
    const blend = smoothstep(progress);
    current = progress === 1 ? target : interpolate(origin, target, blend);
    draw(current);
  }

  function rotation(name) {
    const { tilt, turn, roll } = getPose(name);
    return { x: tilt, y: turn, z: roll };
  }

  function applyRotation(next) {
    if (current === target || reducedMotion()) {
      current = target = next;
      draw(current);
    } else {
      target = next;
      requestFrame();
    }
  }

  function rotate(name, axis, degrees) {
    const key = rotationAxes[axis];
    const pose = getPose(name);
    if (pose[key] === degrees) return;
    const next = { ...pose, [key]: degrees };
    adjustedPoses.set(name, next);
    applyRotation(next);
  }

  function resetRotation(name) {
    if (adjustedPoses.delete(name)) applyRotation(poses[name]);
  }

  return { select, step, rotation, rotate, resetRotation, pointLoopPose, isMoving: () => current !== target };
}

Files

Loading files

Details

Size
5.1 KiB (5,232 bytes)
Object ID
a9bf528…ab3Copy the object ID
  • README.md
  • cover.gif
  • cover.jpg
  • finger-loops.js
  • glove.js
  • index.html
  • kit.json
  • poses.js
  • soft-glove.js
  • study.css
  • study.js
  • switcher.js
Size
5.1 KiB (5,232 bytes)
Object ID
a9bf528…ab3Copy the object ID
Storage
Stored as one object
Size
5.1 KiB (5,232 bytes)
Object ID
a9bf528…ab3Copy the object ID
Storage
Stored as one object
Verification