Reproject DOM into another tab

The widgets below are DOM, slotted into a canvas on this page. Open a viewer and each named slot is streamed to it over a MessagePort. Click and type in the viewer: the events come back over the same port and are replayed on the elements here.

…or open the viewer in a new tab 0 viewers

slot="counter"

0

slot="notes"

    slot="clock"

    Show the code a slot in another tab
    1. This page: DOM, slotted into a canvas. Each slot name is something a viewer can ask for.
    2. This page: draw the slots, publish them, and give each viewer a port. A popup is handed a MessagePort directly. An unrelated tab gets one through a SharedWorker.
    3. The other tab: one <slot-mirror> per slot.
    4. The other tab: get the port and attach the mirrors to it.
    5. What publish() does: cut each drawn element out of the canvas and transfer it. After drawElementImage() the element's client rect says where it sits in the canvas, so createImageBitmap(canvas, x, y, w, h) gets exactly that element.
    6. What publish() does with input: replay it on the real element. The viewer sends element-local coordinates and keys.
    7. The SharedWorker that introduces two tabs.
    8. The whole library, including <slot-mirror>.

    How it travels

    1. Introductions. A popup gets a port directly: popup.postMessage({…}, '*', [port2]). An unrelated tab has no window reference, so both tabs connect to a SharedWorker that creates a MessageChannel and hands one end to each. After that they talk port to port.
    2. Frames. On every paint event the publisher draws the slotted elements, then cuts each element's region out with createImageBitmap(canvas, …) and transfers it. The element's client rect gives the region, because drawElementImage() moved its geometry to where it was drawn.
    3. Input. The viewer sends element-local pointer coordinates and keys. The publisher maps them with elementFromPoint(), focuses, clicks, and edits inputs with setRangeText().

    What can cross a tab boundary (live)

    ResultExperimentDetail

    Findings