This module contains a collection of functions and classes to work with the HDR support for HTML canvas
elements in chromium based (like Chrome, Edge, Opera and Brave) browsers.
This should only be considered as proof of concept or alpha code, don't use it in production environments!
Even if the display of HDR images works, the HDR support for the canvas
element needs the browser flag enable-experimental-web-platform-features
to be enabled. For example, open chrome://flags#enable-experimental-web-platform-features in Chrome to activate it.
Especially operations on the ImageData
arrays are not optimized, e.g. quite slow.
Import the required function(s):
import { checkHDR, checkHDRCanvas } from "hdr-canvas";
The functions return true
if HDR is supported, example:
const canvas = document.getElementById("canvas");
if (checkHDRCanvas()) {
canvas.configureHighDynamicRange({ mode: "extended" });
} else {
console.debug("hdr not supported");
return;
}
This can be useful to add a warning (using the fillText()
method) to the canvas if it doesn't support HDR content.
const hdrCanvasStatus = document.getElementById("hdr-check-status")! as HTMLDivElement;
if (checkHDRCanvas()) {
hdrCanvasStatus.innerText = "HDR Canvas are supported";
hdrCanvasStatus.style.color = "green";
} else {
hdrCanvasStatus.innerText = "HDR Canvas are not supported";
hdrCanvasStatus.style.color = "red";
}
const hdrCanvasStatus = document.getElementById("hdr-check-status")! as HTMLDivElement;
if (checkHDRVideo()) {
hdrCanvasStatus.innerText = "HDR Video is supported";
hdrCanvasStatus.style.color = "green";
} else {
hdrCanvasStatus.innerText = "HDR Video is not supported";
hdrCanvasStatus.style.color = "red";
}
Note: Currently the Chrome flag enable-experimental-web-platform-features
needs to be enabled to have HDR support for the canvas
element. You need to tell your visitors about that.
The HDR canvas
support is activated by initializing a canvas context using the following snippet:
const colorSpace = "rec2100-hlg";
canvas.configureHighDynamicRange({ mode: "extended" });
const ctx = canvas.getContext("2d", {
colorSpace: colorSpace,
colorType: "float16"
// Use this for Chrome < 133
//pixelFormat: "float16"
});
The snippet above is also available as function:
import { initHDRCanvas } from "hdr-canvas";
It's now also possible to use a HDR enabled Canvas by wrapping the browser internal getContext()
function, by calling defaultGetContextHDR()
.
import { defaultGetContextHDR, checkHDR, checkHDRCanvas } from "hdr-canvas";
if (checkHDR() && checkHDRCanvas()) {
defaultGetContextHDR();
console.log("Enabled HDR Canvas");
}
Note: This example wraps the call to defaultGetContextHDR()
into a check (checkHDR() && checkHDRCanvas()
), because calling the function in a browser that isn't HDR-capable will break every subsequent call to getContext()
.
Use the method resetGetContext()
to undo the changes by defaultGetContextHDR()
.
import { resetGetContext } from "hdr-canvas";
resetGetContext();
Afterwards one can use ImageData with a float16
array, first the Uint16Image
needs to be imported:
import { Uint16Image } from "hdr-canvas";
This example assumes image
to be a HTMLImageElement including an existing image.
const offscreen = new OffscreenCanvas(image.width, image.height);
const loadCtx = offscreen.getContext("2d");
loadCtx.drawImage(image, 0, 0);
const imData = loadCtx.getImageData(0, 0, image.width, image.height);
console.log(imData);
var hdrCanvas = document.createElement("canvas");
hdrCanvas.width = image.width;
hdrCanvas.height = image.height;
const rec210hglImage = Uint16Image.fromImageData(imData);
const ctx = initHDRCanvas(hdrCanvas);
ctx.putImageData(rec210hglImage.getImageData(), 0, 0);
Note: Make sure to have Three.js added as a dependency.
This is just a drop-in-replacement for the regular WebGPURenderer
of Three.js.
import HDRWebGPURenderer from "hdr-canvas/three/HDRWebGPURenderer.js";
Note: Starting Three.js 167 the WebGPU renderer is the new default renderer. This has several consequences for the required imports. Use this import instead of the official one and if your using Vite don't provide an import map of resolver alias configuration.
import * as THREE from "three/src/Three.js";
Use it as you'll do with a WebGPURenderer
.
renderer = new HDRWebGPURenderer({ canvas: canvas, antialias: true });
Starting from Three.js version 167 you need to fix imported UHDR Textures, otherwise they will appear black:
model = gltf.scene;
model.traverse((element) => {
if (element?.material?.type != undefined) {
let targetMaterial = new THREE.MeshBasicMaterial();
THREE.MeshBasicMaterial.prototype.copy.call(targetMaterial, element.material);
element.material = targetMaterial;
}
});
scene.add(model);
This currently doesn't work with Firefox, due to missing support for HDR and only partial WebGPU support.
One can import WebGPU
and use also a HDR check to guard from errors:
import WebGPU from "hdr-canvas/three/WebGPU.js";
Only use the provided renderer if the browser supports WebGPU and HDR:
if (WebGPU.isAvailable() && checkHDRCanvas()) {
renderer = new HDRWebGPURenderer({ canvas: canvas, antialias: true });
} else {
renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true });
}
All examples requires a Chromium based browser (like Chrome, Edge, Opera and Brave) and a HDR-enable monitor.
Some of the examples above are also part of this repository.
npm i
npm run dev
Open This URL in your browser: http://localhost:5173/, you can also access them directly from GitHub.
The following things might be improved:
- Change
pixelFormat
inHTMLCanvasElement.getContext("2d")
tocolorType
(["unorm8", "float16"]) while keeping some downward compatibility - #151 - Try to detect change of screen for HDR detection - #107
- Improve
Uint16Image
- Check error "
Failed to construct 'ImageData': Overload resolution failed.
" onImageData
overload - Use
Float16Array
instead ofUint16Array
- Check error "
- Improve speed
- Provide WebWorker
- Documentation
- Link to browser HDR support
- Document
Uint16Image
This section contains some development related notes which might be helpful for reusing or extending the code.
pixelFormat
to colorType
(#151)
As @reitowo pointed out, there has been a change to the getContext("2d")
method. This is currently not implemented
This has been implemented in Chromium 134. Browser type definitions for TypeScript reflecting this change are marked as unimplemeneted
Starting with 137 the ImageData
constructor only acceppts Float16Array
instead of Uint16Array
.
This is currently documented in the WhatWG spec, but not on MDN.
This section contains different definitions, which can be helpful to impkement HDR related things
- Change to the WhatsWG spec - Issue Tracker
- Change apporoved
- MDN picks up spec change - Create an issue
- Changes are popagated to the TypeScript generated DOM library
- Extend
src/types
, try to extend existing interfaces
Starting with version 0.1.0 the prefered way is to update the browser / DOM types instead of adding our own HDR types. The existing types will continue to exist for now. This change reflects improvements in browser support and should make transitioning to standard base types later on.
The basic workflow is as follows:
- Update the definitions
- Regenerate the types (need the MDN submodule to be checked out)
To make this repaetable add patches to this repository:
Edit the types, for example to change existing definitions, see Update the definitions
cd node_modules/@typescript/dom-lib-generator/
vi inputfiles/overridingTypes.jsonc
To generate a patch pmake sure, that the Git submmodule is remove, otherwise patch-package
will fail.
node scripts/git-submodules.js -c -d node_modules/@typescript/dom-lib-generator/
cd ../../..
npx patch-package @typescript/dom-lib-generator