Explorar o código

1.删除不必要的初始化;2.将baseUrl送到worker中;

jevian ma(马作伟_沃航科技) hai 1 mes
pai
achega
dc60f36395
Modificáronse 2 ficheiros con 10 adicións e 15 borrados
  1. 5 13
      src/utils/worker.js
  2. 5 2
      src/views/runtime.vue

+ 5 - 13
src/utils/worker.js

@@ -46,8 +46,8 @@ const initWasm = (url) => {
   }
 }
 
-let canvasEl
 let ctx
+let baseUrl
 
 const r = 26 // 圆的半径
 const l_x = 47 + r  // 左上角圆的圆的心x坐标
@@ -62,7 +62,7 @@ const drawPlc = async (key, index, val, type) => {
   if (!ctx) return
   ctx.font = '30px Noto Sans SC'
   if (type === 'init') {
-    const response = await fetch(new URL('/wxapp/canvas-icon/ditu.png', self.location.href).href)
+    const response = await fetch(new URL(baseUrl + 'canvas-icon/ditu.png', self.location.href).href)
     const blob = await response.blob()
     const imgBitmap = await createImageBitmap(blob)
     ctx.drawImage(imgBitmap, 0, 0)
@@ -107,23 +107,15 @@ const DIValMap = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 let doCom = false
 
 onmessage = (e) => {
-  const { type, url, canvas, position, val, i } = e.data
-  if (!canvasEl && type === 'init') {
-    canvasEl = canvas
+  const { type, url, canvas, position, val, i, pathname } = e.data
+  if (type === 'init') {
+    baseUrl = pathname
     ctx = canvas.getContext('2d')
     initWasm(url)
     drawPlc(null, null, null, 'init')
     for (let i = 0; i < 10; i++) {
-      drawPlc('DI', i, 0, 'draw')
-      drawPlc('DO', i, 0, 'draw')
       DIValMap[i] = 0
     }
-    for (let i = 0; i < 4; i++) {
-      drawPlc('AI', i, 0, 'draw')
-    }
-    for (let i = 0; i < 2; i++) {
-      drawPlc('AO', i, 0, 'draw')
-    }
   }
   if (type === 'set') {
     _SetTrustAI(+i, +val * 1000)

+ 5 - 2
src/views/runtime.vue

@@ -106,7 +106,7 @@ runtimeWorker.onmessage = (e) => {
   }
   if (type === 'AI') {
     port.value = `AI${i}`
-  } 
+  }
   // else {
   //   port.value = `设备时间与系统时间差值`
   // }
@@ -215,11 +215,14 @@ const handleCanvasClick = (e: MouseEvent) => {
 onMounted(() => {
   if (!canvasRef.value) return
   const offscreenCanvas = canvasRef.value.transferControlToOffscreen()
+  let pathname = window.location.pathname
+  pathname = pathname.slice(0, pathname.replace(/[?#].*/, '').lastIndexOf('/')+1)
   runtimeWorker.postMessage(
     {
       type: 'init',
       url: route.query.url,
-      canvas: offscreenCanvas
+      canvas: offscreenCanvas,
+      pathname: pathname
     },
     [offscreenCanvas]
   )