Skip to content

Commit

Permalink
Increase cache canvas in steps, do not increase every mouse move (#4037)
Browse files Browse the repository at this point in the history
* first try

* step increase size of canvas

* removed console.log
git push
  • Loading branch information
asturur authored Jun 27, 2017
1 parent 6185b07 commit ede5d84
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
},

/**
Expand Down

0 comments on commit ede5d84

Please sign in to comment.