From 3665449f0a4f6ad4adb99b95f7791058c945a98c Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 7 May 2018 06:20:44 -0700 Subject: [PATCH 1/2] isTargetTransparent() now samples the cache directly. Previously, isTargetTransparent would do a paint from the cache to the contextCache which has more overhead and is slower. With this commit, fabricjs will test the cache canvas directly using x/y coordinates calculated with respect to the target object center. This is more performant since it avoids an extra render on every call to isTargetTransparent. --- src/canvas.class.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/canvas.class.js b/src/canvas.class.js index 055e78ae1a3..bf18b94ebaf 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -492,6 +492,17 @@ * @return {Boolean} */ isTargetTransparent: function (target, x, y) { + if (target.shouldCache() && target._cacheCanvas && !target.isCacheDirty()) { + var normalizedPointer = this._normalizePointer(target, {x: x, y: y}), + targetRelativeX = target.cacheTranslationX + (normalizedPointer.x * target.zoomX), + targetRelativeY = target.cacheTranslationY + (normalizedPointer.y * target.zoomY); + + var isTransparent = fabric.util.isTransparent( + target._cacheContext, targetRelativeX, targetRelativeY, this.targetFindTolerance); + + return isTransparent; + } + var ctx = this.contextCache, originalColor = target.selectionBackgroundColor, v = this.viewportTransform; From c30e5b4cc1af36323a9c190cdfec727d0d80b752 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sat, 19 May 2018 23:43:08 +0200 Subject: [PATCH 2/2] remove cacheDirty that should not be needed. --- src/canvas.class.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/canvas.class.js b/src/canvas.class.js index bf18b94ebaf..502a9a08d0a 100644 --- a/src/canvas.class.js +++ b/src/canvas.class.js @@ -492,7 +492,7 @@ * @return {Boolean} */ isTargetTransparent: function (target, x, y) { - if (target.shouldCache() && target._cacheCanvas && !target.isCacheDirty()) { + if (target.shouldCache() && target._cacheCanvas) { var normalizedPointer = this._normalizePointer(target, {x: x, y: y}), targetRelativeX = target.cacheTranslationX + (normalizedPointer.x * target.zoomX), targetRelativeY = target.cacheTranslationY + (normalizedPointer.y * target.zoomY);