NS_ERROR_NOT_AVAILABLE: Component is not available

前端 未结 3 1049
我在风中等你
我在风中等你 2020-12-03 16:53

I have a problem. I am trying to draw an image onto a canvas. The image is not from the HTML page, but on a file. Here is the code i use:

var img = new Image         


        
3条回答
  •  醉梦人生
    2020-12-03 17:24

    The problem that I guess here, is when the resources be available?, but there is a way to confirm that the resources are available, just check 'complete' attribute of the image object.

    if (img.complete == true){
       // is available.
    } else {
       // wait until is ready.
    }
    

    Also, you can make a merge method with onload event and a delay method that checking both stuff.

    var img = new Image();
    //first attach handler
    img.onload = function(e){
       if (e.target.complete == true) {
                yourHandler(e);
            } else {               
                var maxTimes = 10;
                var countTimes = 0;
                function retryLoadImage() {
                    setTimeout(function () {
                        if (countTimes > maxTimes) {
                            // -- cannot load image.
                            return;
                        } else {
                            if (e.target.complete == true) {
                                yourHandler(e);
                            } else {
                                retryLoadImage();
                            }
                        }
                        countTimes++;
                    }, 50);
                }; 
            }
    };
    img.src = yourSource;
    

    This is working for me!!! on IE and FF.

提交回复
热议问题