Muath
Muath

Reputation: 4417

Compute the size in [bytes] of ImageData Object

Code:

// Create a new canvas
var canvas = document.createElement('canvas');
canvas.width = canvas.height = 100;
document.body.appendChild(canvas);

// Get drawing context
var ctx = canvas.getContext('2d');

// Get image data
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);

How to compute the size of imageData object in bytes?

Upvotes: 1

Views: 2096

Answers (1)

Alexander
Alexander

Reputation: 12785

imageData.data has a type of Uint8ClampedArray wich is 1 byle long .

So : imageData.data.length will give you the size in bytes which should be equal to canvas.width * canvas.height * 4 .
You multiply width and height by 4 because each pixel stores 4 bytes, RGBA .

Upvotes: 2

Related Questions