1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Added canvas cache to isPointInPath and isPointInStroke

This commit is contained in:
kkapsner 2020-05-24 23:39:44 +02:00
parent da5a9f4509
commit 253d3f68b1
3 changed files with 44 additions and 13 deletions

View File

@ -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;

View File

@ -1,6 +1,6 @@
Version 1.3:
changes:
-
- added canvas cache to isPointInPath and isPointInStroke
new features:
- added link to FAQ

View File

@ -13,8 +13,7 @@
<ul>
<li>all the hashes should be equal for each test</li>
<li>the hash before and after the "/" do not have to match</li>
<li>"isPointInPath" may vary between tests</li>
<li>if "refresh" is clicked the hash must not change (but "isPointInPath" may)</li>
<li>if "refresh" is clicked the hash must not change</li>
<li>upon page reload the hash changes (depending on CanvasBlocker settings - e.g. not in the stealth preset)</li>
</ul>
<h2>Tests</h2>