Linear-light colour spaces, contrast-color() and ctx.lang

I wanted to test three newer colour and text APIs alongside drawElementImage(): linear-light 2D canvas colour spaces (srgb-linear and display-p3-linear), CSS contrast-color() driven by a canvas tint, and CanvasTextDrawingStyles.lang next to drawn CJK DOM text. Drag the sliders, click the neon badges and switch the language to see what your browser supports.

1. Linear-light blending: srgb vs srgb-linear

Standard srgb checking
Linear srgb-linear checking

 

Show the code srgb-linear context, blurred glow pass and overlapping draws
  1. The neon buttons. Four drawable buttons sit directly inside each canvas.
  2. Opening the linear context. Pass colorSpace and read back getContextAttributes().colorSpace.
  3. On paint. Draw a blurred halo pass, then the semi-transparent overlapping controls with explicit sizes.

2. CSS contrast-color() driven by a canvas tint

Adaptive contrast card

This text colour comes from contrast-color(var(--tint)) in CSS. No JavaScript luminance math runs here.

color: checking

 

Show the code registered --tint property, contrast-color() and tinted backdrop
  1. The card styles. color: contrast-color(var(--tint)) flips text and borders between black and white.
  2. Registering --tint. Giving it syntax: '<color>' resolves the colour on the card.
  3. Painting the backdrop. Read --tint, paint the tinted backdrop and draw the card centred.

3. CanvasTextDrawingStyles.lang alongside drawn DOM

直 骨 海 角 刃 令

 

Show the code ctx.lang compared with drawElementImage() on a lang-tagged span
  1. Side-by-side drawing. Set ctx.lang for fillText() and draw <span lang> with the same font right beside it.

How it works

In an srgb canvas, alpha compositing and ctx.filter = 'blur(...)' average gamma-encoded channel values. Half red plus half cyan ends up at sRGB 0.5, which is only about 21% physical light, so the overlap looks dark and muddy. When the canvas uses srgb-linear or display-p3-linear, the same 50% mix averages linear light and encodes to about 0.73 sRGB, so blurred glows and overlapping neon controls stay bright. Switching between srgb-linear and display-p3-linear replaces the right canvas and calls window.__gfx?.retire() on the old one because a canvas can't change its context attributes after creation.

For the contrast demo, CSS.registerProperty() defines --tint with syntax: '<color>'. The sliders update --tint on the drawable card and request a paint. The canvas paints a gradient backdrop from that colour and draws the card on top, while CSS contrast-color(var(--tint)) picks black or white text based on relative luminance (yellow at 50% lightness flips to black while blue at 50% lightness stays white).

In the third demo, ctx.lang tells Canvas 2D which regional font variant to pick when shaping Han characters that share a Unicode code point across Japanese, Simplified Chinese and Traditional Chinese (直 骨 海 角 刃 令). The canvas copies the computed font size and family from the <span>, draws with ctx.fillText(), and calls drawElementImage() on the <span lang="..."> element right beside it so you can compare strokes directly.

Live checks

These run on small fixtures in the corner of the window. Any pixel inspection copies the drawable canvas into a plain 2D canvas first so a drawable canvas is never read back directly.

Findings