View Full Version : Accessing image data

01-14-2011, 08:38 PM
I have the following code snippet:

var texture = gl.createTexture();
texture.image = new Image();
texture.image.onload = function() {
.... // I want to read the pixels once the image has been loaded
texture.image.src = urlImage;

I'd like to get the image pixels once it has been loaded.

Is it possible? If so, what's the best to do it?

Thanks in advance for your help.

01-15-2011, 01:51 PM
I use HTML5 <canvas> to read the texels with the following code snippets:

var myCanvas = document.createElement("canvas");
myCanvas.width = texture.image.width;
myCanvas.height = texture.image.height;
var myCanvasContext = myCanvas.getContext("2d"); // Get canvas 2d context
myCanvasContext.drawImage(texture.image, 0, 0); // Draw the texture
var texels = myCanvasContext.getImageData(0,0, width, height); // Read the texels/pixels back

It does what I want to do. Please let me know if there is a better way