From 253d3f68b1048f6e15e37687fd3d6c436787431c Mon Sep 17 00:00:00 2001 From: kkapsner Date: Sun, 24 May 2020 23:39:44 +0200 Subject: [PATCH] Added canvas cache to isPointInPath and isPointInStroke --- lib/modifiedCanvasAPI.js | 52 ++++++++++++++++++++++++++++++++-------- releaseNotes.txt | 2 +- test/test.html | 3 +-- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/modifiedCanvasAPI.js b/lib/modifiedCanvasAPI.js index b8c59e2..8242938 100644 --- a/lib/modifiedCanvasAPI.js +++ b/lib/modifiedCanvasAPI.js @@ -244,6 +244,31 @@ } } + const isPointCache = Object.create(null); + function getIsPointCacheIndex(x, y, values){ + return String.fromCodePoint(...values, x, y); + } + function getIsPointValue({func, x, y, index, originalValue, window, prefs}){ + const useCanvasCache = prefs("useCanvasCache"); + let cacheIndex; + const values = [originalValue, func(x^1, y), func(x, y^1), func(x^1, y^1)]; + if (useCanvasCache){ + cacheIndex = getIsPointCacheIndex(x, y, values); + const cached = isPointCache[cacheIndex]; + if ((typeof cached) === "boolean"){ + return cached; + } + } + + const rng = randomSupply.getIndexRng(1, 4, window); + const res = values[rng(index)]; + + if (useCanvasCache){ + isPointCache[cacheIndex] = res; + } + return res; + } + // changed functions and their fakes scope.changedFunctions = { getContext: { @@ -348,13 +373,16 @@ fakeGenerator: function(checker){ return function isPointInPath(x, y){ return checkerWrapper(checker, this, arguments, function(args, check){ - const {notify, window, original} = check; - const rng = randomSupply.getValueRng(1, window); + const {prefs, notify, window, original} = check; const originalValue = original.call(this, ...args); if ((typeof originalValue) === "boolean"){ notify("fakedReadout"); - const index = x + this.width * y; - return original.call(this, rng(x, index), rng(y, index), args[2]); + return getIsPointValue({ + func: (x, y) => original.call(this, x, y, args[2]), + x, y, + index: x + this.width * y, + originalValue, window, prefs + }); } else { return originalValue; @@ -370,22 +398,26 @@ fakeGenerator: function(checker){ return function isPointInStroke(x, y){ return checkerWrapper(checker, this, arguments, function(args, check){ - const {notify, window, original} = check; - const rng = randomSupply.getValueRng(1, window); + const {prefs, notify, window, original} = check; const originalValue = original.call(this, ...args); if ((typeof originalValue) === "boolean"){ notify("fakedReadout"); + let func; if (x instanceof window.Path2D){ let path = x; x = y; y = args[2]; - let index = x + this.width * y; - return original.call(this, path, rng(x, index), rng(y, index)); + func = (x, y) => original.call(this, path, x, y); } else { - let index = x + this.width * y; - return original.call(this, rng(x, index), rng(y, index)); + func = (x, y) => original.call(this, x, y); } + return getIsPointValue({ + func, + x, y, + index: x + this.width * y, + originalValue, window, prefs + }); } else { return originalValue; diff --git a/releaseNotes.txt b/releaseNotes.txt index 101182b..26820c1 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -1,6 +1,6 @@ Version 1.3: changes: - - + - added canvas cache to isPointInPath and isPointInStroke new features: - added link to FAQ diff --git a/test/test.html b/test/test.html index 4c51cb3..e17f0f6 100644 --- a/test/test.html +++ b/test/test.html @@ -13,8 +13,7 @@

Tests