canvas → slot, nothing else
The markup you would want to write: a <slot> inside a normal <canvas>, and normal elements elsewhere with a matching
slot attribute. No template, no shadow root, no custom element.
This paragraph sits between the canvas and the slotted elements, in the normal document flow.
A normal element in the page.
<canvas id="stage" content="drawable">
<slot name="first"></slot>
<slot name="second"></slot>
</canvas>
<section slot="first">…</section>
<section slot="second">…</section>
<script type="module">
const stage = document.getElementById('stage');
const ctx = stage.getContext('2d');
stage.onpaint = () => {
ctx.reset();
let x = 20;
for (const slot of stage.querySelectorAll('slot'))
for (const el of slot.assignedElements()) {
// Always pass a size: without one the element is drawn in canvas pixels,
// which is too big once the canvas is sized for a high-density screen.
ctx.drawElementImage(el, x, 20, el.offsetWidth, el.offsetHeight);
x += el.offsetWidth + 20;
}
};
</script>
What is going on
- Natively, nothing is projected. A
<slot>only does something inside a shadow tree, and then only for children of that tree's host. In a plain documentslot.assignedElements()is empty, the sections render in normal flow below, and drawing them throws because they are not inside the canvas. That is why the other demo wraps the canvas in a<template shadowrootmode>. - One script makes this exact markup work.
light-slots.jsattaches a shadow root to the canvas's parent, moves the canvas into it (same element, your references stay valid), and assigns matching siblings to the canvas's slots. Everything else keeps its place. - The cost of the workaround: slotted elements have to share a parent with the canvas (or opt in to being moved with
slots="document"), and document CSS reaches the canvas only because the script copies stylesheet rules into the shadow root. - The platform gap: if
<canvas content="drawable">treated its own<slot>children as real slots for elements in the same tree, this page would work with no script at all. Worth proposing on the WICG repo.
How light-slots.js works, step by step
Read the pictures top to bottom. Each one is the DOM tree of this page at that moment, and the code next to it is the part of
light-slots.js that made the change. Orange outlines mark what is new in each step. The trees leave out this page's heading and text, which are treated just like the <p>.
On the “With one script” tab this line checks the pictures against the real page.
-
Before What you write, and what the browser does with it
The canvas, its two
<slot>s, a paragraph and two sections are all ordinary children of<main>. A<slot>outside a shadow tree projects nothing, so the canvas has nothing to draw and the sections stay in the page flow.On screen <canvas id="stage" content="drawable" layoutsubtree> <slot name="first"></slot> <slot name="second"></slot> </canvas> <p>…</p> <section slot="first">…</section> <section slot="second">…</section> -
1 Attach a shadow root to the canvas’s parent
Slots only work inside a shadow tree, so the script gives the parent of the canvas one. It is in manual slot mode, so the script decides which child goes into which slot. It starts with one unnamed slot, and copies the page’s CSS rules in so your canvas styles still apply. Nothing has moved yet. Because the parent now has a shadow root, its children stop rendering until step 3 assigns them, all within the same frame.
const host = canvas.parentElement; root = host.attachShadow({ mode: 'open', slotAssignment: 'manual' }); copyDocumentStyles(root); const first = document.createElement('slot'); first.dataset.flow = ''; root.append(first); -
2 Move the canvas into the shadow root, and leave a marker
The same canvas element moves (your references and its 2D context stay valid). Its slots are now inside a shadow tree, so they are real slots. A comment stays behind to remember where the canvas was, and a second unnamed slot goes after the canvas for everything that follows it.
const marker = document.createComment('canvas moved to shadow root by light-slots'); canvas.replaceWith(marker); const after = document.createElement('slot'); after.dataset.flow = ''; // anon[region] is the unnamed slot for the content before the marker anon[region].after(canvas, after); -
3 Assign every child of the parent to a slot
The script walks the parent’s children in order. A child whose
slot="…"matches a slot in the canvas is assigned to that slot and gets thedrawableattribute. Every other child is assigned to the unnamed slot for its side of the marker, so it renders exactly where it did before. A MutationObserver repeats this step whenever children orslotattributes change.for (const node of host.childNodes) { const m = markers.indexOf(node); if (m !== -1) { region = m + 1; continue; } const name = node.nodeType === 1 ? node.getAttribute('slot') : null; if (name && named.has(name)) { if (!node.hasAttribute('drawable')) node.setAttribute('drawable', ''); named.get(name).nodes.push(node); } else if (node.nodeType === 1 || node.nodeType === 3) { flow[region].push(node); } } anon.forEach((slot, i) => slot.assign(...flow[i])); for (const { slot, nodes } of named.values()) slot.assign(...nodes); for (const canvas of state.canvases) canvas.requestPaint?.(); -
After Your paint handler draws what was slotted
The script stops there. It does not draw anything. The sections are now laid out as children of the canvas,
slot.assignedElements()returns them, and your ownpainthandler decides where each one goes. They stay real elements, so the button and the input keep working where they are drawn.stage.onpaint = () => { ctx.reset(); let x = 20; for (const slot of stage.querySelectorAll('slot')) for (const el of slot.assignedElements()) { ctx.drawElementImage(el, x, 20, el.offsetWidth, el.offsetHeight); x += el.offsetWidth + 20; } };
With slots="document" on the canvas there is one step before step 1: elements anywhere in the page whose slot matches are
appended to the canvas’s parent, because only children of the shadow host can be assigned.
The source of light-slots.js
All of it. Use it with <script type="module" src="/lib/light-slots.js"></script>, or import upgrade(canvas) from /lib/light-slots.js?manual and call it yourself.
light-slots.js
← back to the declarative shadow DOM version and the live tests