如何使用web worker加载图像?

时间:2014-04-24 12:01:37

标签: javascript html image web-worker

我正在尝试使用webworker api加载图像。我的html页面中有大图像,需要5分钟来加载所有图像,因此我使用webworker来加载图像。

这是技术..

  1. 我在html页面中保留所有img标签的src属性为空。

  2. 所有图片都有每个img的唯一ID,例如id = events_all_1_01 意味着图片src将是“pictures / keywords / events / all / 1 / 01.jpg ”。即最后一部分events / all / 1 / 01.jpg是id。

  3. Main.html文件

     <body>  
        <script src="js/modernizr.custom.js"></script>
        <script language="javascript">
           window.onload = function(){   
            if (Modernizr.webworkers) {               
                var worker = new Worker('js/webworker/test_ww_23_04.js');  
                worker.onmessage = function(event) {
                    var url = event.data.replace(/_/g, "/");
                    var image_src = "pictures/keywords/"+url+".jpg";
                    var img = new Image();
                    img.src = image_src;     
                    img.onload = function(){
                        // do stuff when your image is loaded
                        document.getElementById(event.data).src = image_src;
                    }                          
                };
    
                worker.onerror = function(e) {
                    alert('Error: Line ' + e.lineno + ' in ' + e.filename + ': ' + e.message);
                };
    
                    var img_container = document.getElementById("wrapper");
                    var image_array = img_container.getElementsByTagName('img');
                    for(var i=0;i<image_array.length;i++){
                        var img_id = image_array[i].id;
                        console.log(img_id);  // http://jsfiddle.net/5vyseob7/
                        postMessage(img_id); // http://jsfiddle.net/k04t6760/ here i am passing id one by one to webworker..
                    }
                } // end of if condition                                             
            } // end of window.onload()
    
        </script>  
        <div id="wrapper" style="height: 500px;width: 200px;overflow-y: auto;border: 1px solid gray;">
            <div id="pictures1">
                <div class="effect-1">
                    <div><img src="" id="events_all_1_01" width="150" height="100"></div>
                    <div><img src="" id="events_all_1_02" width="150" height="100"></div>
                    <div><img src="" id="events_all_1_03" width="150" height="100"></div>
                    <div><img src="" id="events_all_1_04" width="150" height="100"></div>
                </div>
                <hr/>
                <div class="effect-2">
                    <div><img src="" id="events_all_2_01" width="150" height="100"></div>
                    <div><img src="" id="events_all_2_02" width="150" height="100"></div>
                    <div><img src="" id="events_all_2_03" width="150" height="100"></div>
                    <div><img src="" id="events_all_2_04" width="150" height="100"></div>
                </div>
                <hr/>
                <div class="effect-3">
                    <div><img src="" id="events_all_3_01" width="150" height="100"></div>
                    <div><img src="" id="events_all_3_02" width="150" height="100"></div>
                    <div><img src="" id="events_all_3_03" width="150" height="100"></div>
                    <div><img src="" id="events_all_3_04" width="150" height="100"></div>
                </div>
                <hr/>
                <div class="effect-4">
                    <div><img src="" id="events_all_4_01" width="150" height="100"></div>
                    <div><img src="" id="events_all_4_02" width="150" height="100"></div>
                    <div><img src="" id="events_all_4_03" width="150" height="100"></div>
                    <div><img src="" id="events_all_4_04" width="150" height="100"></div>
                </div>
            </div>
        </div>
    </body> 
    

    webworker代码。

     //var src = 'pictures/keywords/events/all/1/01.jpg';
     //var id = src.substring(src.substring(0,18).length).split('.')[0].replace(/\//g, "_"); // Creating id here......events_all_1_01
     //var fst = id.substring(0, id.lastIndexOf("_")+1); // get first part.... events_all_1
     //var lst = parseInt(id.substr(id.lastIndexOf("_")+1)); // get last part i.e imagename ..01,02,03 etc....... and convert it to int
    
        function LoadImages(currID) {
                setTimeout(function() {
                    postMessage(currID); 
                }, 100);  
        }
        self.onmessage = function(event) {
            var currID = event.data;
            LoadImages(currID); 
        };
    

    我收到以下错误:

    未捕获的SyntaxError:无法在'Window'上执行'postMessage':调用'postMessage'时无效的目标来源。

1 个答案:

答案 0 :(得分:2)

错误错误:

...
worker.onmessage = function(event) {
  var url = e.data.replace(/_/g, "/");

e 未定义...您可能需要function(e)event.data.replace

<强>更新

窗口没有postMessage方法......你需要使用worker方法worker.postMessage