Skip to content

Commit cd47d40

Browse files
committed
Better canvas farbling detection logic
1 parent 91968f8 commit cd47d40

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/Util.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,31 @@ export const Util = {
603603
}
604604

605605
const c = this.createCanvasElement();
606-
c.width = 1;
607-
c.height = 1;
606+
c.width = 10;
607+
c.height = 10;
608608
const ctx = c.getContext('2d', {
609609
willReadFrequently: true,
610610
}) as CanvasRenderingContext2D;
611-
ctx.clearRect(0, 0, 1, 1);
612-
ctx.fillStyle = 'rgba(255,0,255,1)'; // clear #FF00FF, no alpha
613-
ctx.fillRect(0, 0, 1, 1);
614-
const d = ctx.getImageData(0, 0, 1, 1).data;
615-
const exact = d[0] === 255 && d[1] === 0 && d[2] === 255 && d[3] === 255;
616-
_isCanvasFarblingActive = !exact;
611+
ctx.clearRect(0, 0, 10, 10);
612+
613+
ctx.fillStyle = '#282828'; // 40, 40, 40
614+
ctx.fillRect(0, 0, 10, 10);
615+
616+
const d = ctx.getImageData(0, 0, 10, 10).data;
617+
let isFarbling = false;
618+
for (let i = 0; i < 100; i++) {
619+
if (
620+
d[i * 4] !== 40 ||
621+
d[i * 4 + 1] !== 40 ||
622+
d[i * 4 + 2] !== 40 ||
623+
d[i * 4 + 3] !== 255
624+
) {
625+
isFarbling = true;
626+
break;
627+
}
628+
}
629+
630+
_isCanvasFarblingActive = isFarbling;
617631
this.releaseCanvas(c);
618632
return _isCanvasFarblingActive;
619633
},

test/sandbox.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
const layer = new Konva.Layer();
3232
stage.add(layer);
3333

34+
// const circle = new Konva.Circle({
35+
// x: 100,
36+
// y: 100,
37+
// radius: 10,
38+
// fill: 'red',
39+
// });
40+
// layer.add(circle);
41+
// throw new Error('test');
42+
3443
// Given gridRows and gridCols, compute maximum possible radius and position circles
3544
const gridRows = 200;
3645
const gridCols = 200;

0 commit comments

Comments
 (0)