diff --git a/src/shapes/object.class.js b/src/shapes/object.class.js index bfa59290b83..c127bd0c91f 100644 --- a/src/shapes/object.class.js +++ b/src/shapes/object.class.js @@ -922,23 +922,39 @@ } } var dims = this._limitCacheSize(this._getCacheCanvasDimensions()), + minCacheSize = fabric.minCacheSideLimit, width = dims.width, height = dims.height, zoomX = dims.zoomX, zoomY = dims.zoomY, dimensionsChanged = width !== this.cacheWidth || height !== this.cacheHeight, zoomChanged = this.zoomX !== zoomX || this.zoomY !== zoomY, - shouldRedraw = dimensionsChanged || zoomChanged; + shouldRedraw = dimensionsChanged || zoomChanged, + additionalWidth = 0, additionalHeight = 0, shouldResizeCanvas = false; + if (dimensionsChanged) { + var canvasWidth = this._cacheCanvas.width, + canvasHeight = this._cacheCanvas.height, + sizeGrowing = width > canvasWidth || height > canvasHeight, + sizeShrinking = (width < canvasWidth * 0.9 || height < canvasHeight * 0.9) && + canvasWidth > minCacheSize && canvasHeight > minCacheSize; + shouldResizeCanvas = sizeGrowing || sizeShrinking; + if (sizeGrowing) { + additionalWidth = (width * 0.1) & ~1; + additionalHeight = (height * 0.1) & ~1; + } + } if (shouldRedraw) { - if (dimensionsChanged) { - this._cacheCanvas.width = Math.ceil(width); - this._cacheCanvas.height = Math.ceil(height); + if (shouldResizeCanvas) { + this._cacheCanvas.width = Math.max(Math.ceil(width) + additionalWidth, minCacheSize); + this._cacheCanvas.height = Math.max(Math.ceil(height) + additionalHeight, minCacheSize); this.cacheWidth = width; this.cacheHeight = height; + this.cacheTranslationX = (width + additionalWidth) / 2; + this.cacheTranslationY = (height + additionalHeight) / 2; } else { this._cacheContext.setTransform(1, 0, 0, 1, 0, 0); this._cacheContext.clearRect(0, 0, this._cacheCanvas.width, this._cacheCanvas.height); } - this._cacheContext.translate(width / 2, height / 2); + this._cacheContext.translate(this.cacheTranslationX, this.cacheTranslationY); this._cacheContext.scale(zoomX, zoomY); this.zoomX = zoomX; this.zoomY = zoomY; @@ -1272,7 +1288,7 @@ */ drawCacheOnCanvas: function(ctx) { ctx.scale(1 / this.zoomX, 1 / this.zoomY); - ctx.drawImage(this._cacheCanvas, -this.cacheWidth / 2, -this.cacheHeight / 2); + ctx.drawImage(this._cacheCanvas, -this.cacheTranslationX, -this.cacheTranslationY); }, /**