Skip to content

add a regression test for the fractional canvas size - #12293

Open
HuzaifaChaudary wants to merge 1 commit into
chartjs:masterfrom
HuzaifaChaudary:test/retina-scale-fractional-size
Open

add a regression test for the fractional canvas size#12293
HuzaifaChaudary wants to merge 1 commit into
chartjs:masterfrom
HuzaifaChaudary:test/retina-scale-fractional-size

Conversation

@HuzaifaChaudary

Copy link
Copy Markdown

while looking at #12269 i went to find why 4.5.1 behaves differently from 4.5.0 on a responsive chart and it comes down to retinaScale.

in 4.5.1 the canvas dimensions are written straight from round1:

const deviceHeight = round1(chart.height * pixelRatio);
const deviceWidth = round1(chart.width * pixelRatio);
...
if (chart.currentDevicePixelRatio !== pixelRatio
    || canvas.height !== deviceHeight
    || canvas.width !== deviceWidth) {
  canvas.height = deviceHeight;
  canvas.width = deviceWidth;

canvas.width and canvas.height are unsigned long so the browser truncates whatever is assigned to them. when the container measures to a fraction the stored value can never equal the value it is compared against. the check is then true on every call and retinaScale keeps reporting that the size changed.

i measured it in chrome headless:

canvas.width = 600.8            -> 600
4.5.1 logic on 300.4 x 150.2    -> true true true
master on the same size         -> true false false

this matters because _resize uses the return value as its early exit:

if (!retinaScale(this, newRatio, true)) {
  return;
}

so on 4.5.1 a fractional size makes every attach and every resize fall through to _doResize instead of stopping there. that is the path in the stack trace on #12269.

#12142 already fixed this by flooring the canvas dimensions but it landed without a test and it is not in a release yet. this adds the missing guard so the behaviour does not slip back. putting the floor back out makes the new test fail with Expected true to be false on the second call.

no source change here.

retinaScale kept reporting a change when the chart size had a decimal in it. the canvas stores whole pixels only so the value written back never matched the value it was compared against and every resize looked like a real one. the floor added in 12142 fixed that but nothing guarded it so this pins the second call returning false.
Copilot AI lite review requested due to automatic review settings August 30, 2026 16:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants