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…
<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
- The host: one element.
- The host: point it at the card, then restyle it.
effectandtintchange how the host draws the pixels.themeasks the card to restyle itself.REMOTEis the other origin. - The card (other origin): its markup sits in a
<live-region>. - The card: serve pictures on the port the host sends, and accept only the theme properties it allows.
- Inside
<live-card>: open the channel when the framed card says it is ready. - Inside
<live-card>: run a click backwards through the distortion and send it to the card. - The whole element, with the shader.
- 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. - Inside: on every
paintevent, draw the element. - 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
- The frame (other origin): the card is a child of a drawable canvas.
- The frame: transfer the canvas to the host, then a snapshot on every paint.
badgeis the card element. - The host: keep the canvas and the newest snapshot. This is inside its
messagelistener;mis the message. - 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.
| Result | Experiment | Detail |
|---|
Findings
- Sending pictures and forwarding input (A) lets the host put the card anywhere: outside the frame's box, through a shader, in 3D. The cost is that the host's copy is a picture. Semantics have to be rebuilt from the element map, and the card must trust the host with its pixels.
- Lending the canvas (B) leaves hit testing, focus, selection and accessibility to the browser, so there's no input code. The cost is that the drawing stays inside the frame's rectangle.
- The card has to agree to be restyled. The host can recolour pixels by itself. Changing fonts, the radius or the width only happens because the card accepts a short list of custom properties, so the card decides what is allowed.
- In B the host decides where the embed can be clicked. Hit testing follows the last
drawElementImage()call, and the host makes that call. An embed that lends its canvas is trusting the host not to slide its buttons under the user's pointer. - The embed has to opt in either way. A cross-origin frame placed inside the host's own drawable canvas paints blank, so a host can't do any of this to an embed that doesn't cooperate.