From f89a9b67ed03f038d9220ca40deae286d729258f Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 7 May 2018 06:20:44 -0700 Subject: [PATCH] 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..2df20ea4a24 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;