workerloader.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var createImageBitmapOK=self.createImageBitmap?true:false;
  2. onmessage =function (evt){
  3. var data = evt.data;//通过evt.data获得发送来的数据
  4. loadImage2(data);
  5. if(!isSet)
  6. {
  7. isSet=true;
  8. setInterval(workerloop,1000);
  9. }
  10. }
  11. var isSet=true;
  12. function workerloop()
  13. {
  14. myTrace("png:workerloop");
  15. }
  16. var enableTrace=false;
  17. var ifShowTraceToMain=false;
  18. function myTrace(msg)
  19. {
  20. if(!enableTrace) return;
  21. console.log("png:"+msg)
  22. if(ifShowTraceToMain)
  23. {
  24. showMsgToMain(msg);
  25. }
  26. }
  27. function loadImage2(url)
  28. {
  29. var xhr,
  30. _this = this;
  31. var failed=false;
  32. xhr = new XMLHttpRequest;
  33. xhr.open("GET", url, true);
  34. //showMsgToMain("loadImage2");
  35. xhr.responseType = "arraybuffer";
  36. myTrace("load:"+url);
  37. xhr.onload = function() {
  38. var response=xhr.response || xhr.mozResponseArrayBuffer;
  39. //showMsgToMain("onload:");
  40. myTrace("onload:"+url);
  41. if((xhr.status != 200&&xhr.status!=0)||response.byteLength<10)
  42. {
  43. if(!failed)
  44. {
  45. failed=true;
  46. pngFail(url,"loadFail from onload"+xhr.status);
  47. }
  48. return;
  49. }
  50. var data, png;
  51. data = new Uint8Array(response);
  52. doCreateImageBitmap(data,url);
  53. };
  54. xhr.onerror = function(e){
  55. pngFail(url,"loadFail");
  56. }
  57. xhr.send(null);
  58. }
  59. function doCreateImageBitmap(response,url)
  60. {
  61. try
  62. {
  63. //showMsgToMain("hihidoCreateImageBitmap");
  64. //showMsgToMain("doCreateImageBitmap:"+response);
  65. //var startTime=getTimeNow();
  66. //showMsgToMain("new self.Blob");
  67. var startTime=getTimeNow();
  68. response = new self.Blob([response],{type:"image/png"});
  69. self.createImageBitmap(response).then(function(imageBitmap) {
  70. //showMsgToMain("imageBitmapCreated:");
  71. var data={};
  72. data.url=url;
  73. data.imageBitmap=imageBitmap;
  74. data.dataType="imageBitmap";
  75. data.startTime=startTime;
  76. data.decodeTime=getTimeNow()-startTime;
  77. data.sendTime=getTimeNow();
  78. myTrace("png:Decode By createImageBitmap,"+data.decodeTime,url);
  79. data.type="Image";
  80. postMessage(data,[data.imageBitmap]);
  81. }).catch(
  82. function(e)
  83. {
  84. showMsgToMain("catch e:"+e);
  85. pngFail(url,"parse fail"+e+":ya");
  86. }
  87. )
  88. }catch(e)
  89. {
  90. pngFail(url,"parse fail"+e.toString()+":ya");
  91. }
  92. }
  93. function getTimeNow()
  94. {
  95. return new Date().getTime();
  96. }
  97. function disableWorker(msg)
  98. {
  99. var data={};
  100. data.url=url;
  101. data.imagedata=null;
  102. data.type="Disable";
  103. data.msg=msg;
  104. postMessage(data);
  105. }
  106. function pngFail(url,msg)
  107. {
  108. var data={};
  109. data.url=url;
  110. data.imagedata=null;
  111. data.type="Image";
  112. data.msg=msg;
  113. console.log("png:"+msg+" "+url);
  114. postMessage(data);
  115. }
  116. function showMsgToMain(msg)
  117. {
  118. var data={};
  119. data.type="Msg";
  120. data.msg=msg;
  121. postMessage(data);
  122. }