Skip to content

Commit 9306d7f

Browse files
authored
fix: avoid resize loop when browser zoom is set to 90% (#10971)
* test: new test to reproduce issue #10951 * test: validate the canvas style too * fix: Avoid reassigning the the chart size. For specific values of pixelRatio the assignment would cause the size to reduce by 1px. Since it's called from the ResizeObserver it will be stuck in a loop that constantly reduce the size of the chart and canvas. * Revert "fix: Avoid reassigning the the chart size. For specific values of pixelRatio the assignment would cause the size to reduce by 1px. Since it's called from the ResizeObserver it will be stuck in a loop that constantly reduce the size of the chart and canvas." This reverts commit ed7a348. * fix: Avoid the resize loop by fixing the rounding error in the retinaScale function. * fix: getMaximumSize was flooring non-integer height values unnecessarily. * Revert "fix: Avoid the resize loop by fixing the rounding error in the retinaScale function." This reverts commit 23525ab. * fix: Avoid the resize loop by fixing the rounding error in the retinaScale function.
1 parent 64a0278 commit 9306d7f

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/helpers/helpers.dom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export function getMaximumSize(
188188
height -= paddings.height + borders.height;
189189
}
190190
width = Math.max(0, width - margins.width);
191-
height = Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height - margins.height);
191+
height = Math.max(0, aspectRatio ? width / aspectRatio : height - margins.height);
192192
width = round1(Math.min(width, maxWidth, containerSize.maxWidth));
193193
height = round1(Math.min(height, maxHeight, containerSize.maxHeight));
194194
if (width && !height) {
@@ -222,8 +222,8 @@ export function retinaScale(
222222
const deviceHeight = Math.floor(chart.height * pixelRatio);
223223
const deviceWidth = Math.floor(chart.width * pixelRatio);
224224

225-
chart.height = deviceHeight / pixelRatio;
226-
chart.width = deviceWidth / pixelRatio;
225+
chart.height = Math.floor(chart.height);
226+
chart.width = Math.floor(chart.width);
227227

228228
const canvas = chart.canvas;
229229

test/specs/helpers.dom.tests.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,30 @@ describe('DOM helpers tests', function() {
254254
expect(canvas.style.width).toBe('400px');
255255
});
256256

257+
it ('should handle devicePixelRatio correctly', function() {
258+
const chartWidth = 800;
259+
const chartHeight = 400;
260+
let devicePixelRatio = 0.8999999761581421; // 1.7999999523162842;
261+
var chart = window.acquireChart({}, {
262+
canvas: {
263+
width: chartWidth,
264+
height: chartHeight,
265+
}
266+
});
267+
268+
helpers.retinaScale(chart, devicePixelRatio, true);
269+
270+
var canvas = chart.canvas;
271+
expect(canvas.width).toBe(Math.floor(chartWidth * devicePixelRatio));
272+
expect(canvas.height).toBe(Math.floor(chartHeight * devicePixelRatio));
273+
274+
expect(chart.width).toBe(chartWidth);
275+
expect(chart.height).toBe(chartHeight);
276+
277+
expect(canvas.style.width).toBe(`${chartWidth}px`);
278+
expect(canvas.style.height).toBe(`${chartHeight}px`);
279+
});
280+
257281
describe('getRelativePosition', function() {
258282
it('should use offsetX/Y when available', function() {
259283
const event = {offsetX: 50, offsetY: 100};
@@ -504,4 +528,21 @@ describe('DOM helpers tests', function() {
504528

505529
document.body.removeChild(container);
506530
});
531+
532+
it('should round non-integer container dimensions', () => {
533+
const container = document.createElement('div');
534+
container.style.width = '799.999px';
535+
container.style.height = '299.999px';
536+
537+
document.body.appendChild(container);
538+
539+
const target = document.createElement('div');
540+
target.style.width = '200px';
541+
target.style.height = '100px';
542+
container.appendChild(target);
543+
544+
expect(helpers.getMaximumSize(target, undefined, undefined, 2)).toEqual(jasmine.objectContaining({width: 800, height: 400}));
545+
546+
document.body.removeChild(container);
547+
});
507548
});

0 commit comments

Comments
 (0)