Embeddable live cards

The card below is served by a different origin (). This page () can't read or touch its DOM. I wanted to know if it could still bend the card, recolour it and resize it with the buttons working. I found two ways, and each costs something different.

A · The card sends pictures, the host draws them

Waiting for the card…

Host draws (pixels)
Host asks (the card restyles itself)
Host sizes (the card reflows)

<live-card src effect> (live-card.js) runs the card in an invisible frame, receives ImageBitmaps of it over a MessagePort and draws them with a WebGL shader. A click is run backwards through the same distortion to find the point on the card, and sent to it. Press Tab: the canvas's fallback content is real buttons and a text field rebuilt from the card's element map.

Show the code the card sends pictures
  1. The host: one element.
  2. The host: point it at the card, then restyle it. effect and tint change how the host draws the pixels. theme asks the card to restyle itself. REMOTE is the other origin.
  3. The card (other origin): its markup sits in a <live-region>.
  4. The card: serve pictures on the port the host sends, and accept only the theme properties it allows.
  5. Inside <live-card>: open the channel when the framed card says it is ready.
  6. Inside <live-card>: run a click backwards through the distortion and send it to the card.
  7. The whole element, with the shader.
  8. Inside <live-region>: a shadow root with a drawable canvas and a slot. Your markup is slotted into the canvas, so it can be drawn without being moved.
  9. Inside: on every paint event, draw the element.
  10. Inside: copy the pixels to a second canvas, and black out private fields. That second canvas is the only thing that ever leaves the page.

B · The card lends its canvas, the host draws inside it

Here the frame is visible and the card's DOM is in it. The frame transferred its canvas to this page as an OffscreenCanvas, and sends an ElementImage snapshot on each paint. This page does all the drawing. Nothing is forwarded: the button you click is the frame's real button, hit-testable wherever this page last drew it.

Show the code the card lends its canvas
  1. The frame (other origin): the card is a child of a drawable canvas.
  2. The frame: transfer the canvas to the host, then a snapshot on every paint. badge is the card element.
  3. The host: keep the canvas and the newest snapshot. This is inside its message listener; m is the message.
  4. The host: draw the snapshot any way it likes. The last plain drawElementImage() tells the frame where its button is, so the click is a real click in the frame.

What a host can and can't do (live)

The checks that press, type, restyle and resize run on a hidden second copy of this page, so the demo above stays as you left it.

ResultExperimentDetail

Findings