diff --git a/lib/modifiedCanvasAPI.js b/lib/modifiedCanvasAPI.js index b05bbf1..f78f5da 100644 --- a/lib/modifiedCanvasAPI.js +++ b/lib/modifiedCanvasAPI.js @@ -226,17 +226,26 @@ if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){ notify("fakedInput"); let oldImageData; + let x, y, width, height; + const border = 4; try { // "this" is not trustable - it may be not a context - oldImageData = getImageData(window, this).imageData; + const measurement = window.CanvasRenderingContext2D.prototype.measureText.call(this, args[0]); + width = Math.ceil(measurement.actualBoundingBoxRight + measurement.actualBoundingBoxLeft) + border; + height = Math.ceil(measurement.actualBoundingBoxDescent + measurement.actualBoundingBoxAscent) + border; + x = Math.max(0, args[1] - width); + y = Math.max(0, args[2] - height); + width = Math.min(3 * width, this.canvas.width - x); + height = Math.min(3 * height, this.canvas.height - y); + oldImageData = window.CanvasRenderingContext2D.prototype.getImageData.call(this, x, y, width, height); } catch (error){ // nothing to do here } // if "this" is not a correct context the next line will throw an error const ret = original.call(this, ...args); - const newImageData = getImageData(window, this).imageData; - this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0); + const newImageData = window.CanvasRenderingContext2D.prototype.getImageData.call(this, x, y, width, height); + this.putImageData(randomMixImageData(window, oldImageData, newImageData), x, y); return ret; } else { diff --git a/releaseNotes.txt b/releaseNotes.txt index 090af57..1487cc0 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -4,6 +4,7 @@ Version 1.5: new features: - added {empty}, {false} and {undefined} to webGL preference parameters (i.e. VENDOR, RENDERER, UNMASKED_VENDOR_WEBGL and UNMASKED_RENDERER_WEBGL) + - improved performance for protected canvas part "input" fixes: - protection of DOM manipulations on unloaded windows may break websites