diff --git a/web/compatibility.js b/web/compatibility.js index 400a940eb3c018..1c1a2d1577b8bb 100644 --- a/web/compatibility.js +++ b/web/compatibility.js @@ -481,9 +481,9 @@ if (typeof PDFJS === 'undefined') { } })(); -// TODO CanvasPixelArray is deprecated; use Uint8ClampedArray -// once it's supported. +// Support: IE<11, Chrome<21 (function checkSetPresenceInImageData() { + // IE < 11 will use window.CanvasPixelArray which lacks set function. if (window.CanvasPixelArray) { if (typeof window.CanvasPixelArray.prototype.set !== 'function') { window.CanvasPixelArray.prototype.set = function(arr) { @@ -492,6 +492,25 @@ if (typeof PDFJS === 'undefined') { } }; } + } else { + // Chrome < 21 uses an inaccessible CanvasPixelArray prototype. + // be cause we cannot feature detect it, we rely on user agent. + if (navigator.userAgent.indexOf('Chrom') >= 0) { + var version = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]; + if (parseInt(version) < 21) { + var contextPrototype = window.CanvasRenderingContext2D.prototype; + contextPrototype._createImageData = contextPrototype.createImageData; + contextPrototype.createImageData = function(w, h) { + var imageData = this._createImageData(w, h); + imageData.data.set = function(arr) { + for (var i = 0, ii = this.length; i < ii; i++) { + this[i] = arr[i]; + } + }; + return imageData; + }; + } + } } })();