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

strokeUniform and cache canvas dimensions #5626

Merged
merged 6 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mixins/object_geometry.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@

/*
* Calculate object bounding box dimensions from its properties scale, skew.
* @param {Number} skewX, a value to override current skewX
* @param {Number} skewY, a value to override current skewY
* @private
* @return {Object} .x width dimension
* @return {Object} .y height dimension
Expand Down
30 changes: 15 additions & 15 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,20 +734,20 @@
*/
_getCacheCanvasDimensions: function() {
var objectScale = this.getTotalObjectScaling(),
dim = this._getNonTransformedDimensions(),
zoomX = objectScale.scaleX,
zoomY = objectScale.scaleY,
width = dim.x * zoomX,
height = dim.y * zoomY;
// caculate dimensions without skewing
dim = this._getTransformedDimensions(0, 0),
neededX = dim.x * objectScale.scaleX / this.scaleX,
neededY = dim.y * objectScale.scaleY / this.scaleY;
return {
// for sure this ALIASING_LIMIT is slightly crating problem
// in situation in wich the cache canvas gets an upper limit
width: width + ALIASING_LIMIT,
height: height + ALIASING_LIMIT,
zoomX: zoomX,
zoomY: zoomY,
x: dim.x,
y: dim.y
// for sure this ALIASING_LIMIT is slightly creating problem
// in situation in which the cache canvas gets an upper limit
// also objectScale contains already scaleX and scaleY
width: neededX + ALIASING_LIMIT,
height: neededY + ALIASING_LIMIT,
zoomX: objectScale.scaleX,
zoomY: objectScale.scaleY,
x: neededX,
y: neededY
};
},

Expand Down Expand Up @@ -796,8 +796,8 @@
this._cacheContext.setTransform(1, 0, 0, 1, 0, 0);
this._cacheContext.clearRect(0, 0, canvas.width, canvas.height);
}
drawingWidth = dims.x * zoomX / 2;
drawingHeight = dims.y * zoomY / 2;
drawingWidth = dims.x / 2;
drawingHeight = dims.y / 2;
this.cacheTranslationX = Math.round(canvas.width / 2 - drawingWidth) + drawingWidth;
this.cacheTranslationY = Math.round(canvas.height / 2 - drawingHeight) + drawingHeight;
this.cacheWidth = width;
Expand Down
15 changes: 14 additions & 1 deletion test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,20 @@
object.scaleX = 2;
object.scaleY = 3;
dims = object._getCacheCanvasDimensions();
assert.deepEqual(dims, { width: 26, height: 38, zoomX: 2, zoomY: 3, x: 12, y: 12 }, 'cache is as big as the scaled object');
assert.deepEqual(dims, { width: 26, height: 38, zoomX: 2, zoomY: 3, x: 24, y: 36 }, 'cache is as big as the scaled object');
});

QUnit.test('_getCacheCanvasDimensions and strokeUniform', function(assert) {
var object = new fabric.Object({ width: 10, height: 10, strokeWidth: 2 });
var dims = object._getCacheCanvasDimensions();
assert.deepEqual(dims, { width: 14, height: 14, zoomX: 1, zoomY: 1, x: 12, y: 12 }, 'if no scaling is applied cache is as big as object + strokeWidth');
object.strokeUniform = true;
var dims = object._getCacheCanvasDimensions();
assert.deepEqual(dims, { width: 14, height: 14, zoomX: 1, zoomY: 1, x: 12, y: 12 }, 'if no scaling is applied strokeUniform makes no difference');
object.scaleX = 2;
object.scaleY = 3;
dims = object._getCacheCanvasDimensions();
assert.deepEqual(dims, { width: 24, height: 34, zoomX: 2, zoomY: 3, x: 22, y: 32 }, 'cache is as big as the scaled object');
});

QUnit.test('_updateCacheCanvas check if cache canvas should be updated', function(assert) {
Expand Down