mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2024-12-22 12:50:36 +01:00
Modified randomSuppies API
getRng now returns a 32 bit random Number getIndexRng returns a random index in a range getBitRng returns a random bit getValueRng is the old getRng which alters the provided value
This commit is contained in:
parent
e079e1c0a3
commit
032bc2e424
@ -124,7 +124,7 @@
|
||||
var data2 = imageData2.data;
|
||||
var l = data1.length;
|
||||
if (l === data2.length){
|
||||
var rng = randomSupply.getRng(l, window);
|
||||
var rng = randomSupply.getValueRng(l, window);
|
||||
|
||||
for (var i = 0; i < l; i += 1){
|
||||
if (data1[i] > data2[i]){
|
||||
@ -314,7 +314,7 @@
|
||||
object: "CanvasRenderingContext2D",
|
||||
fakeGenerator: function(prefs, notify, window, original){
|
||||
return function isPointInPath(x, y){
|
||||
var rng = randomSupply.getRng(1, window);
|
||||
var rng = randomSupply.getValueRng(1, window);
|
||||
var originalValue = original.apply(this, window.Array.from(arguments));
|
||||
if ((typeof originalValue) === "boolean"){
|
||||
notify.call(this, "fakedReadout");
|
||||
@ -337,7 +337,7 @@
|
||||
object: "CanvasRenderingContext2D",
|
||||
fakeGenerator: function(prefs, notify, window, original){
|
||||
return function isPointInStroke(x, y){
|
||||
var rng = randomSupply.getRng(1, window);
|
||||
var rng = randomSupply.getValueRng(1, window);
|
||||
var originalValue = original.apply(this, window.Array.from(arguments));
|
||||
if ((typeof originalValue) === "boolean"){
|
||||
notify.call(this, "fakedReadout");
|
||||
|
@ -12,6 +12,56 @@
|
||||
window.scope.randomSupplies = {};
|
||||
scope = window.scope.randomSupplies;
|
||||
}
|
||||
|
||||
const rngTemplate = {
|
||||
getBitRng: function(length, window){
|
||||
const rng = this.getRng(Math.ceil(length / 32), window);
|
||||
let bitIndex = 32;
|
||||
let rnd = 0;
|
||||
let mask = 0xffffffff * 2;
|
||||
return function(value, i){
|
||||
if (mask > 0xffffffff){
|
||||
mask = 1;
|
||||
rnd = rng(i / 32);
|
||||
}
|
||||
let bit = 1 * (!!(rnd & mask));
|
||||
mask *= 2;
|
||||
return bit;
|
||||
};
|
||||
},
|
||||
getIndexRng: function(length, maxIndex, window){
|
||||
const rng = this.getRng(length, window);
|
||||
|
||||
return function(i){
|
||||
return Math.floor(rng(i) / 0xffffffff * maxIndex);
|
||||
};
|
||||
},
|
||||
getValueRng: function(length, window){
|
||||
const rng = this.getBitRng(length, window);
|
||||
return function(value, i){
|
||||
var rnd = rng(value, i);
|
||||
|
||||
// XOR the last bit to alter it... or not
|
||||
return value ^ (rnd & 0x01);
|
||||
};
|
||||
},
|
||||
getPixelRng: function(length, window, ignoredColors){
|
||||
var rng = this.getValueRng(length, window);
|
||||
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
||||
var index = String.fromCharCode(r, g, b, a);
|
||||
if (ignoredColors[index]){
|
||||
return [r, g, b, a];
|
||||
}
|
||||
var baseIndex = i * 4;
|
||||
return [
|
||||
rng(r, baseIndex + 0),
|
||||
rng(g, baseIndex + 1),
|
||||
rng(b, baseIndex + 2),
|
||||
rng(a, baseIndex + 3)
|
||||
];
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const settings = require("./settings");
|
||||
|
||||
@ -40,7 +90,7 @@
|
||||
return typeof value === "number" && value >= 0 && value < 256;
|
||||
})
|
||||
){
|
||||
persistentRnd[domain] = value;
|
||||
persistentRnd[domain] = new Uint8Array(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -73,120 +123,90 @@
|
||||
return persistentRnd[domain];
|
||||
};
|
||||
}());
|
||||
scope.persistent = {
|
||||
name: "persistent",
|
||||
setDomainRnd: function(domain, rnd){
|
||||
persistentRnd[domain] = new Uint8Array(rnd);
|
||||
},
|
||||
getRng: function(length, window){
|
||||
var bitSet = getPersistentRnd(window);
|
||||
|
||||
scope.persistent = Object.create(rngTemplate);
|
||||
scope.persistent.name = "persistent";
|
||||
scope.persistent.setDomainRnd = function(domain, rnd){
|
||||
persistentRnd[domain] = new Uint8Array(rnd);
|
||||
};
|
||||
scope.persistent.getRng = function(length, window){
|
||||
var bitSet = new Uint32Array(getPersistentRnd(window).buffer);
|
||||
var bitSetLength = bitSet.length;
|
||||
return function(i){
|
||||
return bitSet[i % bitSetLength];
|
||||
};
|
||||
};
|
||||
scope.persistent.getBitRng = function(length, window){
|
||||
var bitSet = getPersistentRnd(window);
|
||||
|
||||
return function(value, i){
|
||||
// use the last 7 bits from the value for the index of the
|
||||
// random number
|
||||
var index = value & 0x7F;
|
||||
|
||||
// use the last 3 bits from the position and the first bit from
|
||||
// from the value to get bit to use from the random number
|
||||
var bitIndex = ((i & 0x03) << 1) | (value >>> 7);
|
||||
|
||||
// extract the bit
|
||||
var bit = (bitSet[index] >>> bitIndex) & 0x01;
|
||||
|
||||
return bit;
|
||||
};
|
||||
};
|
||||
|
||||
scope.constant = Object.create(rngTemplate);
|
||||
scope.constant.name = "constant";
|
||||
scope.constant.getRng = function(length, window){
|
||||
return scope.nonPersistent.getRng(length, window);
|
||||
};
|
||||
scope.constant.getPixelRng = (function(){
|
||||
var colors = Object.create(null);
|
||||
return function getConstantPixelRng(length, window, ignoredColors){
|
||||
var rng = scope.nonPersistent.getValueRng(1024, window);
|
||||
|
||||
return function(value, i){
|
||||
// use the last 7 bits from the value for the index of the
|
||||
// random number
|
||||
var index = value & 0x7F;
|
||||
|
||||
// use the last 3 bits from the position and the first bit from
|
||||
// from the value to get bit to use from the random number
|
||||
var bitIndex = ((i & 0x03) << 1) | (value >>> 7);
|
||||
|
||||
// extract the bit
|
||||
var bit = (bitSet[index] >>> bitIndex) & 0x01;
|
||||
|
||||
// XOR the bit and the value to alter the last bit of it... or not
|
||||
return value ^ bit;
|
||||
};
|
||||
},
|
||||
getPixelRng: function(length, window, ignoredColors){
|
||||
var rng = this.getRng(length, window);
|
||||
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
||||
var index = String.fromCharCode(r, g, b, a);
|
||||
if (ignoredColors[index]){
|
||||
return [r, g, b, a];
|
||||
}
|
||||
var baseIndex = i * 4;
|
||||
return [
|
||||
rng(r, baseIndex + 0),
|
||||
rng(g, baseIndex + 1),
|
||||
rng(b, baseIndex + 2),
|
||||
rng(a, baseIndex + 3)
|
||||
];
|
||||
var color = colors[index];
|
||||
if (!color){
|
||||
color = [
|
||||
rng(r, 0),
|
||||
rng(g, 0),
|
||||
rng(b, 0),
|
||||
rng(a, 0)
|
||||
];
|
||||
colors[index] = color;
|
||||
}
|
||||
return color;
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
}());
|
||||
|
||||
scope.constant = {
|
||||
name: "constant",
|
||||
getRng: function(length, window){
|
||||
return scope.nonPersistent.getRng(length, window);
|
||||
},
|
||||
getPixelRng: (function(){
|
||||
var colors = Object.create(null);
|
||||
return function getConstantPixelRng(length, window, ignoredColors){
|
||||
var rng = scope.nonPersistent.getRng(1024, window);
|
||||
|
||||
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
||||
var index = String.fromCharCode(r, g, b, a);
|
||||
if (ignoredColors[index]){
|
||||
return [r, g, b, a];
|
||||
}
|
||||
var color = colors[index];
|
||||
if (!color){
|
||||
color = [
|
||||
rng(r, 0),
|
||||
rng(g, 0),
|
||||
rng(b, 0),
|
||||
rng(a, 0)
|
||||
];
|
||||
colors[index] = color;
|
||||
}
|
||||
return color;
|
||||
};
|
||||
};
|
||||
}())
|
||||
};
|
||||
|
||||
scope.nonPersistent = {
|
||||
name: "nonPersistent",
|
||||
getRng: function(length, window){
|
||||
// Initialize the random number batch creation
|
||||
var randomI = 65536;
|
||||
var randomNumbers = new Uint8Array(Math.min(65536, length));
|
||||
scope.nonPersistent = Object.create(rngTemplate);
|
||||
scope.nonPersistent.name = "nonPersistent";
|
||||
scope.nonPersistent.getRng = function(length, window){
|
||||
const maxLength = 0x4000;
|
||||
var randomI = maxLength;
|
||||
var randomNumbers = new Uint32Array(Math.min(maxLength, length));
|
||||
return function(i){
|
||||
if (randomI >= randomNumbers.length){
|
||||
// refill the random number bucket if empty
|
||||
randomI = 0;
|
||||
if (length - i < maxLength){
|
||||
randomNumbers = new Uint32Array(Math.max(1, length - i));
|
||||
}
|
||||
window.crypto.getRandomValues(randomNumbers);
|
||||
}
|
||||
var rnd = randomNumbers[randomI];
|
||||
randomI += 1;
|
||||
|
||||
return function(value, i){
|
||||
if (randomI >= randomNumbers.length){
|
||||
// refill the random number bucket if empty
|
||||
randomI = 0;
|
||||
if (length - i < 65536){
|
||||
randomNumbers = new Uint8Array(length - i);
|
||||
}
|
||||
window.crypto.getRandomValues(randomNumbers);
|
||||
}
|
||||
var rnd = randomNumbers[randomI];
|
||||
randomI += 1;
|
||||
|
||||
// XOR the last bit to alter it... or not
|
||||
return value ^ (rnd & 0x01);
|
||||
};
|
||||
},
|
||||
getPixelRng: function(length, window, ignoredColors){
|
||||
var rng = this.getRng(length, window);
|
||||
return function(r, g, b, a, i){ // eslint-disable-line max-params
|
||||
var index = String.fromCharCode(r, g, b, a);
|
||||
if (ignoredColors[index]){
|
||||
return [r, g, b, a];
|
||||
}
|
||||
var baseIndex = i * 4;
|
||||
return [
|
||||
rng(r, baseIndex + 0),
|
||||
rng(g, baseIndex + 1),
|
||||
rng(b, baseIndex + 2),
|
||||
rng(a, baseIndex + 3)
|
||||
];
|
||||
};
|
||||
}
|
||||
return rnd;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
scope.white = {
|
||||
name: "white",
|
||||
getRng: function(){
|
||||
@ -194,6 +214,19 @@
|
||||
return 255;
|
||||
};
|
||||
},
|
||||
getBitRng: function(){
|
||||
return function(){
|
||||
return 1;
|
||||
};
|
||||
},
|
||||
getIndex: function(){
|
||||
return function(){
|
||||
return 0;
|
||||
};
|
||||
},
|
||||
getValueRng: function(){
|
||||
return this.getRng();
|
||||
},
|
||||
getPixelRng: function(){
|
||||
return function(){
|
||||
return [255, 255, 255, 255];
|
||||
|
Loading…
x
Reference in New Issue
Block a user