Skip to content

πŸŽ₯ Make videos programmatically with React

Add html-in-canvas-support.html

Jonny Burger9c1fff70ceb0
diff --git a/html-in-canvas-support.html b/html-in-canvas-support.html
new file mode 100644
index 0000000000..0570c0f1f1
--- /dev/null
+++ b/html-in-canvas-support.html
@@ -0,0 +1,137 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <title>HTML-in-canvas support check</title>
+ <style>
+ body {
+ font-family: ui-monospace, monospace;
+ padding: 24px;
+ line-height: 1.5;
+ }
+ .row {
+ display: flex;
+ gap: 12px;
+ align-items: baseline;
+ }
+ .ok {
+ color: #0a7a2f;
+ }
+ .no {
+ color: #b00020;
+ }
+ code {
+ background: #f3f3f3;
+ padding: 2px 6px;
+ border-radius: 4px;
+ }
+ #stage {
+ position: fixed;
+ left: -9999px;
+ top: 0;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>HTML-in-canvas support</h1>
+ <p>
+ Spec: <a href="https://github.com/WICG/html-in-canvas">WICG/html-in-canvas</a>.
+ Enable <code>chrome://flags/#canvas-draw-element</code> in Chrome, and use
+ Chrome Canary for <code>requestPaint</code> + <code>paint</code> event.
+ </p>
+ <ul id="results"></ul>
+
+ <div id="stage">
+ <div id="probe" style="width: 32px; height: 32px; background: red"></div>
+ </div>
+
+ <script>
+ const results = document.getElementById('results');
+ const stage = document.getElementById('stage');
+ const probe = document.getElementById('probe');
+
+ const log = (label, ok, detail) => {
+ const li = document.createElement('li');
+ li.className = 'row';
+ li.innerHTML =
+ '<span class="' +
+ (ok ? 'ok' : 'no') +
+ '">' +
+ (ok ? 'YES' : 'NO ') +
+ '</span><span><code>' +
+ label +
+ '</code>' +
+ (detail ? ' β€” ' + detail : '') +
+ '</span>';
+ results.appendChild(li);
+ };
+
+ const canvas = document.createElement('canvas');
+ canvas.width = 32;
+ canvas.height = 32;
+ const ctx = canvas.getContext('2d');
+
+ const hasDrawElementImage = typeof ctx.drawElementImage === 'function';
+ log('CanvasRenderingContext2D.drawElementImage', hasDrawElementImage);
+
+ const hasLayoutSubtree = 'layoutSubtree' in canvas;
+ log('HTMLCanvasElement.layoutSubtree', hasLayoutSubtree);
+
+ const hasRequestPaint = typeof canvas.requestPaint === 'function';
+ log('HTMLCanvasElement.requestPaint()', hasRequestPaint);
+
+ (async () => {
+ if (!hasRequestPaint || !hasLayoutSubtree) {
+ log("'paint' event on canvas", false, 'skipped (requires the above)');
+ return;
+ }
+
+ canvas.layoutSubtree = true;
+ stage.removeChild(probe);
+ canvas.appendChild(probe);
+ stage.appendChild(canvas);
+
+ const fired = await new Promise((resolve) => {
+ const timer = setTimeout(() => resolve(false), 1000);
+ canvas.addEventListener(
+ 'paint',
+ () => {
+ clearTimeout(timer);
+ resolve(true);
+ },
+ {once: true},
+ );
+ try {
+ canvas.requestPaint();
+ } catch (e) {
+ clearTimeout(timer);
+ resolve(false);
+ }
+ });
+ log(
+ "'paint' event on canvas",
+ fired,
+ fired ? 'fired after requestPaint()' : 'no event within 1s',
+ );
+
+ if (fired && hasDrawElementImage) {
+ try {
+ ctx.reset();
+ ctx.drawElementImage(probe, 0, 0, 32, 32);
+ const px = ctx.getImageData(16, 16, 1, 1).data;
+ const red = px[0] > 200 && px[1] < 50 && px[2] < 50;
+ log(
+ 'drawElementImage end-to-end',
+ red,
+ red
+ ? 'captured red pixel from probe div'
+ : 'drew but pixel was rgba(' + px.join(',') + ')',
+ );
+ } catch (e) {
+ log('drawElementImage end-to-end', false, String(e));
+ }
+ }
+ })();
+ </script>
+ </body>
+</html>