# 3D Workout

A prototype for watching looping 3D models of calisthenics exercises and queuing them into a workout plan. It is a static web page with no dependencies and no build step: plain ES modules, a small software 3D pipeline, and Canvas 2D drawing.

Everything is saved in the browser's `localStorage` and nothing is ever sent anywhere.

## What is in it

- **Viewer.** A looping model of the selected exercise. Press and drag (mouse, finger or stylus) to rotate, pinch or scroll to zoom, double-click or double-tap to reset, arrow keys when the model has focus. Front, Side, Top and 3/4 camera buttons, speed from 0.5× to 2×, a skeleton view, and auto-spin.
- **Exercises tab.** The library, with a still preview of each exercise, a filter by group (push, pull, legs, core, cardio) and an Add to plan button.
- **Plan tab.** The queue: sets, reps and rest per exercise, reorder, duplicate, remove, total time, and Start workout.
- **Workout mode.** Steps through the plan set by set with a rep and set counter and rest timers. Rep time counts animation time (so speed matters) and rests count real time.
- **Settings tab.** Theme, floor, shadow, pause-while-holding, export and import of the plan and settings, and erase all.

## How the models work

Every figure is a skeleton with fixed bone lengths (see `BODY` in `rig.js`). An exercise is a function `pose(t)` that takes the position in one rep's loop (0 to 1) and returns where the pelvis, chest, hands and feet are. The arms and legs are then solved with two-bone inverse kinematics, so limb lengths never stretch. Every exercise starts and ends in the same pose so the loop is seamless, and the unit tests check bone lengths, the floor, loop closure and smoothness for every exercise at 96 points along the loop.

## Files

```
index.html             page shell
assets/app.css         styles (tokens on :root)
assets/js/math3d.js    vectors, easing, two-bone IK, orbit camera and perspective projection (no DOM)
assets/js/rig.js       body dimensions, figure builder, and the list of shapes a renderer draws (no DOM)
assets/js/exercises.js the exercise library: one pose(t) function per exercise (no DOM)
assets/js/renderer.js  Canvas 2D renderer: depth-sorted capsules and spheres, floor, shadow
assets/js/controls.js  pointer-based orbit controls (drag, pinch, wheel, keyboard)
assets/js/plan.js      plan model, storage, backup files and the workout runner (no DOM)
assets/js/app.js       UI
dev/make-thumb.mjs     draws the home-page thumbnail from the real models
tests/unit/            node --test
tests/e2e/smoke.mjs    Playwright smoke test
```

## Swapping in a real 3D renderer

Only `renderer.js` draws anything. It receives plain data (joint positions, props, camera) and exposes `resize(w, h)` and `render(frame)`. A WebGL or three.js renderer with the same two methods, plus `setTheme`, can replace it without touching the exercises, the plan or the controls. The rig already describes the figure as capsules and spheres (`figureParts` in `rig.js`), which map directly onto instanced meshes, or onto a skinned model by treating the joint names as bones.

## Storage

Keys: `w3d.plan.v1` (the plan) and `w3d.settings.v1` (settings). Backup files are JSON with `app: "3d-workout"`, `format`, `kind` (`all`, `plan` or `settings`), and the matching `plan` and/or `settings`.
