fix space for cache canvas for thin lines (#3596)

* fix space for cache canvas
* add the fixed padding to cache canvas
* update group test
This commit is contained in:
Andrea Bogazzi 2016-12-30 18:19:18 +01:00 committed by GitHub
parent 3c0fe81b77
commit 1cb047bb66
4 changed files with 13 additions and 12 deletions

View file

@ -860,8 +860,8 @@
width = dim.x * zoomX,
height = dim.y * zoomY;
return {
width: width,
height: height,
width: Math.ceil(width) + 2,
height: Math.ceil(height) + 2,
zoomX: zoomX,
zoomY: zoomY
};

View file

@ -404,8 +404,9 @@
*/
_getCacheCanvasDimensions: function() {
var dim = this.callSuper('_getCacheCanvasDimensions');
dim.width += 2 * this.fontSize;
dim.height += 2 * this.fontSize;
var fontSize = Math.ceil(this.fontSize) * 2;
dim.width += fontSize;
dim.height += fontSize;
return dim;
},

View file

@ -544,7 +544,7 @@
var dims = obj._getCacheCanvasDimensions();
g1.scaleX = 2;
var dims2 = obj._getCacheCanvasDimensions();
equal(dims2.width, dims.width * g1.scaleX, 'width of cache has increased with group scale');
equal((dims2.width - 2), (dims.width - 2) * g1.scaleX, 'width of cache has increased with group scale');
});
test('test group transformMatrix', function() {

View file

@ -1505,29 +1505,29 @@
test('_getCacheCanvasDimensions returns dimensions and zoom for cache canvas', function() {
var object = new fabric.Object({ width: 10, height: 10, strokeWidth: 0 });
var dims = object._getCacheCanvasDimensions();
deepEqual(dims, { width: 10, height: 10, zoomX: 1, zoomY: 1 }, 'if no scaling is applied cache is as big as object');
deepEqual(dims, { width: 12, height: 12, zoomX: 1, zoomY: 1 }, 'if no scaling is applied cache is as big as object');
object.strokeWidth = 2;
dims = object._getCacheCanvasDimensions();
deepEqual(dims, { width: 12, height: 12, zoomX: 1, zoomY: 1 }, 'cache contains the stroke');
deepEqual(dims, { width: 14, height: 14, zoomX: 1, zoomY: 1 }, 'cache contains the stroke');
object.scaleX = 2;
object.scaleY = 3;
dims = object._getCacheCanvasDimensions();
deepEqual(dims, { width: 24, height: 36, zoomX: 2, zoomY: 3 }, 'cache is as big as the scaled object');
deepEqual(dims, { width: 26, height: 38, zoomX: 2, zoomY: 3 }, 'cache is as big as the scaled object');
});
test('_updateCacheCanvas check if cache canvas should be updated', function() {
var object = new fabric.Object({ width: 10, height: 10, strokeWidth: 0 });
object._createCacheCanvas();
equal(object.cacheWidth, 10, 'current cache dimensions are saved');
equal(object.cacheHeight, 10, 'current cache dimensions are saved');
equal(object.cacheWidth, 12, 'current cache dimensions are saved');
equal(object.cacheHeight, 12, 'current cache dimensions are saved');
equal(object._updateCacheCanvas(), false, 'second execution of cache canvas return false');
object.scaleX = 2;
equal(object._updateCacheCanvas(), true, 'if scale change, it returns true');
equal(object.cacheWidth, 20, 'current cache dimensions is updated');
equal(object.cacheWidth, 22, 'current cache dimensions is updated');
equal(object.zoomX, 2, 'current scale level is saved');
object.width = 2;
equal(object._updateCacheCanvas(), true, 'if dimension change, it returns true');
equal(object.cacheWidth, 4, 'current cache dimensions is updated');
equal(object.cacheWidth, 6, 'current cache dimensions is updated');
object.strokeWidth = 2;
equal(object._updateCacheCanvas(), true, 'if strokeWidth change, it returns true');
});