v155 · canvas color spaces
Detection traps
Feature-detecting an enum value is different from feature-detecting an API, and two obvious-looking probes give the wrong answer. WebIDL ignores unknown dictionary keys but throws on unknown enum values — and a probe that never reads the result back can report support the backing store does not have. Run each probe below against your real browser.
Migration guidance: wrap context creation in try/catch, fall back to "srgb", and verify with getContextAttributes().colorSpace. Blink flag on pre-155 builds: --enable-blink-features=ColorSpacePredefinedLinearSpaces. The related rec2100-linear value is a separate, still-experimental feature (ColorSpaceRec2100Linear) — do not bundle the two in one check.
Three probes, one truth
wrong "The property exists, so the values must work"
'colorSpace' in ctx.getContextAttributes() // true since Chrome 94
The attribute has existed since the wide-gamut canvas shipped. Its existence says nothing about which enum values this browser accepts — this probe passes on browsers that reject every linear space.
wrong "Unknown dictionary members are ignored, so just pass it"
// Unknown KEYS are ignored… but colorSpace's TYPE is an enum,
// and an unknown enum VALUE throws. These two lines behave differently:
canvas.getContext('2d', { colourSpaice: 'srgb-linear' }) // typo key: ignored
canvas.getContext('2d', { colorSpace: 'made-up-space' }) // enum value: THROWS
Code written on the "dictionaries ignore what they don't know" assumption crashes at runtime the moment it meets the enum. This is why the naive "pass it and see" pattern needs the try/catch.
right try, catch, and read the answer back
function supportsCanvasColorSpace(space) {
try {
const ctx = document.createElement('canvas')
.getContext('2d', { colorSpace: space });
return ctx.getContextAttributes().colorSpace === space;
} catch {
return false; // unknown enum value → TypeError
}
}
| value | supported |
|---|---|
| not run yet | |
The catch handles the enum throw; the readback catches the other failure mode — an implementation that tolerates the string but silently substitutes another space would fail the equality, not slip through.