problem about "gl.texImage2D" function

about my program:

var imgSrc = “XXX.gif”;
var texture = gl.createTexture();
texture.image = new Image();
texture.onload = function(){
gl.bindTexture(gl.TEXTURE_2D,texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,true);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
};
texture.image.src = imgSrc;

problem:
the error firefox tell me is happen on the line

"gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,texture.image);

but the URL of the image is correct,so I think the error is on “put image to graphic card’s texture space”

but I have no idea how to solve this error

the message firefox says

error: uncaught exception: [Exception… “Failure arg 5 [nsIDOMWebGLRenderingContext.texImage2D]” nsresult: “0x80004005 (NS_ERROR_FAILURE)” location: “JS frame :: file:///E:/%E5%B0%88%E9%A1%8C(%E6%9C%89bug)/code/Painter.js :: <TOP_LEVEL> :: line 11” data: no]

line 11 is the place gl.texImage2D function

How amazing

var imgSrc = “XXX.gif”;
var texture = gl.createTexture();
texture.image = new Image();
texture.image.src = imgSrc;//<----- 1
texture.onload = function(){
gl.bindTexture(gl.TEXTURE_2D,texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL,true);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
//gl.bindTexture(gl.TEXTURE_2D, null);//<------ 2
};

I put 1 to the front and comment 2 and the program run~~

Is there any one can tell me why?

because you are doing it wrong :slight_smile:

you need to set the onload event for image in the texture, not the texture. because image is loaded asynchronously and you want to wait untill it is loaded and the n perform the actions.

so instead of texture.onload = … use texture.image.onload = …