Skip to content

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.

js
import { Scene } from "./trid/scene.js";

Creating a scene ​

js
new Scene(canvas, options?)
OptionDefaultMeaning
widthcanvas's current widthSets canvas.width (drawing resolution in pixels)
heightcanvas's current heightSets 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
lineWidth3Default wireframe thickness
camera{ z: -1 }The camera object
light{ x: -0.5, y: 1, z: -0.8 }Direction light comes from, for filled faces
ambient0.25Brightness of faces turned away from the light (0..1)
lightsan ambient + a directional lightEvery light, instead of light and ambient
post[]Post effects run on the picture, in order
renderera new CanvasRendererWhat 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.

js
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.

js
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:

  1. every tool's update(dt) runs,
  2. your update(dt, time, scene) runs,
  3. 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.

Click to pause / resume
js
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.

js
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.

js
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:

PropertyMeaning
cameraThe camera object (tools like OrbitCamera move it for you)
backgroundClear color
colorDefault wireframe color
lineWidthDefault wireframe thickness
lightsEvery light: a plain array, edit it freely
light, ambientShortcuts for the first directional light's direction and the first ambient light's intensity
postPost effects, run in order before tools draw
rendererWhat draws the scene
canvas, ctxThe canvas and its 2D context (read them, don't replace them)