Skip to content

Commit

Permalink
fix(): _initRetinaScaling initializing the scaling regardless of re…
Browse files Browse the repository at this point in the history
…tina settings in Canvas class. (fabricjs#8565)
  • Loading branch information
ShaMan123 authored Jan 5, 2023
1 parent 70f955c commit 0bc63dc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(): `_initRetinaScaling` initializaing the scaling regardless of settings in Canvas. [#8565](https://github.com/fabricjs/fabric.js/pull/8565)
- fix(): regression of canvas migration with pointer and sendPointToPlane [#8563](https://github.com/fabricjs/fabric.js/pull/8563)
- chore(TS): Use exports from files to build fabricJS, get rid of HEADER.js [#8549](https://github.com/fabricjs/fabric.js/pull/8549)
- chore(): rm `fabric.filterBackend` => `getFilterBackend` [#8487](https://github.com/fabricjs/fabric.js/pull/8487)
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/canvas.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export class SelectableCanvas<
this._createUpperCanvas();
// @ts-ignore
this._initEventListeners();
this._initRetinaScaling();
this._isRetinaScaling() && this._initRetinaScaling();
this.calcOffset();
this._createCacheCanvas();
}
Expand Down
7 changes: 2 additions & 5 deletions src/canvas/static_canvas.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export class StaticCanvas<
this.renderAndResetBound = this.renderAndReset.bind(this);
this.requestRenderAllBound = this.requestRenderAll.bind(this);
this._initStatic(el, options);
this._initRetinaScaling();
this._isRetinaScaling() && this._initRetinaScaling();
this.calcViewportBoundaries();
}

Expand Down Expand Up @@ -382,9 +382,6 @@ export class StaticCanvas<
* @private
*/
_initRetinaScaling() {
if (!this._isRetinaScaling()) {
return;
}
this.__initRetinaScaling(this.lowerCanvasEl, this.contextContainer);
}

Expand Down Expand Up @@ -536,7 +533,7 @@ export class StaticCanvas<
}
});

this._initRetinaScaling();
this._isRetinaScaling() && this._initRetinaScaling();
this.calcOffset();

if (!cssOnly) {
Expand Down
62 changes: 35 additions & 27 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -1958,36 +1958,44 @@
assert.equal(aGroup._objects[3], circle2);
});

QUnit.test('set dimensions', async function (assert) {
var el = fabric.getDocument().createElement('canvas'),
parentEl = fabric.getDocument().createElement('div');
el.width = 200; el.height = 200;
parentEl.className = 'rootNode';
parentEl.appendChild(el);

fabric.config.configure({ devicePixelRatio: 1.25 });

assert.equal(parentEl.firstChild, el, 'canvas should be appended at partentEl');
assert.equal(parentEl.childNodes.length, 1, 'parentEl has 1 child only');

el.style.position = 'relative';
var elStyle = el.style.cssText;
assert.equal(elStyle, 'position: relative;', 'el style should not be empty');

var canvas = new fabric.Canvas(el, { enableRetinaScaling: true, renderOnAddRemove: false });

canvas.setDimensions({ width: 500, height: 500 });
assert.equal(canvas._originalCanvasStyle, elStyle, 'saved original canvas style for disposal');
assert.notEqual(el.style.cssText, canvas._originalCanvasStyle, 'canvas el style has been changed');

await canvas.dispose();
assert.equal(canvas._originalCanvasStyle, undefined, 'removed original canvas style');
assert.equal(el.style.cssText, elStyle, 'restored original canvas style');
assert.equal(el.width, 500, 'restored width');
assert.equal(el.height, 500, 'restored height');
[true, false].forEach(enableRetinaScaling => {
QUnit.test(`set dimensions, enableRetinaScaling ${enableRetinaScaling}`, async function (assert) {
var el = fabric.getDocument().createElement('canvas'),
parentEl = fabric.getDocument().createElement('div');
el.width = 200; el.height = 200;
parentEl.className = 'rootNode';
parentEl.appendChild(el);

const dpr = 1.25;
fabric.config.configure({ devicePixelRatio: dpr });

assert.equal(parentEl.firstChild, el, 'canvas should be appended at partentEl');
assert.equal(parentEl.childNodes.length, 1, 'parentEl has 1 child only');

el.style.position = 'relative';
var elStyle = el.style.cssText;
assert.equal(elStyle, 'position: relative;', 'el style should not be empty');

var canvas = new fabric.Canvas(el, { enableRetinaScaling, renderOnAddRemove: false });

canvas.setDimensions({ width: 500, height: 500 });
assert.equal(canvas._originalCanvasStyle, elStyle, 'saved original canvas style for disposal');
assert.notEqual(el.style.cssText, canvas._originalCanvasStyle, 'canvas el style has been changed');
assert.equal(el.width, 500 * (enableRetinaScaling ? dpr : 1), 'expected width');
assert.equal(el.height, 500 * (enableRetinaScaling ? dpr : 1), 'expected height');
assert.equal(canvas.upperCanvasEl.width, 500 * (enableRetinaScaling ? dpr : 1), 'expected width');
assert.equal(canvas.upperCanvasEl.height, 500 * (enableRetinaScaling ? dpr : 1), 'expected height');

await canvas.dispose();
assert.equal(canvas._originalCanvasStyle, undefined, 'removed original canvas style');
assert.equal(el.style.cssText, elStyle, 'restored original canvas style');
assert.equal(el.width, 500, 'restored width');
assert.equal(el.height, 500, 'restored height');

});
});


QUnit.test('clone', function(assert) {
var done = assert.async();
assert.ok(typeof canvas.clone === 'function');
Expand Down

0 comments on commit 0bc63dc

Please sign in to comment.