Skip to content

Commit

Permalink
fix(fabric.Canvas): Update backgroundImage and overlayImage coordinat…
Browse files Browse the repository at this point in the history
…es on zoom change (fabricjs#6777)
  • Loading branch information
asturur authored and shanicerae committed Jan 16, 2021
1 parent 48163cf commit b8d0899
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"nyc": "^15.1.0",
"onchange": "^3.x.x",
"pixelmatch": "^4.0.2",
"qunit": "2.9.2",
"qunit": "^2.13.0",
"testem": "^3.2.0",
"uglify-js": "3.3.x"
},
Expand Down
11 changes: 10 additions & 1 deletion src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,10 @@
* @chainable true
*/
setViewportTransform: function (vpt) {
var activeObject = this._activeObject, object, i, len;
var activeObject = this._activeObject,
backgroundObject = this.backgroundImage,
overlayObject = this.overlayImage,
object, i, len;
this.viewportTransform = vpt;
for (i = 0, len = this._objects.length; i < len; i++) {
object = this._objects[i];
Expand All @@ -685,6 +688,12 @@
if (activeObject) {
activeObject.setCoords();
}
if (backgroundObject) {
backgroundObject.setCoords(true);
}
if (overlayObject) {
overlayObject.setCoords(true);
}
this.calcViewportBoundaries();
this.renderOnAddRemove && this.requestRenderAll();
return this;
Expand Down
19 changes: 19 additions & 0 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,25 @@
canvas.viewportTransform = fabric.StaticCanvas.prototype.viewportTransform;
});

QUnit.test('setViewportTransform calls objects setCoords', function(assert) {
var vpt = [2, 0, 0, 2, 50, 50];
assert.deepEqual(canvas.viewportTransform, [1, 0, 0, 1, 0, 0], 'initial viewport is identity matrix');
var rect = new fabric.Rect({ width: 10, heigth: 10 });
var rectBg = new fabric.Rect({ width: 10, heigth: 10 });
var rectOverlay = new fabric.Rect({ width: 10, heigth: 10 });
canvas.add(rect);
canvas.cancelRequestedRender();
canvas.backgroundImage = rectBg;
canvas.overlayImage = rectOverlay;
assert.deepEqual(rect.lineCoords.tl, new fabric.Point(0,0), 'rect linecoords are set for normal viewport');
assert.equal(rectBg.lineCoords, undefined, 'rectBg linecoords are not set');
assert.equal(rectOverlay.lineCoords, undefined, 'rectOverlay linecoords are not set');
canvas.setViewportTransform(vpt);
assert.deepEqual(rect.lineCoords.tl, new fabric.Point(50,50), 'rect linecoords are set');
assert.deepEqual(rectBg.lineCoords.tl, new fabric.Point(0,0), 'rectBg linecoords are set');
assert.deepEqual(rectOverlay.lineCoords.tl, new fabric.Point(0,0), 'rectOverlay linecoords are set');
});

QUnit.test('getZoom', function(assert) {
assert.ok(typeof canvas.getZoom === 'function');
var vpt = [2, 0, 0, 2, 50, 50];
Expand Down

0 comments on commit b8d0899

Please sign in to comment.