Frames and nested canvases

Five ideas from the element catalogue in which the thing a canvas draws is itself a container with a document or a drawing of its own: a second drawable canvas, an <iframe>, a custom element's shadow tree, an <svg> with HTML in <foreignObject>, and the <script> element that is doing the drawing. For each one I wanted to know whether the inner content still draws, tells the outer canvas when it changed, and takes a tap at the place it was drawn. Each demo is one 2D canvas.

Widget with its own effect inside a scene

Two widgets in a room. Each widget is drawn by a small canvas of its own, and the room is a third canvas that draws those two. Drag the lamp's slider: the lamp canvas adds bloom to its card, and the room is lit by the same value. Press + or −: the counter canvas splits its card into red, green and blue for a second. Move a finger or the pointer across the room and both lean.

Lamp

40%

Counter

0

The room's paint handler never draws a card. It draws the two inner canvases, sheared, and once more upside down for the floor. A change inside an inner canvas reaches the room as a paint event with that canvas in changedElements, so the room has no listener on either widget. The line above says which route is drawing. If the inner canvases draw nothing, the page moves the cards into the room's canvas and the room runs the two effects itself. The button switches by hand.

Show the code
  1. A drawable canvas as a drawable child of a drawable canvas.
  2. Each inner canvas draws its card with its own effect.
  3. The scene draws the inner canvases as elements.
  4. The second route, if the inner canvases draw nothing.
  5. Styles.

Email preview on a drawn phone

The message is an <iframe srcdoc>. The phone round it is drawn by the canvas. Scroll the message with a finger or the wheel, and tap its links and buttons. Turn the phone makes the iframe 420 pixels wide, so the message's own media query puts its cards in two columns. The other buttons load a different message or switch its colours.

Scrolling happens inside the iframe, in the browser. The canvas hears about it only as a paint event, and draws the iframe again where the phone's screen is. No scroll or touch listener is involved.

Show the code
  1. The iframe is a drawable child of the canvas.
  2. Each message is a whole document.
  3. Listen for links inside the message.
  4. Draw the phone, then the iframe on its screen.
  5. Resize and reload the iframe outside the paint handler.
  6. Styles.

Custom element that packages an effect

The form is ordinary markup inside one tag, <effect-frame>. The tag's shadow tree holds the canvas and draws the form through the effect named in its effect attribute. The buttons below only change that attribute. The effect is calm under the pointer or the finger, so the field being used can be read. The stars are a second custom element with a shadow tree of its own.

Rate this page

The form needs no drawable attribute. The element's shadow tree has one drawable <div> round a <slot>, and the picture of that div is the picture of whatever is slotted into it. Blinds isn't in the library: this page registers it in six lines.

Show the code
  1. The page's markup: a tag round a form.
  2. The shadow tree: a canvas, a drawable div and a slot.
  3. The paint handler calls the effect and puts the hit region back.
  4. The built-in effects.
  5. Registering one more effect from the page.
  6. The stars: a custom element inside the custom element.
  7. After a lost context the children are plain HTML.
  8. Styles.

HTML button inside an SVG diagram

The diagram is one <svg> element. Each node holds HTML in a <foreignObject>: a button, a range, a checkbox and an output. The lights that travel along the edges are drawn by the canvas on the SVG's own paths. Send 5 adds lights, the range sets their size from the second node on, and the checkbox sends them the long way. Tilt the diagram shears everything, controls included.

Source
Size
Route
Arrived0

The SVG is drawn with one call. The controls in it are hit tested through the SVG's viewBox, the foreignObject and the canvas transform together. The lights use path.getPointAtLength() and the same transform, so they stay on the dashed lines when the diagram is tilted.

Show the code
  1. One drawable SVG with HTML controls in its nodes.
  2. Draw the SVG, then the lights on its paths.
  3. The node's button and the tilt.
  4. Styles.

Script that shows itself

The code block isn't a copy of the script. It's the <script> element, a child of the canvas with display: block. The script draws the wheel from its own picture: eight wedges of its source text, every second one mirrored. The text can be edited. Change a number, for example WEDGES or ZOOM, and press Run the edited script.

Editing the text of a script that has run does nothing by itself. Run the edited script puts a new <script> element with the edited text in the old one's place, and the browser runs it because it's new. A syntax error in the edited text is reported in the line under the canvas and the old script stays.

Show the code
  1. The script, in the canvas. This listing is the same text as the block in the demo.
  2. The page draws the script element and then calls the function it defined.
  3. Run the edited text by replacing the element.
  4. Styles.

How it works

In each demo the canvas draws one element whose content is produced somewhere else: by another paint handler, by another document, by a shadow tree, by SVG layout, or by the HTML parser. Four things are the same in all five:

Show the code
  1. The paint loop and the draw helpers shared by the demos.
  2. Each demo starts when its section comes near the screen.
  3. A lost context stops every demo.
  4. The elements are then shown as plain HTML.

Live checks

The checks wait for a tap. Each one builds its own small canvas in a corner of the window, and none of them touches the demos above. Pixels are read from a drawImage() copy, not from the drawable canvas.

Findings

Measured in headless Chromium 157 on 2026-11-21, at 412 px wide with emulated touch (taps, and drags sent as touch events) and at 1100 px with a mouse. Not yet run on a phone.