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
srgb
checking
srgb-linear
checking
Show the code srgb-linear context, blurred glow pass and overlapping draws
- The neon buttons. Four
drawablebuttons sit directly inside each canvas. - Opening the linear context. Pass
colorSpaceand read backgetContextAttributes().colorSpace. - 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
Show the code registered --tint property, contrast-color() and tinted backdrop
- The card styles.
color: contrast-color(var(--tint))flips text and borders between black and white. - Registering
--tint. Giving itsyntax: '<color>'resolves the colour on the card. - 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
- Side-by-side drawing. Set
ctx.langforfillText()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
- Not verified yet in headless Chromium or on Android Chrome 159. The live checks above measure your browser directly when this page opens.
- To verify linear-light blending: check that
getContext('2d', { colorSpace: 'srgb-linear' })and'display-p3-linear'report those strings ingetContextAttributes().colorSpace, and that overlapping red and cyandrawElementImage()calls measure brighter insrgb-linearthan insrgb(around 188 vs 128 in sRGB 8-bit). - To verify
contrast-color(): check thatCSS.supports('color', 'contrast-color(red)')is true and that dragging the lightness slider or tapping Yellow (50%) vs Blue (50%) flips the drawn card's text between black and white. - To verify
ctx.lang: check that'lang' in CanvasRenderingContext2D.prototypeis true and compare the glyph strokes of直 骨 海 角 刃 令betweenctx.fillText()anddrawElementImage()on a system that has distinct Japanese, Simplified Chinese and Traditional Chinese fonts installed.