Skip to content
Writing
Engineering
· · 3 min

A screenshot that isn't a screenshot

listen to this article from the Wonder Why? podcast
Listen

In October 2024, whilst working on Scoutbar, I ended up maintaining a fork of .

What started as an attempt to fix a few rendering inconsistencies eventually became an open-source package that found its way into , a session replay and product analytics platform with over 12,000 GitHub stars.

Apparently, I wasn't the only person having trouble taking a bloody screenshot.

There's something particularly misleading about the premise of html2canvas. It doesn't actually take a screenshot. It traverses the document, interprets its computed styles and attempts to reconstruct what the browser has already rendered.

The distinction is important. Taking a screenshot requires access to pixels. Reconstructing one requires an understanding of how those pixels came to exist.

You're effectively implementing a small portion of a browser's rendering engine inside another browser.

The mathematics of a colour

One of the problems I encountered was unsupported CSS colour functions.

The browser could perfectly happily render an colour whilst html2canvas would encounter the same colour and throw an exception.

Supporting OKLCH requires considerably more than recognising another string format. OKLCH describes colour through lightness, and hue. The renderer needs to translate that representation into RGB channel values.

The first operation converts polar coordinates into Cartesian coordinates:

const oklchToOklab = (L, C, h) => {
    const a = C * Math.cos((h * Math.PI) / 180);
    const b = C * Math.sin((h * Math.PI) / 180);

    return [L, a, b];
};

Then comes a matrix transformation, a cubic operation and another matrix transformation into linear .

const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
const s_ = L - 0.0894841775 * a - 1.291485548 * b;

const l = l_ ** 3;
const m = m_ ** 3;
const s = s_ ** 3;

return [
    4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
   -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
   -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s
];

The resulting channels represent linear light. They still need to pass through the before being quantised.

The browser · oklch(0.70 0.250 30)
The conversion · rgb(255 65 45)
1 channel outside sRGB · clipped
oklch0.700.2530.00
polar →
oklab0.7000.2170.125
matrix →
lms′0.8130.6690.519
cube →
lms0.5370.3000.140
matrix →
linear1.2300.0530.026
transfer →
sRGB1.0950.2560.175
round →
8-bit2556545
r
1.23
g
0.05
b
0.03
Drag the chroma up. When a linear channel leaves 0 to 1, it's clipped, and the right swatch stops being the colour asked for.

There's an interesting limitation here. Not every colour representable in OKLCH is representable in sRGB. Converting a sufficiently saturated colour can produce negative channels or values greater than one.

Clipping those channels produces a representable colour, but not necessarily the .

My implementation also recognised several wider-gamut colour spaces without properly converting between their primaries. Supporting the syntax of a colour function and supporting the colour space itself are two very different accomplishments.

When two rendering systems disagree

In a subsequent patch, I encountered a particularly interesting incompatibility between CSS gradients and Canvas gradients.

This is perfectly valid CSS:

background: linear-gradient(
    90deg,
    #eb645a -20%,
    #3669eb 120%
);

Canvas, however, requires the offsets passed to to exist between zero and one.

My original fix was fairly unremarkable:

gradient.addColorStop(
    Math.min(1, Math.max(0, colorStop.stop)),
    asString(colorStop.color)
);

It prevented an exception. It didn't necessarily preserve the gradient.

Consider the interpolation parameter at the beginning of the visible gradient. With stops at −20% and 120%, we're already approximately 14% of the way through the interpolation when we reach the first visible pixel.

Clamping the first stop to zero changes that parameter to zero.

left edge at t = 0.00
canvas
the browser's CSS
Not recommended: Clamp the stops
left edge at t = 0.14
canvas
the browser's CSS
Recommended: Extend the line, remap the stops
Drag the stops past the edges. Clamping keeps canvas happy and changes the gradient; extending the line keeps both.

For a simple linear gradient, a more faithful solution is to extend the drawing geometry and remap the stops into that extended coordinate system.

The pixels outside the element's bounds can then be clipped without altering the interpolation inside them.

The difference between a successful render and a correct render can be a single line of code.

The architecture of partial correctness

Several of my changes followed a similar pattern.

When an unsupported image function was encountered, instead of throwing an exception, I introduced an explicit representation for it.

const FALLBACK_IMAGE: UnsupportedImage = {
    type: CSSImageType.UNSUPPORTED
};

Unsupported transformations returned an identity matrix.

return [1, 0, 0, 1, 0, 0];

An unsupported colour returned a fallback colour.

These weren't implementations of the missing functionality. They were decisions about what the renderer should do in its absence.

SecuritySigned in as ada@studio.dev
Two-factor authentication
Save changes
What the browser drew
Feedback · rejectsRegression · rejects
SecuritySigned in as ada@studio.dev
Two-factor authentication
Save changes
  • transform translateX(16px)→ identity
What the reconstruction drew
Pick what the renderer can't reproduce. A missing image is a cosmetic loss; a missing transform turns the switch off.

There's a legitimate trade-off here. A screenshot of a document missing a background image might still be useful. A screenshot missing a transformation that determines the position of an important element might be completely misleading.

Today, I'd make these compromises visible to the consuming application through structured rendering diagnostics. Not just a canvas, but a canvas accompanied by an account of what couldn't be reproduced.

That way, a feedback widget could accept an approximate rendering whilst a visual regression test could reject the same result.

The interesting thing about reconstructing a browser's output is discovering just how many guarantees the browser ordinarily makes on our behalf.

I started out wanting an image of a webpage.

I ended up thinking about colour spaces, affine transformations, interpolation domains and what it actually means for a rendering operation to succeed.

— Adenekan

Thoughts on this one?

Tell me what it made you think, or where you think I got it wrong.

Still working on the last 10%.0:00pm in london