npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hdr-canvas

v0.0.9

Published

HDR capable HTML canvas

Downloads

1,068

Readme

hdr-canvas

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!

Especially operations on the ImageData arrays are not optimized, e.g. quite slow.

Feature detection

Import the required function(s):

import { checkHDR, checkHDRCanvas } from "hdr-canvas";

Examples checkHDRCanvas()

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.

Example checkHDRCanvas()

if (checkHDRCanvas()) {
  hdrCanvas.innerText = "HDR Canvas are supported";
  hdrCanvas.style.color = "green";
} else {
  hdrCanvas.innerText = "HDR Canvas are not supported";
  hdrCanvas.style.color = "red";
}

Canvas

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,
  pixelFormat: "float16",
});

Canvas setup

The snippet above is also available as function:

import { initHDRCanvas } from "hdr-canvas";

Importing Uint16Image

Afterwards one can use ImageData with a float16 array, first the Uint16Image needs to be imported:

import { Uint16Image } from "hdr-canvas";

Example: Loading an image

Thisexample 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);

Three.js WebGPU

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";

Use it as you'll do with a WebGPURenderer.

renderer = new HDRWebGPURenderer({ canvas: canvas, antialias: true });

Example

See this blog post for an example in action, requires a Chromium based browser (like Chrome, Edge, Opera and Brave) and a HDR-enable monitor.


TODO

The following things might be improved:

  • Try to detect change of screen for HDR detection
  • Improve speed
    • Provide WebWorker
  • Documentation
    • Link to browser HDR support
    • Document Uint16Image