Scene
Scene holds a canvas, a camera and a list of objects, and runs the frame loop for you. Most programs only need this class, makeShape and a tool or two.
import { Scene } from "./trid/scene.js";Creating a scene
new Scene(canvas, options?)| Option | Default | Meaning |
|---|---|---|
width | canvas's current width | Sets canvas.width (drawing resolution in pixels) |
height | canvas's current height | Sets canvas.height |
background | "#141414" | Color the canvas is cleared with each frame. "transparent" (or null) leaves it see-through: see below |
color | "#ffffff" | Default wireframe color |
lineWidth | 3 | Default wireframe thickness |
camera | { z: -1 } | The camera object |
light | { x: -0.5, y: 1, z: -0.8 } | Direction light comes from, for filled faces |
ambient | 0.25 | Brightness of faces turned away from the light (0..1) |
lights | an ambient + a directional light | Every light, instead of light and ambient |
post | [] | Post effects run on the picture, in order |
renderer | a new CanvasRenderer | What draws the scene, for example a WebGLRenderer. See Renderers |
The canvas can be resized with CSS (for example width: 100%). Drawing stays at width × height, and tools still get pointer positions in canvas pixels.
See-through background
With background: "transparent" (or null), the canvas is cleared each frame and only the objects are drawn: the page behind it shows through, so a model can float over your page's own background, like the fox on the home page. A color with some transparency, like "rgba(20, 20, 20, 0.5)", tints the page behind instead.
const scene = new Scene(canvas, { background: "transparent" });Screenshots keep the transparency (PNG). Fog fades toward the background, which here has no color: give it its own color. Post effects that darken or brighten the whole picture, like a vignette, also change the see-through parts.
Adding and removing objects
scene.add(obj)
Adds an object, fills in a missing pos, rot or scale, and returns the same object so you can keep it and animate it.
const ball = scene.add({
mesh: makeShape("sphere"),
pos: { x: 0, y: 0.5, z: 0 }, // rot and scale get defaults
color: "tomato",
});
ball.rot.y = Math.PI / 4;An object can hold other objects in children, placed relative to it: see Groups. add fills in transforms down the whole tree.
scene.remove(obj)
Takes an object out of the scene, or out of its group if it's a child. Its own children go with it. Does nothing if it isn't there.
scene.objects
The array of top-level objects, drawn in order (children right after their parent). You can read it or change it directly. To go through every object, children included, use worldObjects(scene).
Running the loop
scene.start(update?)
Starts the frame loop. Each frame:
- every tool's
update(dt)runs, - your
update(dt, time, scene)runs, scene.render()redraws everything.
Calling start again restarts the loop with the new update. Returns the scene.
scene.stop()
Stops the loop. The last frame stays on the canvas.
scene.running
Read-only: true while the loop is running.
scene.render()
Draws a single frame: the renderer clears the canvas and draws every object, then tools draw on top. Use it without start for static images, or to redraw only when something changes.
import { Scene, makeShape } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, { width: 800, height: 300, camera: { z: -2, fov: 0.6 } });
// One mesh, shared by five objects
const mesh = makeShape("cube", { size: 0.3, radius: 0.04, segments: 2 });
const cubes = [-2, -1, 0, 1, 2].map((i) => scene.add({
mesh,
pos: { x: i * 0.5, y: 0, z: 0 },
material: { fill: `hsl(${10 + i * 25}, 75%, 65%)`, smooth: true },
}));
const update = (dt, time) => {
cubes.forEach((c, i) => {
c.pos.y = Math.sin(time * 2 + i) * 0.25;
c.rot.y += dt * (1 + i * 0.3);
});
};
scene.start(update);
// Click to pause / resume
canvas.addEventListener("click", () => {
if (scene.running) scene.stop();
else scene.start(update);
});
return () => scene.destroy();
}Tools
scene.use(tool)
Adds a tool and returns it. Tools added later get the first chance at clicks and scrolls, so add camera tools first and object tools after.
const orbit = scene.use(new OrbitCamera());
scene.use(new DragObject());scene.unuse(tool)
Removes a tool and its event listeners. Does nothing if it isn't added.
scene.tools
The scene's ToolManager. You don't usually need it directly.
Exporting
scene.screenshot(options?)
Draws a new frame and returns it as an image file (a promise for a Blob, PNG by default). The page's canvas doesn't change. See Screenshots.
download(scene.screenshot(), "scene.png");scene.record(options?)
Starts recording the canvas as a video. Returns a recording with stop() and done. See Videos.
Cleaning up
scene.destroy()
Stops the loop, removes every tool along with its canvas and keyboard listeners, and frees the renderer (for example WebGL memory). Call it when the scene is no longer needed, for example before removing the canvas from the page.
Other properties
These can all be changed at any time and take effect on the next frame:
| Property | Meaning |
|---|---|
camera | The camera object (tools like OrbitCamera move it for you) |
background | Clear color |
color | Default wireframe color |
lineWidth | Default wireframe thickness |
lights | Every light: a plain array, edit it freely |
light, ambient | Shortcuts for the first directional light's direction and the first ambient light's intensity |
post | Post effects, run in order before tools draw |
renderer | What draws the scene |
canvas, ctx | The canvas and its 2D context (read them, don't replace them) |