Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions html-in-canvas/2d-context-offscreencanvas/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>HTML-in-Canvas basic test</title>
<script type="module" defer src="index.js"></script>
<link href="style.css" rel="stylesheet" />
</head>
<body>
<canvas width="480" height="320" content="drawable">
<form drawable>
<div>
<label for="name">Name:</label>
<input type="text" id="name" name="name" />
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" />
</div>
<button>Submit</button>
</form>
</canvas>
</body>
</html>
37 changes: 37 additions & 0 deletions html-in-canvas/2d-context-offscreencanvas/index.js
Original file line number Diff line number Diff line change
@@ -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();
34 changes: 34 additions & 0 deletions html-in-canvas/2d-context-offscreencanvas/style.css
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions html-in-canvas/2d-context-offscreencanvas/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const x = 100;
const y = 100;
let ctx;

self.addEventListener("message", ({ 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
}
});
9 changes: 5 additions & 4 deletions html-in-canvas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)).