JavaScript loading progress of an image

前端 未结 7 1942
慢半拍i
慢半拍i 2020-11-29 18:55

Is there a way in JS to get the progress of a loading image while the image is being loaded? I want to use the new Progress tag of HTML5 to show the progress of loading imag

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 19:12

    Here is a small update of the code of Julian Jensen in order to be able to draw the image in a Canvas after it is loaded :

    xmlHTTP.onload = function( e ) {
            var h = xmlHTTP.getAllResponseHeaders(),
                m = h.match( /^Content-Type\:\s*(.*?)$/mi ),
                mimeType = m[ 1 ] || 'image/png';
                // Remove your progress bar or whatever here. Load is done.
    
            var blob = new Blob( [ this.response ], { type: mimeType } );
            thisImg.src = window.URL.createObjectURL( blob );
    
             thisImg.onload = function()
                {
                    if ( callback ) callback( this );
                };
        };
    

提交回复
热议问题