Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid resize loop when browser zoom is set to 90% #10971

Merged
merged 10 commits into from Dec 17, 2022
4 changes: 2 additions & 2 deletions src/core/core.controller.js
Expand Up @@ -295,8 +295,8 @@ class Chart {
const newRatio = options.devicePixelRatio || this.platform.getDevicePixelRatio();
const mode = this.width ? 'resize' : 'attach';

this.width = newSize.width;
this.height = newSize.height;
this.width = Math.floor(newSize.width);
this.height = Math.floor(newSize.height);
this._aspectRatio = this.aspectRatio;
if (!retinaScale(this, newRatio, true)) {
return;
Expand Down
5 changes: 1 addition & 4 deletions src/helpers/helpers.dom.ts
Expand Up @@ -188,7 +188,7 @@ export function getMaximumSize(
height -= paddings.height + borders.height;
}
width = Math.max(0, width - margins.width);
height = Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height - margins.height);
height = Math.max(0, aspectRatio ? width / aspectRatio : height - margins.height);
width = round1(Math.min(width, maxWidth, containerSize.maxWidth));
height = round1(Math.min(height, maxHeight, containerSize.maxHeight));
if (width && !height) {
Expand Down Expand Up @@ -222,9 +222,6 @@ export function retinaScale(
const deviceHeight = Math.floor(chart.height * pixelRatio);
kurkle marked this conversation as resolved.
Show resolved Hide resolved
const deviceWidth = Math.floor(chart.width * pixelRatio);

chart.height = deviceHeight / pixelRatio;
chart.width = deviceWidth / pixelRatio;

const canvas = chart.canvas;

// If no style has been set on the canvas, the render size is used as display size,
Expand Down
41 changes: 41 additions & 0 deletions test/specs/helpers.dom.tests.js
Expand Up @@ -254,6 +254,30 @@ describe('DOM helpers tests', function() {
expect(canvas.style.width).toBe('400px');
});

it ('should handle devicePixelRatio correctly', function() {
const chartWidth = 800;
const chartHeight = 400;
let devicePixelRatio = 0.8999999761581421; // 1.7999999523162842;
var chart = window.acquireChart({}, {
canvas: {
width: chartWidth,
height: chartHeight,
}
});

helpers.retinaScale(chart, devicePixelRatio, true);

var canvas = chart.canvas;
expect(canvas.width).toBe(Math.floor(chartWidth * devicePixelRatio));
expect(canvas.height).toBe(Math.floor(chartHeight * devicePixelRatio));

expect(chart.width).toBe(chartWidth);
expect(chart.height).toBe(chartHeight);

expect(canvas.style.width).toBe(`${chartWidth}px`);
expect(canvas.style.height).toBe(`${chartHeight}px`);
});

describe('getRelativePosition', function() {
it('should use offsetX/Y when available', function() {
const event = {offsetX: 50, offsetY: 100};
Expand Down Expand Up @@ -504,4 +528,21 @@ describe('DOM helpers tests', function() {

document.body.removeChild(container);
});

it('should round non-integer container dimensions', () => {
const container = document.createElement('div');
container.style.width = '799.999px';
container.style.height = '299.999px';

document.body.appendChild(container);

const target = document.createElement('div');
target.style.width = '200px';
target.style.height = '100px';
container.appendChild(target);

expect(helpers.getMaximumSize(target, undefined, undefined, 2)).toEqual(jasmine.objectContaining({width: 800, height: 400}));

document.body.removeChild(container);
});
});