Skip to content

Instantly share code, notes, and snippets.

@greggman
Created August 20, 2025 06:26
Show Gist options
  • Save greggman/f45b9939a1396a54829c6332ff869fc6 to your computer and use it in GitHub Desktop.
Save greggman/f45b9939a1396a54829c6332ff869fc6 to your computer and use it in GitHub Desktop.
Canvas HDR WTF
/*bug-in-github-api-content-can-not-be-empty*/
<canvas style="zoom: 4;"></canvas>
/** @type CanvasRenderingContext2D */
const ctx = document.querySelector('canvas').getContext('2d', {
colorSpace: 'display-p3',
colorType: 'float16',
});
console.log(JSON.stringify(ctx.getContextAttributes(), null, 2));
ctx.fillStyle = 'color(rec2020 2 2 2)';
ctx.fillRect(10, 30, 30, 40);
ctx.fillStyle = 'color(display-p3 1 0.5 0)';
ctx.fillRect(50, 30, 30, 40);
ctx.fillStyle = 'color(display-p3 2 0.5 0)';
ctx.fillRect(90, 30, 30, 40);
const imgData = ctx.getImageData(0, 0, 21, 20, { pixelFormat: 'rgba-float16' });
for (let i = 0; i < imgData.data.length; ++i) {
const p = i / 4 | 0;
const ch = i % 4;
let v = 1;
if (ch < 3) {
v = p % 2 ? 20 : 1;
}
imgData.data[i] = v;
}
ctx.putImageData(imgData, 0, 0);
//console.log(imgData.data.constructor.name);
{"name":"Canvas HDR WTF","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment