vue canvas绘制信令图,动态显示标题、宽度、高度

news2024/10/6 8:39:55

需求:

1、 根据后端返回的数据,动态绘制出信令图
2、根据 dataStatus 返回值: 0 和 1, 判断 文字内容的颜色,0:#000,1:red
3.、根据 lineType 返回值: 0 和 1,  判断 箭头线的显示 是实线、虚线
4、根据返回的文字内容的换行符:“\r\n” 自动换行 (这步比较难,得计算高度)
最后的效果图大概是这样的:

 一、标题的动态获取


1-1、如果后端给你返回的标题是随机顺序的,这里需要根据全部标题数组做一下排序。
// 全部标题数组:
titletypeArr: ['ATP', 'MT', 'BTS', 'BSC', 'MSC', 'RBC'],


// 后端返回的随机标题数组: 
resultTitle: ['MT', 'ATP' ]


// 处理方法
this.typeArr = this.titletypeArr.filter(item => this.resultTitle.indexOf(item) !== -1)

 二、canvas绘制初始化

    // 初始化加载
    initData() {
      var mycanvas = document.getElementById("myCanvas");
      this.canvas = mycanvas;
      var context = mycanvas.getContext("2d");
      // 动态设置宽高一定要在 myCanvas 节点添加之后
      document.getElementById("myCanvas").width = this.typeArr.length * 320 - 120;
      document.getElementById("myCanvas").style.background = "#fff";
      const minHeight = window.innerHeight - 180;
      if (this.xlArr.length > 0) {
        document.getElementById("myCanvas").height =
          30 * this.xlArr.length + 80 < minHeight
            ? minHeight
            : 30 * this.xlArr.length + 80;
      } else {
        document.getElementById("myCanvas").height = minHeight;
      }
      var height = this.paddingTop + 62; // 初始值
      this.xlArr.map((v, i) => {
        const k = this.typeArr.indexOf(v.startDataDir);
        const j = this.typeArr.indexOf(v.endDataDir);
        context.font = '13px "微软雅黑"'; // 设置字体
        // 时间文字
        context.fillStyle = '#000' // 时间颜色
        context.fillText(v.recTime.split(' ')[1], 40, height);

        // 箭头
        this.paintArr(
          v,
          [this.gapX * k + this.paddingLeft, height],
          [this.gapX * j + this.paddingLeft, height],
          k < j ? "right" : "left",
          context
        );

        var maxWidth = 260; // 最大宽度,超过这个宽度会自动换行
        var words = v.showInfo.split("\r\n");

        // 文字自动换行
        this.wrapText(
          v,
          context,
          words,
          this.gapX * (k < j ? k : j) + this.paddingLeft,
          height - 10,
          maxWidth,
          this.lineHeight
        );

        if (i < this.xlArr.length - 1) {
          let nextWords = this.xlArr[i + 1].showInfo.split("\r\n");
          height += (this.lineHeight * (words.length + nextWords.length)) / 2 + 30;
        } else {
          height += this.lineHeight * words.length + 30;
        }
        // console.log(height, "height")
      })
      // 画虚线以及标题
      this.typeArr.map((v, i) => {
        this.paintText(context, v, i);
        setTimeout(() => {
          this.drawDashed(context, i);
        }, 300)
      })

      // document.getElementById('container').onscroll = (e) => {
      //   // console.log('e:', e.target)
      //   this.left = e.target.scrollLeft
      // }
      // 屏蔽所有页面 右键菜单
      // document.oncontextmenu = (event) => {
      //   event.returnValue = false
      // }
      // 屏蔽当前页面 右键菜单
      // document.getElementById('container').oncontextmenu = (event) => {
      //   event.returnValue = false
      // }
    }

三、绘制箭头

    // 箭头
    paintArr(item, s, t, direction, ctx) {
      ctx.beginPath()
      ctx.lineWidth = 1
      if (item.dataStatus == 1) {
        ctx.strokeStyle = 'red'
      } else {
        ctx.strokeStyle = '#000' // 箭头线的颜色
      }

      if (item.lineType === 1) {
        ctx.setLineDash([5, 2]); // 虚线
      }
      ctx.moveTo(s[0], s[1])
      ctx.lineTo(t[0], t[1])
      ctx.stroke()
      ctx.closePath()

      ctx.beginPath()
      if (direction === 'right') {
        ctx.moveTo(t[0] - 10, t[1] + 3)
        ctx.lineTo(t[0], t[1])
        ctx.lineTo(t[0] - 10, t[1] - 3)
      } else {
        ctx.moveTo(t[0] + 10, t[1] - 3)
        ctx.lineTo(t[0], t[1])
        ctx.lineTo(t[0] + 10, t[1] + 3)
      }
      // ctx.closePath()
      ctx.stroke()
      // ctx.fill()
    },

四、绘制 标题列的虚线

    // 标题列的虚线
    drawDashed(ctx, i) {
      ctx.beginPath()
      ctx.lineWidth = 1
      ctx.strokeStyle = '#696969' // '#FF8080'//虚线的颜色
      ctx.setLineDash([5, 2])
      ctx.moveTo(320 * i + this.paddingLeft, this.paddingTop + 40);
      ctx.lineTo(320 * i + this.paddingLeft, 400 * this.typeArr.length);
      ctx.fill()
      ctx.stroke()
      ctx.closePath()
    },

五、文字自动换行 遇到换行符换行

    // 文字自动换行 遇到换行符换行,并且超出最大宽度换行,只计算了最多显示7行的情况,超出7行得再计算
    wrapText(item, context, words, x, y, maxWidth, lineHeight) {
      // console.log(words, "words")
      let originY = y;
      let len = words.length;
      let rectWidth = 0;

      for (var n = 0; n < len; n++) {
        // 不超出一行
        var testWidth = context.measureText(words[n]).width;
        if (testWidth < maxWidth) {
          if (rectWidth < testWidth) {
            rectWidth = testWidth;
          }
        }
      }
      // 在上面循环计算出文字实际最宽的位置,画出背景色遮挡箭头
      // 画背景色遮盖箭头, 背景色自己调,跟画布统一就行
      context.fillStyle = "#fff"; // 背景颜色
      context.fillRect(
        x + this.gapX / 2 - rectWidth / 2 - 4,
        originY,
        rectWidth + 6,
        lineHeight
      ); // 填充黄色背景

      for (var n = 0; n < len; n++) {
        // 不超出一行
        var testWidth = context.measureText(words[n]).width;
        if (testWidth < maxWidth) {
          // console.log(words[n], 1);
          let currentY = y;
          if (len === 1) {
            currentY = y + 14;
          } else if (len === 2) {
            currentY = y + 2;
          } else if (len === 3) {
            currentY = y - 6;
          } else if (len === 4) {
            currentY = y - 18;
          } else if (len === 5) {
            currentY = y - 28;
          } else if (len === 6) {
            currentY = y - 38;
          } else if (len === 7) {
            currentY = y - 48;
          }

          if (item.dataStatus == 1) {
            context.fillStyle = 'red'
          } else {
            context.fillStyle = '#000' // 字体颜色
          }
          // context.fillStyle = "#000"; // 字体颜色
          context.fillText(words[n], x + this.gapX / 2 - testWidth / 2, currentY);
          if (len > 1) {
            y += lineHeight;
          }
        } else {
          console.log(words[n], 2);
          // 文字超出一行,需要换行展示
          // 实际大于页面width font-size: 12, 计算出显示多少行
          let singleWordwith = 13;
          // 计算一行显示的最大字数,以及显示多少行
          let len = Math.floor(maxWidth / singleWordwith);
          let lineCount = Math.ceil(words[n].length / len);
          for (let j = 0; j <= lineCount; j++) {
            // 截取出每行显示的字
            let word = words[n].substr(j * len, len);
            let wordWidth = context.measureText(word).width;
            // 写入画布
            // 画背景色遮盖箭头, 背景色自己调,跟画布统一就行
            context.fillStyle = "#fff";
            context.fillRect(
              x + this.gapX / 2 - wordWidth / 2,
              y - 4,
              wordWidth,
              lineHeight
            ); // 填充黄色背景
            let currentY = y;
            if (lineCount === 2) {
              currentY = y + 2;
            } else if (lineCount === 3) {
              currentY = y - 6;
            } else if (lineCount === 4) {
              currentY = y - 18;
            } else if (lineCount === 5) {
              currentY = y - 28;
            } else if (lineCount === 6) {
              currentY = y - 38;
            } else if (lineCount === 7) {
              currentY = y - 48;
            }
            if (item.dataStatus == 1) {
              context.fillStyle = 'red'
            } else {
              context.fillStyle = '#000' // 字体颜色
            }
            // context.fillStyle = "#000";
            context.fillText(word, x + this.gapX / 2 - wordWidth / 2, currentY);
            y += lineHeight; // 换行
          }
        }
      }
    },

六、模拟后端返回的数据

// signalTimeData: [
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: '2023-09-10 09:12:48',
      //     showInfo: "M136\r\nT_Train=9340940ms\r\nDT:16",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: '2023-09-10 09:12:49',
      //     showInfo: "M24\r\nT_Train=9341070ms",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: '2023-09-10 09:13:06',
      //     showInfo: "M136\r\nT_Train=9358940ms\r\nDT:19\r\n此时,M24之后ATP发送3条APDU",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 1
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: '2023-09-10 09:13:07',
      //     showInfo: "AK:20\r\n此时,M24之后RBC发送3条AK,无APDU",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 1
      //   },
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: '2023-09-10 09:13:08',
      //     showInfo: "TPDU_DR/SaPDU_D",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 1
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: '2023-09-10 09:13:09',
      //     showInfo: "TPDU_DC",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: "2023-09-10 09:13:09",
      //     showInfo: "DISC",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: "2023-09-10 09:13:09",
      //     showInfo: "NO CARRIER",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 0
      //   }
      // ],

 全部代码如下:

<template>
  <!-- :width="dialogWidth" -->
	<vxe-modal v-model="sigModal" :title="titles" width="1200" min-width="550" :height="dialogHeight" :position="{ top: '3vh' }" @close="closeEvent" resize destroy-on-close>
    <div class="con">
      <el-row style="margin-bottom:10px">
        <el-button type="primary" icon="el-icon-upload" @click="screenShot()">上传</el-button>
      </el-row>
      <div ref="screen" :style="{width: canvasWidth, height: canvasHeight}">
        <canvas id="myCanvas" :width="canvasWidth" :height="canvasHeight">
          你的浏览器还不支持canvas
        </canvas>
      </div>
    </div>
  </vxe-modal>
</template>

<script>
import html2canvas from 'html2canvas'
import { DownLoadFromTime } from '@/utils/times.js'
import { get_signallInfo } from '@/api/c3/offlineImportant.js'
import axios from 'axios'
export default {
  data() {
    return {
      uploadId: '',			
      titles: '信令图',
			dialogWidth: '90%',
      dialogHeight: '92%',
			sigModal: false,
      // 'ATP'-----'MT'------------ 'BTS'-------'BSC'----'MSC'------ 'RBC'
      //   Igsmr-R   Um_AMS/Um_BMS      Abis           A      PRI
      resultTitle: ['ATP', 'MT'], // 后台返回的随机顺序
      titletypeArr: ['ATP', 'MT', 'BTS', 'BSC', 'MSC', 'RBC'],
      typeArr: [],
      canvasWidth: '1080px',
      canvasHeight: (window.innerHeight) - 170 + 'px',
      minHeight: (window.innerHeight) - 170,
      // signalTimeData: [
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: '2023-09-10 09:12:48',
      //     showInfo: "M136\r\nT_Train=9340940ms\r\nDT:16",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: '2023-09-10 09:12:49',
      //     showInfo: "M24\r\nT_Train=9341070ms",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: '2023-09-10 09:13:06',
      //     showInfo: "M136\r\nT_Train=9358940ms\r\nDT:19\r\n此时,M24之后ATP发送3条APDU",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 1
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: '2023-09-10 09:13:07',
      //     showInfo: "AK:20\r\n此时,M24之后RBC发送3条AK,无APDU",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 1
      //   },
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: '2023-09-10 09:13:08',
      //     showInfo: "TPDU_DR/SaPDU_D",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 1
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: '2023-09-10 09:13:09',
      //     showInfo: "TPDU_DC",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "ATP",
      //     endDataDir: "MT",
      //     recTime: "2023-09-10 09:13:09",
      //     showInfo: "DISC",
      //     dataDir: 0,
      //     dataDirStr: "上行",
      //     lineType: 0,
      //     dataStatus: 0
      //   },
      //   {
      //     startDataDir: "MT",
      //     endDataDir: "ATP",
      //     recTime: "2023-09-10 09:13:09",
      //     showInfo: "NO CARRIER",
      //     dataDir: 1,
      //     dataDirStr: "下行",
      //     lineType: 0,
      //     dataStatus: 0
      //   }
      // ],
      xlArr: [],
      canvas: null,
      left: 0,
      paddingLeft: 120,
      paddingTop: 20,
      gapX: 320, // x轴间隔
      gapY: 90, // y轴间隔
      lineHeight: 20 // 行高
    }
  },
  created() {
    // this.typeArr = this.titletypeArr.filter(item => this.resultTitle.indexOf(item) !== -1)
    // this.xlArr = this.signalTimeData
    // this.canvasWidth = (this.typeArr.length * 320) - 120 + 'px'
    this.fnHight()
    window.addEventListener('resize', this.fnHight, true)
  },
  mounted() {},
  destroyed() {
    this.$destroy()
    window.removeEventListener('resize', this.fnHight, true)
  },
  methods: {
    // 获取信令图数据
    getSignallList(accidentId) {
      let parmsData = {
        accidentId: accidentId
      }
      get_signallInfo(parmsData).then(res => {
        if (res.status === 200) {
          if (res.data.code === 0) {
            if (res.data.data) {
              this.resultTitle = res.data.data.interfaceTypes
              this.typeArr = this.titletypeArr.filter(item => this.resultTitle.indexOf(item) !== -1)
              this.xlArr = res.data.data.mtDiagramInfoVoList
              this.canvasWidth = (this.typeArr.length * 320) - 120 + 'px'
            } else {
              this.$XModal.message({ content: '未查询到信令图数据.', status: 'warning' })
            }
          } else {
            this.$XModal.message({ content: res.message, status: 'error' })
          }
        }
      })
    },
    // 截屏
    screenShot() {
      html2canvas(this.$refs.screen, {
        backgroundColor: '#FFFFFF',
        useCORS: true
      }).then((canvas) => {
        // 获取到canvas
        canvas.toBlob(blob => {
          // 将二进制对象的内容 转成file
          const file = new File([blob], Date.now() + '.png', { type: 'image/png' })
          const formData = new FormData()
          formData.append('file', file)
          formData.append('uploadId', this.uploadId)
          // 发起请求
          axios({
            headers: {
              'Authorization': this.$store.state.user.token,
              'showInfo-Type': 'multipart/form-data'
            },
            method: 'post',
            url: '/api/offline/analysisContent/uploadReportPdf',
            data: formData,
          }).then((res) => {
            // 上传成功
            if (res.status === 200) {
              this.$XModal.message({ content: '上传成功', status: 'success'})
              this.$emit('sumitSuccess', 'ok')
            }
          }).catch((error) => {
            // 上传失败 执行对应操作
            this.$XModal.message({ content: '上传失败', status: 'error' })
          })
          
        }, 'image/png')

        if (navigator.msSaveBlob) { // IE10+ 
          let blob = canvas.msToBlob(); 
          return navigator.msSaveBlob(blob, name);
        } else {
          let imageurl = canvas.toDataURL('image/png')
          const newTime = DownLoadFromTime(new Date())
          //这里需要自己选择命名规则
          let imagename = '信令图_' + 'G1884' + '_' + newTime
          this.fileDownload(imageurl, imagename)
        }  
      })
    },
    // 下载截屏图片
    fileDownload(downloadUrl, downloadName) {
      let aLink = document.createElement("a")
      aLink.style.display = "none"
      aLink.href = downloadUrl
      aLink.download = `${downloadName}.png`
      // 触发点击-然后移除
      document.body.appendChild(aLink)
      aLink.click()
      document.body.removeChild(aLink)
    },
    closeEvent() {
      this.sigModal = false
    },
    fnHight() {
      setTimeout(() => {
        this.minHeight = (window.innerHeight) - 170
      }, 300)
    },
    // 标题的边框
    paintText(ctx, text, i) {
      // ctx.fillStyle = '#FEFECE'
      ctx.fillStyle = '#FFF'
      ctx.fillRect(320 * i + this.paddingLeft - 30, this.paddingTop, 60, 30); // 填充黄色背景
      // ctx.strokeStyle = 'red'
      ctx.strokeStyle = '#000' // '#A80036' // 标题的边框颜色
      ctx.strokeRect(320 * i + this.paddingLeft - 30, this.paddingTop, 60, 30); // 设置边框

      ctx.font = '12px "微软雅黑"' // 设置字体
      ctx.textBaseline = 'bottom' // 设置字体底线对齐绘制基线
      ctx.textAlign = 'left' // 设置字体对齐的方式
      ctx.fillStyle = 'Black'
      ctx.fillText(text, 320 * i + this.paddingLeft - 10, this.paddingTop + 22); // 填充文字标题
      // ctx.closePath()
    },
    // 箭头
    paintArr(item, s, t, direction, ctx) {
      ctx.beginPath()
      ctx.lineWidth = 1
      if (item.dataStatus == 1) {
        ctx.strokeStyle = 'red'
      } else {
        ctx.strokeStyle = '#000' // 箭头线的颜色
      }

      if (item.lineType === 1) {
        ctx.setLineDash([5, 2]); // 虚线
      }
      ctx.moveTo(s[0], s[1])
      ctx.lineTo(t[0], t[1])
      ctx.stroke()
      ctx.closePath()

      ctx.beginPath()
      if (direction === 'right') {
        ctx.moveTo(t[0] - 10, t[1] + 3)
        ctx.lineTo(t[0], t[1])
        ctx.lineTo(t[0] - 10, t[1] - 3)
      } else {
        ctx.moveTo(t[0] + 10, t[1] - 3)
        ctx.lineTo(t[0], t[1])
        ctx.lineTo(t[0] + 10, t[1] + 3)
      }
      // ctx.closePath()
      ctx.stroke()
      // ctx.fill()
    },
    // 标题列的虚线
    drawDashed(ctx, i) {
      ctx.beginPath()
      ctx.lineWidth = 1
      ctx.strokeStyle = '#696969' // '#FF8080'//虚线的颜色
      ctx.setLineDash([5, 2])
      ctx.moveTo(320 * i + this.paddingLeft, this.paddingTop + 40);
      ctx.lineTo(320 * i + this.paddingLeft, 400 * this.typeArr.length);
      ctx.fill()
      ctx.stroke()
      ctx.closePath()
    },
    // 文字自动换行 遇到换行符换行,并且超出最大宽度换行,只计算了最多显示7行的情况,超出7行得再计算
    wrapText(item, context, words, x, y, maxWidth, lineHeight) {
      // console.log(words, "words")
      let originY = y;
      let len = words.length;
      let rectWidth = 0;

      for (var n = 0; n < len; n++) {
        // 不超出一行
        var testWidth = context.measureText(words[n]).width;
        if (testWidth < maxWidth) {
          if (rectWidth < testWidth) {
            rectWidth = testWidth;
          }
        }
      }
      // 在上面循环计算出文字实际最宽的位置,画出背景色遮挡箭头
      // 画背景色遮盖箭头, 背景色自己调,跟画布统一就行
      context.fillStyle = "#fff"; // 背景颜色
      context.fillRect(
        x + this.gapX / 2 - rectWidth / 2 - 4,
        originY,
        rectWidth + 6,
        lineHeight
      ); // 填充黄色背景

      for (var n = 0; n < len; n++) {
        // 不超出一行
        var testWidth = context.measureText(words[n]).width;
        if (testWidth < maxWidth) {
          // console.log(words[n], 1);
          let currentY = y;
          if (len === 1) {
            currentY = y + 14;
          } else if (len === 2) {
            currentY = y + 2;
          } else if (len === 3) {
            currentY = y - 6;
          } else if (len === 4) {
            currentY = y - 18;
          } else if (len === 5) {
            currentY = y - 28;
          } else if (len === 6) {
            currentY = y - 38;
          } else if (len === 7) {
            currentY = y - 48;
          }

          if (item.dataStatus == 1) {
            context.fillStyle = 'red'
          } else {
            context.fillStyle = '#000' // 字体颜色
          }
          // context.fillStyle = "#000"; // 字体颜色
          context.fillText(words[n], x + this.gapX / 2 - testWidth / 2, currentY);
          if (len > 1) {
            y += lineHeight;
          }
        } else {
          console.log(words[n], 2);
          // 文字超出一行,需要换行展示
          // 实际大于页面width font-size: 12, 计算出显示多少行
          let singleWordwith = 13;
          // 计算一行显示的最大字数,以及显示多少行
          let len = Math.floor(maxWidth / singleWordwith);
          let lineCount = Math.ceil(words[n].length / len);
          for (let j = 0; j <= lineCount; j++) {
            // 截取出每行显示的字
            let word = words[n].substr(j * len, len);
            let wordWidth = context.measureText(word).width;
            // 写入画布
            // 画背景色遮盖箭头, 背景色自己调,跟画布统一就行
            context.fillStyle = "#fff";
            context.fillRect(
              x + this.gapX / 2 - wordWidth / 2,
              y - 4,
              wordWidth,
              lineHeight
            ); // 填充黄色背景
            let currentY = y;
            if (lineCount === 2) {
              currentY = y + 2;
            } else if (lineCount === 3) {
              currentY = y - 6;
            } else if (lineCount === 4) {
              currentY = y - 18;
            } else if (lineCount === 5) {
              currentY = y - 28;
            } else if (lineCount === 6) {
              currentY = y - 38;
            } else if (lineCount === 7) {
              currentY = y - 48;
            }
            if (item.dataStatus == 1) {
              context.fillStyle = 'red'
            } else {
              context.fillStyle = '#000' // 字体颜色
            }
            // context.fillStyle = "#000";
            context.fillText(word, x + this.gapX / 2 - wordWidth / 2, currentY);
            y += lineHeight; // 换行
          }
        }
      }
    },
    // 初始化加载
    initData() {
      var mycanvas = document.getElementById("myCanvas");
      this.canvas = mycanvas;
      var context = mycanvas.getContext("2d");
      // 动态设置宽高一定要在 myCanvas 节点添加之后
      document.getElementById("myCanvas").width = this.typeArr.length * 320 - 120;
      document.getElementById("myCanvas").style.background = "#fff";
      const minHeight = window.innerHeight - 180;
      if (this.xlArr.length > 0) {
        document.getElementById("myCanvas").height =
          30 * this.xlArr.length + 80 < minHeight
            ? minHeight
            : 30 * this.xlArr.length + 80;
      } else {
        document.getElementById("myCanvas").height = minHeight;
      }
      var height = this.paddingTop + 62; // 初始值
      this.xlArr.map((v, i) => {
        const k = this.typeArr.indexOf(v.startDataDir);
        const j = this.typeArr.indexOf(v.endDataDir);
        context.font = '13px "微软雅黑"'; // 设置字体
        // 时间文字
        context.fillStyle = '#000' // 时间颜色
        context.fillText(v.recTime.split(' ')[1], 40, height);

        // 箭头
        this.paintArr(
          v,
          [this.gapX * k + this.paddingLeft, height],
          [this.gapX * j + this.paddingLeft, height],
          k < j ? "right" : "left",
          context
        );

        var maxWidth = 260; // 最大宽度,超过这个宽度会自动换行
        var words = v.showInfo.split("\r\n");

        // 文字自动换行
        this.wrapText(
          v,
          context,
          words,
          this.gapX * (k < j ? k : j) + this.paddingLeft,
          height - 10,
          maxWidth,
          this.lineHeight
        );

        if (i < this.xlArr.length - 1) {
          let nextWords = this.xlArr[i + 1].showInfo.split("\r\n");
          height += (this.lineHeight * (words.length + nextWords.length)) / 2 + 30;
        } else {
          height += this.lineHeight * words.length + 30;
        }
        // console.log(height, "height")
      })
      // 画虚线以及标题
      this.typeArr.map((v, i) => {
        this.paintText(context, v, i);
        setTimeout(() => {
          this.drawDashed(context, i);
        }, 300)
      })

      // document.getElementById('container').onscroll = (e) => {
      //   // console.log('e:', e.target)
      //   this.left = e.target.scrollLeft
      // }
      // 屏蔽所有页面 右键菜单
      // document.oncontextmenu = (event) => {
      //   event.returnValue = false
      // }
      // 屏蔽当前页面 右键菜单
      // document.getElementById('container').oncontextmenu = (event) => {
      //   event.returnValue = false
      // }
    }
  }
}
</script>

<style lang="scss" scoped>
  .con {
    position: relative;
  }
  .topTitle{
    position: absolute;
    background: #fff;
    top: 38px;
    padding-left: 15px;
    // width: 2580px;
    display: flex;
    li {
      display: inline-block;
      width: 320px;
      margin: 3px 0;
    }
    li:nth-last-child(1) {
      width: 100px;
    }
    span {
      // border: 1px solid #A80036;
      border: 1px solid #000;
      padding: 5px 10px;
      // background-color: #FEFECE;
      background-color: #fff;
      display: inline-block;
    }
  }
  #container {
    // overflow-x: scroll;
    // overflow-y: scroll;
    overflow: hidden;
  }
  #canvas {
    display: block;
    background-color: #fff;
  }
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1592170.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

人员抽烟AI检测算法原理介绍及实际场景应用

抽烟检测AI算法是一种基于计算机视觉和深度学习技术的先进工具&#xff0c;旨在准确识别并监测个体是否抽烟。该算法通过训练大量图像数据&#xff0c;使模型能够识别出抽烟行为的关键特征&#xff0c;如烟雾、手部动作和口部形态等。 在原理上&#xff0c;抽烟检测AI算法主要…

AI预测体彩排3第2弹【2024年4月13日预测--第1套算法开始计算第2次测试】

各位小伙伴&#xff0c;今天实在抱歉&#xff0c;周末回了趟老家&#xff0c;回来比较晚了&#xff0c;数据今天上午跑完后就回老家了&#xff0c;晚上8点多才回来&#xff0c;赶紧把预测结果发出来吧&#xff0c;虽然有点晚了&#xff0c;但是咱们前面说过了&#xff0c;目前的…

ZL-099动物行为学视频分析系统

简单介绍&#xff1a; 动物行为学视频分析系统是一套通过视频摄像机和计算机&#xff0c;采用图像处理技术&#xff0c;自动跟踪和记录动物活动的通用型运动轨迹记录分析系统&#xff0c;可以应用在神经药理&#xff0c;学习记忆药理&#xff0c;药理和新药神经系统一般药理毒理…

element UI 设置type=“textarea“ 禁止输入框缩放

背景 在 Element UI 中&#xff0c;当您使用 el-input 组件并设置 type"textarea" 时&#xff0c;默认情况下&#xff0c;用户可以通过拖动输入框的右下角来调整其大小。如果您想禁止这种缩放行为&#xff0c;需要使用 CSS 来覆盖默认的浏览器行为。 注意上图&#x…

CH254X 8051芯片手册介绍

1 8051CPU 8051是一种8位元的单芯片微控制器&#xff0c;属于MCS-51单芯片的一种&#xff0c;由英特尔(Intel)公司于1981年制造。Intel公司将MCS51的核心技术授权给了很多其它公司&#xff0c;所以有很多公司在做以8051为核心的单片机&#xff0c;如Atmel、飞利浦、深联华等公…

【Tars-go】腾讯微服务框架学习使用03-- TarsUp协议

3 TarsUP协议 统一通信协议 TarsTup | TarsDocs (tarscloud.github.io) TarsDocs/base at master TarsCloud/TarsDocs (github.com) &#xff1a; 有关于tars的所有介绍 每一个rpc调用双方都约定一套数据序列化协议&#xff0c;gprc用的是protobuff&#xff0c;tarsgo是统一…

《黑马点评》Redis高并发项目实战笔记(上)P1~P45

P1 Redis企业实战课程介绍 P2 短信登录 导入黑马点评项目 首先在数据库连接下新建一个数据库hmdp&#xff0c;然后右键hmdp下的表&#xff0c;选择运行SQL文件&#xff0c;然后指定运行文件hmdp.sql即可&#xff08;建议MySQL的版本在5.7及以上&#xff09;&#xff1a; 下面这…

Vivado抓信号——提高效率的工具化生成XDC(Python脚本)

操作目录 一、要抓取信号的txt列表二、操作流程 通常情况下&#xff0c;Vivado上板抓取信号的方法主要有两类&#xff1a; &#xff08;1&#xff09;通过在信号前添加(mark_debug“true”)&#xff0c;综合完之后点击Set Up Debug&#xff0c;将需要抓取的信号添加进去&#x…

电脑端微信截图文字识别功能效率更高了

近期发现微信中的截图文字识别比QQ中的截图文字识别效率高更高&#xff0c;效果更好。 使用方法&#xff1a; 安装电脑端微信客户端&#xff1a;https://weixin.qq.com/(如果没有下载&#xff0c;可以安装一下) 默认截图组合快捷键是&#xff1a;ALTA (使用下来感觉不是很顺手…

网桥的原理

网桥的原理 1.1 桥接的概念 简单来说&#xff0c;桥接就是把一台机器上的若干个网络接口“连接”起来。其结果是&#xff0c;其中一个网口收到的报文会被复制给其他网口并发送出去。以使得网口之间的报文能够互相转发。 交换机就是这样一个设备&#xff0c;它有若干个网口…

基于SpringBoot+Vue的毕业设计管理系统(源码+文档+部署+讲解)

一.系统概述 二十一世纪我们的社会进入了信息时代&#xff0c;信息管理系统的建立&#xff0c;大大提高了人们信息化水平。传统的管理方式对时间、地点的限制太多&#xff0c;而在线管理系统刚好能满足这些需求&#xff0c;在线管理系统突破了传统管理方式的局限性。于是本文针…

GoC模拟试题2

GoC测试模拟题(2017.3.23)第1题&#xff1a;领奖台 题目描述 小C同学在学校GoC编程比赛中获得了一等奖&#xff0c;他希望在领奖会上能站在一个漂亮的领奖台上。设计的领奖台如下图&#xff0c;请你帮忙使用GoC编程绘制。 说明&#xff1a; 上图中红色数字是标明尺寸的&#…

4月全新热文高科技,套用模板一键生成热文,没脑子拷贝,第二天出盈利

撰写热门文章&#xff0c;如今日头条或微信公众号文章&#xff0c;通常需要多长时间呢&#xff1f;从构思主题、搜集资料&#xff0c;到撰写成文&#xff0c;整个过程至少需要1小时&#xff0c;有时甚至可能需要2小时。 项目 地 址&#xff1a;laoa1.cn/1627.html 现在&…

Fence同步

在《Android图形显示系统》没有介绍到帧同步的相关概念&#xff0c;这里简单介绍补充一下。 在图形显示系统中&#xff0c;图形缓存GraphicBuffer可以被不同的硬件来访问&#xff0c;如CPU、GPU、HWC都可以对缓存进行读写&#xff0c;如果同时对图形缓存进行操作&#xff0c;有…

数据结构--循环队列

1.队列的定义: 和栈相反,队列(queue)是一种先进先出(first in first out,缩写为FIFO)的线性表.它只允许在表的一端进行插入,而在另一端删除元素. 在队列中,允许插入的一端叫做队尾(rear),允许删除的一端则称为队头(front). 2.循环队列的设计图示: 3.循环队列的结构设计: ty…

napi系列学习进阶篇——NAPI生命周期

什么是NAPI的生命周期 我们都知道&#xff0c;程序的生命周期是指程序从启动&#xff0c;运行到最后的结束的整个过程。生命周期的管理自然是指控制程序的启动&#xff0c;调用以及结束的方法。 而NAPI中的生命周期又是怎样的呢&#xff1f;如下图所示&#xff1a; 从图上我们…

foreach无法修改数组值解决方案

效果展示&#xff1a; 解决办法&#xff1a; this.sportList.forEach((item,index) >{let that this;if(item.idinfo.id) {that.sportList[index].sportTime e.detail.value} }) 这里小编解释下&#xff0c;将this赋值给that通常是为了在回调函数或者异步代码中保持对Vu…

C++11 设计模式4. 抽象工厂(Abstract Factory)模式

问题的提出 从前面我们已经使用了工厂方法模式 解决了一些问题。 现在 策划又提出了新的需求&#xff1a;对于各个怪物&#xff0c;在不同的场景下&#xff0c;怪物的面板数值会发生变化&#xff0c; //怪物分类&#xff1a;亡灵类&#xff0c;元素类&#xff0c;机械类 …