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.
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
- A drawable canvas as a
drawablechild of a drawable canvas. - Each inner canvas draws its card with its own effect.
- The scene draws the inner canvases as elements.
- The second route, if the inner canvases draw nothing.
- 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
- The iframe is a
drawablechild of the canvas. - Each message is a whole document.
- Listen for links inside the message.
- Draw the phone, then the iframe on its screen.
- Resize and reload the iframe outside the paint handler.
- 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.
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
- The page's markup: a tag round a form.
- The shadow tree: a canvas, a drawable div and a slot.
- The paint handler calls the effect and puts the hit region back.
- The built-in effects.
- Registering one more effect from the page.
- The stars: a custom element inside the custom element.
- After a lost context the children are plain HTML.
- 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.
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
- One
drawableSVG with HTML controls in its nodes. - Draw the SVG, then the lights on its paths.
- The node's button and the tilt.
- 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
- The script, in the canvas. This listing is the same text as the block in the demo.
- The page draws the script element and then calls the function it defined.
- Run the edited text by replacing the element.
- 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:
- The container is drawn with one call.
drawElementImage(container, x, y, width, height)draws the inner canvas, the iframe, the slotted children or the SVG with everything in it. - A change inside raises
paintoutside. A draw in the inner canvas, a scroll in the iframe, a typed letter in the slotted form and a new value in the SVG's output each raisepainton the canvas that draws the container. No demo forwards events from the inside to the outside. - Input follows the last draw. A tap is tested through every transform between the outer canvas and the control. Where an effect draws a container more than once (the mirror image in the room, the tiles of the glass effect, the wedges of the wheel), the last draw decides, so each handler ends with a draw at the plain position. Where that draw must add no pixels it is made inside an empty clip.
- Changes to a container are made outside the paint handler. The iframe is resized and reloaded from click listeners, the SVG's output is written from a timer and the filter of the melt effect is changed in an animation frame.
Show the code
- The paint loop and the draw helpers shared by the demos.
- Each demo starts when its section comes near the screen.
- A lost context stops every demo.
- 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.
- A drawable canvas can be a
drawablechild of another drawable canvas. The outer canvas draws it with onedrawElementImage()call. A draw in the inner canvas raised onepaintevent on the outer canvas, with the inner canvas inchangedElements. A style change on an element inside the inner canvas raised onepainton each canvas, in that order. - Reported on Chrome 159 for Android, 2026-11-21: the room drew and the two inner canvases did not. No error, lost context or script error was reported from the phone, and the cause isn't known. Headless Chromium 157 at the same pixel ratio (2.625) draws them. Since then each inner canvas is asked for its first paint, and one second after the first paint the page reads one 8 by 8 patch of a copy of the scene where the counter's card should be. If it's dark, the page switches to drawing the cards as children of the scene and sends a report. The line under the demo says which route is in use.
- Input passes through both draws. A button moved by the inner canvas's draw, then enlarged and sheared by the outer canvas's draw, was found by
elementFromPoint()at the drawn place, and taps on it counted. A touch drag on the range input in the sheared lamp widget set it to 96. - An inner canvas is recorded whole. A 400 px inner canvas in a 240 px outer canvas, drawn 300 px to the left, showed its pixel at 360 px. This differs from a
drawableelement nested in anotherdrawableelement, which /keycaps/ found is recorded clipped to the window. - A scroll inside a drawn iframe raises
paint. A touch drag of 160 px on the drawn phone screen scrolled the iframe 145 px, the page didn't scroll, and the canvas gotpaintevents with the iframe inchangedElements. The demo has no scroll or touch listener. - Links inside the iframe take taps at the drawn place, with the phone upright and with the iframe drawn at 0.84 scale after the phone was turned. Resizing the iframe applied the message's media query and raised one
paint. - The outer document sees only the iframe.
elementFromPoint()returns the<iframe>, and itsgetBoundingClientRect()is the box round the rotated drawing. - A drawable
<div>round a<slot>, in a shadow tree, holds the picture of the slotted children. The children need nodrawableattribute, they're hit tested at the drawn place, and a style change on one raisedpaintwith the shadow<div>inchangedElements. A custom element with its own shadow tree, slotted in, drew and took taps too. The<slot>itself can't be drawn ("No cached paint record for element"), and a slotted child with no attribute can't be passed todrawElementImage(). ctx.filter = 'url(#id)'looks for the filter in the canvas's own tree. For a canvas in a shadow root, a filter in the same shadow root applied. A filter in the document wasn't found, and the element was drawn unfiltered with no error. So a component can carry its SVG filters with it.- HTML in
<foreignObject>takes input through theviewBox, the foreignObject's position and a sheared canvas transform together. A button, a checkbox and a range (by touch drag) worked in the tilted diagram. An<output>written in the SVG raisedpaint. The foreignObject, and the button inside it, can't be the drawable element themselves ("No cached paint record for element"). - A
<script>element in a canvas runs, and withdisplay: blockit draws its source text. It can becontenteditable, takes a tap and typed letters at its drawn place, and can pass itself todrawElementImage(). Changing its text doesn't run it again. A new script element put in its place runs. - An SVG element has no
offsetWidth. A draw helper that reads the size from the element passedundefinedand drew nothing, with no error. The size of an SVG is passed in by hand. - The page drew nothing at rest in the widget scene (0 scene paint events in 0.7 s after a change settled), the phone and the glass effect. The diagram, the wheel and the wave, melt and blinds effects draw every frame while they are on screen.
- Not verified: everything on a phone. In particular scroll momentum inside the drawn iframe, the on-screen keyboard over the name field and over the editable script, the speed of the melt effect (an SVG displacement filter on every frame), and what a screen reader reads for the inner canvases and the iframe.