Skip to content

fix: compare charts drawn on an OffscreenCanvas - #45

Merged
kurkle merged 1 commit into
chartjs:masterfrom
kurkle:fix/offscreen-canvas
Nov 24, 2026
Merged

kurkle merged 1 commit into
chartjs:masterfrom
kurkle:fix/offscreen-canvas

Conversation

@kurkle

@kurkle kurkle commented Nov 24, 2026

Copy link
Copy Markdown
Member

What was wrong

toEqualImageData resolved its input only as a CanvasRenderingContext2D, an HTMLCanvasElement, or a chart whose ctx was a CanvasRenderingContext2D. A chart acquired with acquireChart(config, {useOffscreenCanvas: true}) draws on an OffscreenCanvas, so its fixture failed with "Input value is not a valid image source." before any pixel was compared. Chart.js's controller.line/point-style-offscreen-canvas fixture hits this; under 0.x the matcher checked instanceof Chart and compared such charts. toBeValidChart rejected them too, since it required [object HTMLCanvasElement] and [object CanvasRenderingContext2D].

What changed

  • src/matchers.js: both matchers also accept OffscreenCanvas and OffscreenCanvasRenderingContext2D. They are recognised by their Object.prototype.toString tags, as toBeValidChart already did, so a browser without OffscreenCanvas does not throw a ReferenceError.
  • src/fixtures.js: when reference images are updated, an OffscreenCanvas (which has no toDataURL) is copied onto a plain canvas with drawImage first. The copy is exact: in both browsers its getImageData is byte for byte the same as the offscreen context's.
  • New fixture basic/bar-offscreen and two browser specs. The fixture has its own reference image: Chromium rasterizes an OffscreenCanvas slightly differently from an onscreen canvas (4887 of 262144 bytes differ for the same bar chart, along the bar edges), while Firefox renders both the same.
  • README: notes that the matchers accept offscreen charts, and that an offscreen fixture needs its own image.

Failing test before the fix

On 12417a7 with the new fixture and specs, npm run test:browser:

 FAIL  |chromium| test/specs/fixtures.spec.js > basic > basic/bar-offscreen
 FAIL  |chromium| test/specs/utils.spec.js > charts on an OffscreenCanvas > should be valid charts
 FAIL  |chromium| test/specs/utils.spec.js > charts on an OffscreenCanvas > should compare as a chart, a canvas and a context
 FAIL  |firefox| (the same three)
Error: Expected canvas to be an instance of HTMLCanvasElement
Error: Input value is not a valid image source.
      Tests  6 failed | 24 passed | 2 skipped (32)

After the fix, npm test passes: 30 passed, 2 skipped (the existing spec that checks the skip of an unsupported feature, once per browser).

basic/bar-offscreen.png was written by npm run fixtures:update through the new copy path.

How to try it

npm ci
npm run test:browser

toEqualImageData accepted only a CanvasRenderingContext2D, an
HTMLCanvasElement or a chart whose context was one, so a chart acquired
with `useOffscreenCanvas: true` failed with "Input value is not a valid
image source." before any pixel was compared. 0.x checked `instanceof
Chart` and passed such charts on. toBeValidChart rejected them as well.

Both matchers now also accept an OffscreenCanvas and its
OffscreenCanvasRenderingContext2D, recognised by their tags so that a
browser without OffscreenCanvas does not throw. Updating a reference
image copies an OffscreenCanvas onto a plain canvas first, since it has
no toDataURL.
@kurkle
kurkle marked this pull request as ready for review November 24, 2026 03:59
@kurkle
kurkle requested review from etimberg and a balanced review from Copilot and removed request for Copilot November 24, 2026 13:15
@kurkle
kurkle merged commit f92f8cf into chartjs:master Nov 24, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kurkle
kurkle deleted the fix/offscreen-canvas branch November 24, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants