Core concepts
Five ideas cover the whole library: the coordinate system, meshes, objects, the camera and the frame loop.
Coordinate system
Trid uses a left-handed system, as seen from the default camera:
- +X points right
- +Y points up
- +Z points forward, into the screen
Units are arbitrary. Angles are always in radians (Math.PI is half a turn).
import { Scene, makeShape, OrbitCamera } from "./trid/index.js";
// A custom mesh: one line from the origin along an axis
const axis = (x, y, z) => ({
vertices: [{ x: 0, y: 0, z: 0 }, { x, y, z }],
edges: [[0, 1]],
});
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 360, lineWidth: 2,
camera: { x: 1.2, y: 1, z: -1.6, fov: 0.8 },
});
scene.add({ mesh: axis(1, 0, 0), color: "#ff6b6b", lineWidth: 4 }); // X
scene.add({ mesh: axis(0, 1, 0), color: "#7ae96e", lineWidth: 4 }); // Y
scene.add({ mesh: axis(0, 0, 1), color: "#6ba8ff", lineWidth: 4 }); // Z
scene.add({ mesh: makeShape("cube", { size: 0.3 }), material: { fill: "#777" } });
scene.use(new OrbitCamera({ yaw: -0.6, pitch: -0.45 }));
scene.start();
return () => scene.destroy();
}Meshes
A mesh is a plain object with these arrays:
vertices— points in the mesh's own (local) space:{ x, y, z }edges— lines to draw, as arrays of vertex indicesfaces(optional) — polygons to fill, as arrays of vertex indices. Only used by objects with a material
An edge with 2 indices is a single segment. An edge with 3 or more is a closed loop: the last index connects back to the first.
// A square in the XY plane, drawn as one closed loop
const square = {
vertices: [
{ x: -0.5, y: -0.5, z: 0 },
{ x: 0.5, y: -0.5, z: 0 },
{ x: 0.5, y: 0.5, z: 0 },
{ x: -0.5, y: 0.5, z: 0 },
],
edges: [[0, 1, 2, 3]], // same as [0,1], [1,2], [2,3], [3,0]
};You rarely write meshes by hand: makeShape builds cubes, boxes and spheres.
Objects (mesh instances)
An object places a mesh in the world. It's a plain object too:
| Field | Default | Meaning |
|---|---|---|
mesh | none | The mesh to draw. A group that only holds children can leave it out |
pos | { x: 0, y: 0, z: 0 } | Position in world space (in its parent's space, inside a group) |
rot | { x: 0, y: 0, z: 0 } | Rotation around each axis, in radians |
scale | { x: 1, y: 1, z: 1 } | Scale along each axis |
color | scene's color | Any CSS color |
lineWidth | scene's lineWidth | Line thickness in pixels |
material | none | Fill and shade the mesh's faces instead of drawing lines. See Filled faces |
draggable | true | Set false to ignore DragObject |
hoverable | true | Set false to ignore HoverHighlight |
children | none | Objects placed relative to this one. See Groups |
visible | true | Set false to hide it and its children |
scene.add() fills in missing pos, rot and scale. Many objects can share one mesh — drawing never modifies a mesh.
Transform order
Each vertex goes from local space to world space in this order:
- Scale by
scale - Rotate around X, then Y, then Z by
rot - Translate by
pos
So an object always spins around its own center, wherever it sits. Inside a group, the same steps place it in its parent's space, then the parent's steps place that in the world, and so on up the tree.
The camera
The camera is a plain object where every field is optional:
| Field | Default | Meaning |
|---|---|---|
x, y, z | 0 | Position in world space |
yaw | 0 | Turn around Y in radians (> 0 turns right) |
pitch | 0 | Tilt around X in radians (> 0 looks up) |
fov | DEFAULT_FOV (50°) | Vertical field of view, in radians. Wider shows more but makes everything smaller |
near | 0.01 | Near plane: anything closer is clipped |
far | 1000 | Anything farther isn't drawn (WebGL renderer only) |
With yaw and pitch at 0, the camera looks down +Z. That's why Scene's default camera is { z: -1 }: one unit back from the origin, looking at it.
The camera has no roll. Lines that cross behind the camera are cut at the near plane, so you can fly through objects safely.
How a frame is drawn
For every object, every vertex goes through:
- Local → world: the object's scale, rotation and position (
localToWorld) - World → camera: move relative to the camera, undo its yaw and pitch (
worldToCamera) - Near clipping: each edge is clipped against
z = near(clipNear) - Camera → screen: perspective projection with the camera's fov and the canvas aspect ratio, then conversion to pixels (
cameraToScreen)
For wireframe objects, all edges of an object go into one canvas path and are stroked once. Objects with a material go through a few more steps (back faces dropped, shading, sorting far to near); see Filled faces. This describes the default canvas renderer; the WebGL renderer runs the same math on the graphics card. Each math step is an exported function — see the API reference.
The frame loop
scene.start(update) calls your update(dt, time, scene) every animation frame, then redraws:
dt— seconds since the last frame. It's0on the first frame and capped at0.1, so switching tabs doesn't make objects jump.time— seconds since the loop started.
Multiply speeds by dt so motion is the same at 30 or 144 FPS:
scene.start((dt, time) => {
cube.rot.y += Math.PI * dt; // half a turn per second
cube.pos.y = Math.sin(time) * 0.3; // bob up and down
});