| (function() {
|
| 'use strict';
|
|
|
|
|
| const origToDataURL = HTMLCanvasElement.prototype.toDataURL;
|
| const origToBlob = HTMLCanvasElement.prototype.toBlob;
|
| const origGetImageData = CanvasRenderingContext2D.prototype.getImageData;
|
|
|
| function addNoise(ctx, canvas) {
|
| try {
|
| const w = Math.min(canvas.width, 2);
|
| const h = Math.min(canvas.height, 2);
|
| const imageData = ctx.getImageData.call(ctx, 0, 0, w, h);
|
|
|
| const idx = Math.floor(Math.random() * imageData.data.length);
|
| imageData.data[idx] = Math.max(0, Math.min(255, imageData.data[idx] + (Math.random() > 0.5 ? 1 : -1)));
|
| ctx.putImageData(imageData, 0, 0);
|
| } catch(e) { }
|
| }
|
|
|
| HTMLCanvasElement.prototype.toDataURL = function() {
|
| const ctx = this.getContext('2d');
|
| if (ctx) addNoise(ctx, this);
|
| return origToDataURL.apply(this, arguments);
|
| };
|
|
|
| HTMLCanvasElement.prototype.toBlob = function() {
|
| const ctx = this.getContext('2d');
|
| if (ctx) addNoise(ctx, this);
|
| return origToBlob.apply(this, arguments);
|
| };
|
|
|
| CanvasRenderingContext2D.prototype.getImageData = function() {
|
| const result = origGetImageData.apply(this, arguments);
|
|
|
| if (result.data.length > 4) {
|
| const idx = Math.floor(Math.random() * result.data.length);
|
| result.data[idx] = Math.max(0, Math.min(255, result.data[idx] + (Math.random() > 0.5 ? 1 : -1)));
|
| }
|
| return result;
|
| };
|
| })();
|
|
|