From 9ec7b38d5351c0b4540c572d8e50ec93473c3eec Mon Sep 17 00:00:00 2001 From: Chris Mills Date: Thu, 26 Nov 2026 13:10:00 +0000 Subject: [PATCH 1/2] Add HTML-in-Canvas offscreencanvas example --- .../2d-context-offscreencanvas/index.html | 24 ++++++++++++ .../2d-context-offscreencanvas/index.js | 37 +++++++++++++++++++ .../2d-context-offscreencanvas/style.css | 34 +++++++++++++++++ .../2d-context-offscreencanvas/worker.js | 30 +++++++++++++++ html-in-canvas/README.md | 9 +++-- 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 html-in-canvas/2d-context-offscreencanvas/index.html create mode 100644 html-in-canvas/2d-context-offscreencanvas/index.js create mode 100644 html-in-canvas/2d-context-offscreencanvas/style.css create mode 100644 html-in-canvas/2d-context-offscreencanvas/worker.js diff --git a/html-in-canvas/2d-context-offscreencanvas/index.html b/html-in-canvas/2d-context-offscreencanvas/index.html new file mode 100644 index 00000000..a5d8f486 --- /dev/null +++ b/html-in-canvas/2d-context-offscreencanvas/index.html @@ -0,0 +1,24 @@ + + + + + HTML-in-Canvas basic test + + + + + +
+
+ + +
+
+ + +
+ +
+
+ + diff --git a/html-in-canvas/2d-context-offscreencanvas/index.js b/html-in-canvas/2d-context-offscreencanvas/index.js new file mode 100644 index 00000000..9b81d7e4 --- /dev/null +++ b/html-in-canvas/2d-context-offscreencanvas/index.js @@ -0,0 +1,37 @@ +const canvas = document.querySelector("canvas"); +const form = document.querySelector("canvas form"); + +function main() { + if (!("captureElementImage" in canvas)) { + alert("HTML-in-Canvas is not supported."); + return; + } + + const worker = new Worker("worker.js"); + const offscreen = canvas.transferControlToOffscreen(); + // Transfer the offscreen canvas to the worker + worker.postMessage({ canvas: offscreen }, [offscreen]); + + worker.addEventListener("message", (e) => { + if (e.data.unsupported) { + alert("drawElementImage() is not available in the worker."); + } + }); + + // Geometry updates from a worker are reported here + canvas.addEventListener("elementgeometryupdate", (e) => { + console.log("Geometry update:", e.elements); + // getElementTransform() is only available on HTMLCanvasElement, + // so it has to be read on the main thread. + console.log(canvas.getElementTransform(e.elements[0])); + }); + + canvas.addEventListener("paint", () => { + // ElementImage objects are transferrable + const formImage = canvas.captureElementImage(form); + // Transfer form image to the worker + worker.postMessage({ formImage }, [formImage]); + }); +} + +main(); diff --git a/html-in-canvas/2d-context-offscreencanvas/style.css b/html-in-canvas/2d-context-offscreencanvas/style.css new file mode 100644 index 00000000..f1c97779 --- /dev/null +++ b/html-in-canvas/2d-context-offscreencanvas/style.css @@ -0,0 +1,34 @@ +form div { + display: flex; + align-items: center; + gap: 10px; + font-family: Arial, Helvetica, sans-serif; + padding: 5px; + border: 4px solid #576490; + background: #d1d2f9; + border-radius: 5px; + margin-bottom: 10px; +} + +form input { + padding: 3px; + border: 2px solid #576490; + border-radius: 3px; +} + +form button { + background: #576490; + border: 4px solid transparent; + color: white; + padding: 10px 10px; + border-radius: 5px; + transition: 0.6s all; + cursor: pointer; +} + +form button:hover, +form button:focus { + background: #d1d2f9; + color: black; + border: 4px solid #576490; +} diff --git a/html-in-canvas/2d-context-offscreencanvas/worker.js b/html-in-canvas/2d-context-offscreencanvas/worker.js new file mode 100644 index 00000000..45969c66 --- /dev/null +++ b/html-in-canvas/2d-context-offscreencanvas/worker.js @@ -0,0 +1,30 @@ +const x = 100; +const y = 100; +let ctx; + +self.onmessage = ({ data }) => { + if (data.canvas) { + ctx = data.canvas.getContext("2d"); + if (!ctx.drawElementImage) self.postMessage({ unsupported: true }); + return; + } + + if (data.formImage) { + const img = data.formImage; + const offscreen = ctx.canvas; + + ctx.fillStyle = "#7D82B8"; + ctx.fillRect(0, 0, offscreen.width, offscreen.height); + ctx.drawElementImage(img, x, y, { + preserveElementGeometry: true, + }); + + offscreen.updateElementGeometry(img, { + canvasTransform: new DOMMatrix().translate(x, y), + }); + + console.log(offscreen.getElementImageDefaultSize(img)); + + img.close(); // release the snapshot + } +}; diff --git a/html-in-canvas/README.md b/html-in-canvas/README.md index b6b125f4..a7e5554d 100644 --- a/html-in-canvas/README.md +++ b/html-in-canvas/README.md @@ -2,7 +2,8 @@ This set of examples demonstrates usage of the [HTML-in-canvas API](https://github.com/WICG/html-in-canvas): -- [Canvas 2D context example](2d-context-example/): Demonstrates how an HTML form can be rendered on a 2D canvas with a pixel distortion effect that follows the pointer, while the form remains accessible, interactive, and submittable ([run example live](https://mdn.github.io/dom-examples/html-in-canvas/2d-context-example/)). -- [WebGL example](webgl-example/): Demonstrates how to render an HTML form as a texture on all six faces of a rotating 3D cube. The rotation pauses when the form is hovered or focused, so you can interact with the form on the front-most face as expected (clicking into it, and so on). As in the 2D example, the form remains accessible, interactive, and submittable ([run example live](https://mdn.github.io/dom-examples/html-in-canvas/webgl-example/)). -- [WebGPU example](webgpu-example/): Demonstrates the same rotating cube as the WebGL example, only using WebGPU ([run example live](https://mdn.github.io/dom-examples/html-in-canvas/webgpu-example/)). -- [Canvas 2D context nested canvases example](2d-context-nested-canvases/): Demonstrates how `paint` events fire in nested canvases. Click the button to change the color of the inner element, and watch the console to see which canvases repaint ([run example live](https://mdn.github.io/dom-examples/html-in-canvas/2d-context-nested-canvases/)). +- [Canvas 2D context example](https://github.com/mdn/dom-examples/tree/main/html-in-canvas/2d-context-example): Demonstrates how an HTML form can be rendered on a 2D canvas with a pixel distortion effect that follows the pointer, while the form remains accessible, interactive, and submittable ([run example live](2d-context-example/)). +- [WebGL example](https://github.com/mdn/dom-examples/tree/main/html-in-canvas/webgl-example): Demonstrates how to render an HTML form as a texture on all six faces of a rotating 3D cube. The rotation pauses when the form is hovered or focused, so you can interact with the form on the front-most face as expected (clicking into it, and so on). As in the 2D example, the form remains accessible, interactive, and submittable ([run example live](webgl-example/)). +- [WebGPU example](https://github.com/mdn/dom-examples/tree/main/html-in-canvas/webgpu-example): Demonstrates the same rotating cube as the WebGL example, only using WebGPU ([run example live](webgpu-example/)). +- [Canvas 2D context nested canvases example](https://github.com/mdn/dom-examples/tree/main/html-in-canvas/2d-context-nested-canvases): Demonstrates how `paint` events fire in nested canvases. Click the button to change the color of the inner element, and watch the console to see which canvases repaint ([run example live](2d-context-nested-canvases/)). +- [Canvas 2D context offscreen canvas example](https://github.com/mdn/dom-examples/tree/main/html-in-canvas/2d-context-offscreencanvas): Demonstrates how 2D canvas element rendering can be passed to an offscreencanvas and done inside a worker ([run example live](2d-context-offscreencanvas/)). From 3b6e39a6bf047bddb8ba8cb3324a3cc68af5723e Mon Sep 17 00:00:00 2001 From: Chris Mills Date: Thu, 26 Nov 2026 14:41:06 +0000 Subject: [PATCH 2/2] change on property for addEventListener() --- html-in-canvas/2d-context-offscreencanvas/worker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/html-in-canvas/2d-context-offscreencanvas/worker.js b/html-in-canvas/2d-context-offscreencanvas/worker.js index 45969c66..1b38d825 100644 --- a/html-in-canvas/2d-context-offscreencanvas/worker.js +++ b/html-in-canvas/2d-context-offscreencanvas/worker.js @@ -2,7 +2,7 @@ const x = 100; const y = 100; let ctx; -self.onmessage = ({ data }) => { +self.addEventListener("message", ({ data }) => { if (data.canvas) { ctx = data.canvas.getContext("2d"); if (!ctx.drawElementImage) self.postMessage({ unsupported: true }); @@ -27,4 +27,4 @@ self.onmessage = ({ data }) => { img.close(); // release the snapshot } -}; +});