OffscreenDraw.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var offscreen = null, offscreen, ctx,gpu;
  2. time = parseInt(new Date().valueOf())
  3. timeRed = [255, 0, 0, 1]
  4. timeGreen = [0, 255, 0, 1]
  5. timeColor = timeRed
  6. // const multiplyMatrix = self.gpu.createKernel(function(a, b) {
  7. // let sum = 0;
  8. // for (let i = 0; i < 512; i++) {
  9. // sum += a[this.thread.y][i] * b[i][this.thread.x];
  10. // }
  11. // return sum;
  12. // }).setOutput([512, 512]);
  13. onmessage = async function (e) {
  14. let {msg,gpu} = e.data
  15. switch (msg) {
  16. case 'init':
  17. let {offscreen} = e.data
  18. if (offscreen) {
  19. self.gpu = gpu
  20. self.offscreen = offscreen
  21. self.ctx = self.offscreen.getContext('2d');
  22. }
  23. // console.log('offscreen=>', offscreen)
  24. break;
  25. case 'run':
  26. run(e.data.payload)
  27. break
  28. }
  29. // var {width, height, imageData} = e.data.payload;
  30. // if (!offscreen && !ctx) {
  31. // offscreen = new OffscreenCanvas(width, height);
  32. // ctx = offscreen.getContext("2d");
  33. // }
  34. // const imgblob = await fetch(imageData)
  35. // .then(r => r.blob());
  36. // // console.log('imgblob=>', imgblob)
  37. // const img = await createImageBitmap(imgblob);
  38. // // console.log('img=>', img)
  39. // ctx.drawImage(img, 0, 0);
  40. // var imageBitmap = offscreen.transferToImageBitmap();
  41. // postMessage({imageBitmap: imageBitmap}, [imageBitmap]);
  42. }
  43. async function run(payload) {
  44. let {width, height, imageData, colorMode, colorModeMap} = payload;
  45. //debug
  46. // // console.log('test=>', parseInt(new Date().valueOf()) - self.time)
  47. if (parseInt(new Date().valueOf()) - self.time > 5000) {
  48. self.time = parseInt(new Date().valueOf())
  49. if (JSON.stringify(self.timeColor) === JSON.stringify(self.timeRed)) {
  50. self.timeColor = self.timeGreen
  51. } else {
  52. self.timeColor = self.timeRed
  53. }
  54. }
  55. let colorMap = colorModeMap[colorMode]
  56. // // console.log('draw offscreen=>', self.offscreen,e.data.payload)
  57. const imgblob = await fetch(imageData)
  58. .then(r => r.blob());
  59. // // console.log('imgblob=>', imgblob)
  60. // // console.log('colorMode=>', colorMode)
  61. const img = await createImageBitmap(imgblob);
  62. self.ctx.clearRect(0, 0, width, height);
  63. self.ctx.drawImage(img, 0, 0)
  64. // colorMap = false
  65. if (colorMap) {
  66. let imageD = self.ctx.getImageData(0, 0, width, height);
  67. let pdata = imageD.data;
  68. // // console.log('pdata=>',pdata.length)
  69. // console.timeEnd('test');
  70. // console.time('test');
  71. for (let i = 0; i < pdata.length; i += 4) {
  72. // if (pdata[j] === color2[0]) pdata[j] = colorArr[0];
  73. // if (pdata[j + 1] === color2[1]) pdata[j + 1] = colorArr[1];
  74. // if (pdata[j + 2] === color2[2]) pdata[j + 2] = colorArr[2];
  75. // // // console.log('colorMap[key]=>', colorMap[key])
  76. let r = pdata[i]
  77. let j = pdata[i + 1]
  78. let b = pdata[i + 2]
  79. let a = pdata[i + 3]
  80. let map = [r, j, b, pdata[i + 4]]
  81. // // console.log('r=>',r)
  82. // if(r <= 80){
  83. // pdata[i] = 255
  84. // pdata[i + 1] = 255
  85. // pdata[i + 2] = 0
  86. // }else{
  87. // pdata[i] = 0
  88. // pdata[i + 1] = 0
  89. // pdata[i + 2] = 255
  90. // }
  91. for(let item of colorMap){
  92. let {condition, result} = item
  93. let check = true
  94. // eval('2 >200')
  95. condition.map((_, index) => {
  96. let {symbol,target} = _
  97. let ret = false
  98. switch (symbol) {
  99. case '>':
  100. ret = map[index] > target
  101. break;
  102. case '<':
  103. ret = map[index] < target
  104. break
  105. default:
  106. }
  107. if (!ret) {
  108. check = ret
  109. }
  110. })
  111. if (check === true && result) {
  112. pdata[i] = result[0]
  113. pdata[i + 1] = result[1]
  114. pdata[i + 2] = result[2]
  115. pdata[3] = 0
  116. break
  117. }else{
  118. // // console.log('pdata=>',pdata[i],pdata[i+1],pdata[i+2],pdata[i+3])
  119. continue
  120. }
  121. }
  122. // pdata[j] = colorMap[key][0]
  123. // pdata[j + 1] = colorMap[key][1]
  124. // pdata[j + 2] = colorMap[key][2]
  125. }
  126. // console.timeEnd('test');
  127. // // console.log(' self.timeColor=>', self.timeColor)
  128. self.ctx.putImageData(imageD, 0, 0);
  129. // self.ctx.clearRect(0, 0, width, height);
  130. // self.ctx.drawImage(img, 0, 0)
  131. }
  132. }