javascriptcanvasimagebitmap

How can I use createImageBitmap with raw data as input rather then an image


I am writing a data viz app that requires me to process very large 2D arrays of data and convert that data into a scaled down image for display in a canvas in the DOM.

I am bumping up against DOM canvas size limitations. My arrays can be as large as 5000 x 5000. I want to get around the canvas size limitation by using createImageBitmap() to simultaneously scale down and convert the large array to an ImageBitMap of smaller size - 256 x 256 - for insertion into an onscreen canvas.

How can I convert the raw array data into the proper format? Will this approach work?


Solution

  • You can create and manipulate your image before rendering it to canvas. 5000x5000 shouldn't be too large for canvas though. What limitations are you running into? Answer here covers resizing as on canvas then grabbing data.

    var raw = new Uint8ClampedArray(5000*5000*4); // 4 for RBGA
    var imageData = new ImageData(raw, 5000,5000);
    var bitmap = createImageBitmap(imageData);