Reintent

Drawing a scene

Draw with SVG and Canvas so that every thing on screen has a name, a box and a time, and a scene can be checked.

Sam's card has a cake on it, drawn in SVG, with a flame on each candle drawn on a Canvas. When Sam says "the candles taller", the app has to know which pixels are the candles. When a model draws the cake again, the candles still have to be the candles. @reintent/scene is for the app's side of this. An intent never needs it: it draws, it does not choose.

You will learn

  • How semantic() and <Part> give each thing and part a name on screen
  • How <Stage> runs one clock and opens the scene to an inspector
  • How the pick buffer finds what is at a point, on SVG and on Canvas alike
  • How the verifier checks a scene a model wrote
  • How a model's SVG is kept to shapes and paint

Things with names

import { Part, num, semantic } from '@reintent/scene/svg';

export const Cake = semantic(
  {
    id: 'cake',
    kind: 'prop',
    aka: ['the cake', '蛋糕'],
    parts: ['body', 'candles', 'flame'],
    params: { height: num(1, { min: 0.5, max: 2, affects: ['candles'] }) },
  },
  ({ height }) => (
    <>
      <Part name="body">
        <rect x={-40} y={-30} width={80} height={30} fill="#f3d6c4" />
      </Part>
      <Part name="candles">
        <rect x={-2} y={-30 - 20 * height} width={4} height={20 * height} fill="#7aa2d6" />
      </Part>
    </>
  ),
);

semantic(declaration, Render) declares the cake, a Declaration with its id, its words, its parts and its params, and returns a SemanticComponent. Drawn as <Cake instance="table" />, it wraps what Render draws in <g data-sem="cake[table]">; each <Part> adds its own <g data-sem="cake[table].candles">. That string is a semantic path: parsePath takes it apart into a ParsedPath, instancePath builds one, prefixes lists the paths above it, reaches says whether a selection covers it, and isDeclared and isNode check it against DECLARATIONS, every declaration made.

A param is a num (a NumberParam) or a choice (a ChoiceParam); Param, Params and ParamValues are their types, and SemanticProps the props a semantic component takes. <Atmosphere> marks what is drawn but is not a thing, like a glow; it takes PartProps, as <Part> does. Inside a thing, useOwner() gives its Owner: its path and entity.

The Registry holds what is on stage, an InstanceRecord for each thing and a DrawRecord for each Canvas drawing, and the overrides laid over it. registry.setOverride('cake', 'height', 1.5) makes every cake's candles taller without the code changing: an edit survives the cake being drawn again.

On Canvas

Inside the cake's Render, next to its parts:

import { Draw } from '@reintent/scene/canvas';

<Draw part="flame" fn={(ctx, t) => ctx.fillRect(-1, -60 - Math.sin(t * 9), 2, 6)} />

<Draw> (its DrawProps) puts a DrawFn on the stage's canvas, owned by a path: here cake.flame, the part of the thing it is drawn inside. It runs every frame, in the scene's units.

The stage and its time

<Stage width height> (its StageProps) draws one scene: an svg, a canvas over it, one Clock and one registry, which useRegistry() reads. Inside, useTime() is the scene's time, useNow() reads it without drawing again, useFrame() runs a side effect every frame, and useFlag() reads a switch.

timeline({ beats, phases, inputs }) declares a TimelineSpec: beat(at) names a time (a BeatSpec), a PhaseSpec names a state, an InputSpec names what a person does. useTimeline() and resolveTimeline give its TimelineValues, a Timing per beat, with overrides laid over them: "the candles lit sooner" is an override on beat.lit.

The stage opens the scene as window.__scene, a SceneApi. A Still is a named moment it can go to; a Snapshot measures one, an InstanceView and NodeView per thing and part. A LanguageLayer holds the words the scene came from, each Concept with its Localized words. openScene(frame.contentWindow) waits for a scene in a frame to open.

Measuring and picking

measureGeometry gives each path's Box from its shapes; measureAtmosphere gives each AtmosphereBox; sceneToClient maps scene units to the screen. For text, createTextMeasurer() returns a TextMeasurer that measures in the DOM, and approxTextSize estimates a TextSize where there is no DOM.

What can be seen comes from the pick buffer. measureVisible paints the scene once more, each path in its own pickColour, Canvas drawings through pickContext, and pickFromPixels reads the pixels into a Pick: each path's visible box, and pick.at(x, y), the path at a point. A tap on the flame finds cake[table].flame, though it was drawn on Canvas.

Checking a scene

A model that draws the cake again runs window.__scene.verify(): verify, given a running scene's VerifyDeps, goes to each still and returns a Report of each Issue, with what to do about it. Every name used is declared, every pixel drawn belongs to a thing, the same moment draws the same pixels, and turning the candles' height moves only the candles. Its checks on data run anywhere: languageIssues, phaseIssues and localMoves.

A drawing a model wrote

When Sam asks for a cat on the card, System Two answers with SVG. That markup comes from outside, and goes through sanitize(svg, { prefix: 'cat-' }) before it is set into the card's <g>: only the ELEMENTS and ATTRIBUTES on its lists stay (shapes and paint), with no script, link or text, and every id gets the prefix, so the cat's gradient cannot clash with the cake's. SanitizeOptions holds the prefix. It runs without a DOM, on the server as in the page. The app's own markup, copied to show a still again beside the original, keeps everything and gets its ids prefixed with prefixIds.

Recap

  • semantic() and <Part> give each thing and part a semantic path, in data-sem; overrides in the registry survive the code being written again.
  • <Draw> puts Canvas drawings under the same names.
  • <Stage> runs one clock and a timeline, and opens the scene as window.__scene.
  • The pick buffer finds what is at a point and what can be seen of each thing, on SVG and Canvas alike.
  • The verifier checks a scene a model wrote, and says what to fix.
  • sanitize keeps a model's SVG to shapes and paint, its ids prefixed.

Last updated on

Edit on GitHub