3

I have an arraybuffer - named MEM - larger than the canvas width*height size. And I would like to draw the arrayBuffer data to the canvas.

imgData.data.set(...) should work, because imgData.data is an Uint8Array, which has the .set hethod. It works in FireFox, and Chrome, but in IE I get this error: Object doesn't support this property or method: 'set'

Initializaton:

var MEM = new ArrayBuffer(2*1024*1024);
var canvas, ctx, imgData;

var init = function() {

    canvas = document.getElementById('canvas');
    ctx = canvas.getContext('2d');

    imgData=ctx.createImageData(canvas.width, canvas.height);

    repaint();
};

Repaint function:

var repaint = function() {
    // .... //

    imgData.data.set(new Uint8Array(MEM, 0, canvas.width*canvas.height*4));
    ctx.putImageData(imgData, 0, 0);

    requestAnimationFrame(repaint);
};
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
  • 1
    Which version of IE are you using? According to [MDN](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) and [WPD](http://docs.webplatform.org/wiki/apis/canvas/ImageData/data), `ImageData.data` should returns an [`Uint8ClampedArray`](https://developer.mozilla.org/en-US/docs/Web/API/Uint8ClampedArray), not a `Unit8Array`; And even if it's a `Unit8Array`, it [requires IE>=10 standard mode to use `set`](http://msdn.microsoft.com/en-us/library/ie/br212917(v=vs.94).aspx). – Passerby Feb 27 '14 at 08:21
  • 1
    According to [MSDN](http://msdn.microsoft.com/en-us/library/ie/ff974957(v=vs.85).aspx), `ImageData.data` returns a ["`CanvasPixelArray`" object](http://msdn.microsoft.com/en-us/library/ie/ff975056(v=vs.85).aspx), which only has a `length` property. So I guess you're out of luck with IE. – Passerby Feb 27 '14 at 08:51
  • 1
    Perhaps you can get around it by using `ctx.createImageData(w, h)` ? –  Feb 27 '14 at 20:00

1 Answers1

4

With this code, it works

if(window.CanvasPixelArray) {
    CanvasPixelArray.prototype.set = function(arr) {
        var l=this.length, i=0;

        for(;i<l;i++) {
            this[i] = arr[i];
        }
    };
}
Iter Ator
  • 8,226
  • 20
  • 73
  • 164