cancas绘制思维导图

这段代码展示了如何使用HTML5 Canvas元素创建一个复杂的流程图,包括产品信息和责任分配。流程图根据提供的数据动态调整大小,每个部分如产品名称、保障期限、保费等都精确绘制,并通过线条连接。此外,代码还提供了将绘制的图像转换为Base64 URL的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;

    }
    #canvas {
      border: 1px solid #000;
      margin: 50px;
    }
  </style>
</head>

<body>
  <canvas id="canvas"></canvas>
  <img src="" alt="" id="img">
  <script>

    let canvas = document.querySelector('canvas')
    let ctx = canvas.getContext('2d')

    class Xmind {
      constructor(option) {
        this.canvas = document.querySelector(option.id)
        this.ctx = this.canvas.getContext('2d')
        this.data = option.data
        this.lineGutter = option.lineGutter || 5  //行间距
        this.blockGutter = option.blockGutter || 50 // 块间距
        this.type="liab"
        this.b1Width = 200
        this.b2Width = 200
        this.b3Width = 300
        this.liabPadding = 10
        this.len = 40 //连接线长度
        this.lineWidth = 1.5
        this.left = 30
        this.right = 30
        this.top = 60
        this.bottom = 60
        this.red = '#f33'
        this.fontSize = option.fontSize || 14
        this.bgColors = ['#fae3d9','#bbded6','#d4edf4']
        this.fontColors = ['#8ac6d1']
        this.lineColor = option.lineColor || '#dedede'
        this.init()
      }
      init() {
        this.setCanvasWidth()
        this.setCanvasHeight()
        this.setBackground()
        this.darwLiab()
        let height = this.drawProduct()
        this.drawProduct(height)

      }
      setCanvasHeight() {
        let height = this.darwLiab(false)
        this.canvas.height = height
      }
      setCanvasWidth() {
        let width = this.b1Width + this.b2Width + this.b3Width + this.len * 4 + this.lineWidth * 2 + this.left + this.right
        this.canvas.width = width
      }
      setBackground() {
        this.ctx.beginPath()
        this.ctx.fillStyle = "#fff"
        this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height)
      }
      darwLiab(isDraw = true) {
        let height = this.top
        let liabs = this.data.dutyData
        let x = this.left + this.b1Width + this.b2Width + this.lineWidth * 2 + this.len * 4
        this.ctx.save()
        let dutyHeight = []
        liabs.forEach((item, index) => {
          this.ctx.save()
          let itemHeight = 0
          let totalHeight = 0
          item.liabsList.forEach((liab, liabINdex) => {
            this.ctx.save()
            let movex = this.left + this.b1Width + this.b2Width + this.lineWidth * 2 + this.len * 4
            this.ctx.translate(movex, height)
            let proNameHeight = this.drawText(liab.proName, 0, 0, this.b3Width, isDraw, item)
            height += proNameHeight + this.lineGutter
            let liabHeight = this.drawText(liab.productLiabNameFormula, 0, proNameHeight + this.lineGutter, this.b3Width, isDraw, item) +10
            height += liabHeight + this.lineGutter
            // 责任左边线
            this.drawXLine(0, (proNameHeight + liabHeight + this.lineGutter) / 2)
            this.ctx.save()
            //移动坐标原点 准备画竖线
            this.ctx.translate(-this.len, (proNameHeight + liabHeight + this.lineGutter) / 2)

            if (liabINdex == 0 || liabINdex == item.liabsList.length - 1) {
              itemHeight += (proNameHeight + liabHeight + this.lineGutter * 2) / 2
            } else {
              itemHeight += proNameHeight + liabHeight + this.lineGutter * 2
            }
            totalHeight += liabHeight + this.lineGutter * 2 + proNameHeight
            if (liabINdex == item.liabsList.length - 1) {
              dutyHeight.push(totalHeight)
            }
            if (item.liabsList.length > 1 && liabINdex == item.liabsList.length - 1) {
              this.drwaYLine(0, -itemHeight, 0, 0)
            }
            this.ctx.restore()
            this.ctx.restore()
          })
          this.ctx.restore()
          this.ctx.save()
          height += this.blockGutter
          let liabX = this.left + this.b1Width + this.lineWidth + this.len * 2
          this.ctx.translate(liabX, this.getPos(dutyHeight, index))
          this.drawXLine(0, 0)
          this.drawXLine(this.b2Width + this.len, 0)
          let dutyNameHeight = this.drawDutyName(item)
          if (index >0) {
            this.drwaYLine(-this.len, -this.calcHeight(dutyHeight,index), -this.len, 0)

          }
          this.ctx.restore()
        })
        this.ctx.restore()
        return height
      }
      calcHeight(arr,index) {
        return (arr[index]+arr[index-1])/2 + this.blockGutter
      }
      drawProduct(height) {
        this.ctx.save()
        if(height) {
          this.ctx.translate(this.left, this.canvas.height / 2 -height/2)

        }
        let productList = this.data.productList
        let arr = []
        let y = 0
        productList.forEach((item, index) => {
          this.ctx.save()
          let product = item.product
          let h1 = this.drawText(product.productName, 0, 0, this.b1Width, false)
          let h2 = this.drawText('保额:' + product.amount, 0, 0, this.b1Width, false)
          let h3 = this.drawText('交费期间:' + product.payPeriod, 0, 0, this.b1Width, false)
          let h4 = this.drawText('保障期间:' + product.coverage, 0, 0, this.b1Width, false)
          let h5 = this.drawText('保费:' + product.premInitial, 0, 0, this.b1Width, false)
          let rectHeight = h1 + h2 + h3 + h4 + h5 + this.lineGutter * 4 + 15 +10
          arr.push(rectHeight)
          if(height) {
            y += rectHeight + this.blockGutter
            this.ctx.translate(0, index==0?0:y)
            ctx.fillStyle = this.bgColors[index%3]
            ctx.fillRect(0, 0, this.b1Width, rectHeight)
            this.drawText(product.productName, 10, 0, this.b1Width)
            this.drawText('保       额:' + product.amount, 10, h1 + this.lineGutter+10, this.b1Width)
            this.drawText('交费期间:' + product.payPeriod, 10, h1+h2 + this.lineGutter*2+10, this.b1Width)
            this.drawText('保障期间:' + product.coverage, 10, h1 + h2+h3 + this.lineGutter * 3+10, this.b1Width)
            this.drawText('保       费:' + product.premInitial, 10, h1 + h2 + h3 + h4+ this.lineGutter * 4+10, this.b1Width)

          }

          this.ctx.restore()
        })
        if(height) {
          this.drawXLine(this.b1Width + this.len, height/2)
          this.ctx.restore()

        }
        return this.getSum(arr)
      }
      getSum(arr) {
        let sum = 0
        arr.forEach(item => {
          sum += item
        })
        return sum + (arr.length - 1) * this.blockGutter
      }
      drawDutyName(duty) {
        this.ctx.save()
        let height = this.drawText(duty.liabName + duty.amount + '元', 0, 0, this.b2Width-40, false)
        this.ctx.translate(0, -(height + 20) / 2)
        this.ctx.fillStyle = this.bgColors[0]
        this.ctx.fillRect(0, 0, this.b1Width, height + 20)
        this.drawText(duty.liabName + duty.amount + '元', 10, 5, this.b2Width-40)
        this.ctx.restore()
        return height
      }
      getPos(arr, index) {
        let sum = 0
        arr.forEach((item, i) => {
          if (i == arr.length - 1) {
            sum += item / 2 + this.top
          } else {
            sum += item
          }
        })
        sum += this.blockGutter * (arr.length - 1)
        return sum - 2
      }
      sum(arr, index) {
        return arr.reduce((total, num, idx) => {
          return total + num + this.blockGutter * idx
        }, 0)
      }
      drawXLine(x, y) {
        this.ctx.beginPath()
        this.ctx.lineWidth = this.lineWidth
        this.ctx.strokeStyle = this.lineColor
        this.ctx.moveTo(x, y)
        this.ctx.lineTo(x - this.len - 1, y)
        this.ctx.stroke()
      }
      drwaYLine(x, y, x1, y1) {
        this.ctx.beginPath()
        this.ctx.lineWidth = this.lineWidth
        this.ctx.strokeStyle = this.lineColor
        this.ctx.moveTo(x, y)
        this.ctx.lineTo(x1, y1)
        this.ctx.stroke()
      }
      drawText(text, x, y, w, isDraw = true, item) {
        this.ctx.beginPath()
        var chr = text.split("");
        var temp = "";
        var row = [];
        this.ctx.font = `${this.fontSize}px Arial`;
        this.ctx.fillStyle = "black";
        this.ctx.textBaseline = "middle";
        for (var a = 0; a < chr.length; a++) {
          if (this.ctx.measureText(temp).width < w) {
          }
          else {
            row.push(temp);
            temp = "";
          }
          temp += chr[a];
        }

        row.push(temp);
        if (isDraw) {
          for (var b = 0; b < row.length; b++) {
            this.ctx.fillText(row[b], x, y + (b + 1) * this.fontSize);
          }

        }
        return (row.length - 1) * this.lineGutter + row.length * this.fontSize
      }
      getBase64Url() {
        return this.canvas.toDataURL('image/png')
      }
    }

    let data = {
      "productList": [
        {
           "product": {
            "coverage": "终身",
            "amount": "50万元",
            "payPeriod": "18年交",
            "premInitial": "8380元",
            "productName": "童佳保(尊享版)",
          }
        },
        
      ],
      "dutyData": [
        {
          "liabName": "重疾保险金",
          "amount": 2175000,
          "liabsList": [
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,6次为限,分6组,理赔间隔期180天",
             
              "proName": "童佳保(尊享版)",
            
            },
            {
              "productLiabNameFormula": "110种重疾依次给付50万元/60万元/70万元/80万元/90万元/100万元,6次为限,分6组,理赔间隔期180天",
              "proName": "百万守护(典藏版)重疾",
             
            },
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,分5组,5次为限,理赔间隔期180天",
              "proName": "童佳倍(2020版)",
              
            },
            {
              "productLiabNameFormula": "29周岁前确诊重疾额外给付175000元",
              "proName": "童佳倍(2020版)",
             
            }
          ],
          "sort": "6"
        },
        {
          "liabName": "重疾保险金",
          "amount": 2175000,
          "liabsList": [
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,6次为限,分6组,理赔间隔期180天",

              "proName": "童佳保(尊享版)",

            },
            {
              "productLiabNameFormula": "110种重疾依次给付50万元/60万元/70万元/80万元/90万元/100万元,6次为限,分6组,理赔间隔期180天",
              "proName": "百万守护(典藏版)重疾",

            },
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,分5组,5次为限,理赔间隔期180天",
              "proName": "童佳倍(2020版)",

            },
            {
              "productLiabNameFormula": "29周岁前确诊重疾额外给付175000元",
              "proName": "童佳倍(2020版)",

            }
          ],
          "sort": "6"
        },
        {
          "liabName": "重疾保险金",
          "amount": 2175000,
          "liabsList": [
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,6次为限,分6组,理赔间隔期180天",

              "proName": "童佳保(尊享版)",

            },
            {
              "productLiabNameFormula": "110种重疾依次给付50万元/60万元/70万元/80万元/90万元/100万元,6次为限,分6组,理赔间隔期180天",
              "proName": "百万守护(典藏版)重疾",

            },
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,分5组,5次为限,理赔间隔期180天",
              "proName": "童佳倍(2020版)",

            },
            {
              "productLiabNameFormula": "29周岁前确诊重疾额外给付175000元",
              "proName": "童佳倍(2020版)",

            }
          ],
          "sort": "6"
        },
        {
          "liabName": "重疾保险金",
          "amount": 2175000,
          "liabsList": [
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,6次为限,分6组,理赔间隔期180天",

              "proName": "童佳保(尊享版)",

            },
            {
              "productLiabNameFormula": "110种重疾依次给付50万元/60万元/70万元/80万元/90万元/100万元,6次为限,分6组,理赔间隔期180天",
              "proName": "百万守护(典藏版)重疾",

            },
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,分5组,5次为限,理赔间隔期180天",
              "proName": "童佳倍(2020版)",

            },
            {
              "productLiabNameFormula": "29周岁前确诊重疾额外给付175000元",
              "proName": "童佳倍(2020版)",

            }
          ],
          "sort": "6"
        },
        {
          "liabName": "重疾保险金",
          "amount": 2175000,
          "liabsList": [
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,6次为限,分6组,理赔间隔期180天",

              "proName": "童佳保(尊享版)",

            },
            {
              "productLiabNameFormula": "110种重疾依次给付50万元/60万元/70万元/80万元/90万元/100万元,6次为限,分6组,理赔间隔期180天",
              "proName": "百万守护(典藏版)重疾",

            },
            {
              "productLiabNameFormula": "100种重疾每次给付50万元,分5组,5次为限,理赔间隔期180天",
              "proName": "童佳倍(2020版)",

            },
            {
              "productLiabNameFormula": "29周岁前确诊重疾额外给付175000元",
              "proName": "童佳倍(2020版)",

            }
          ],
          "sort": "6"
        },
        

      ],
    }

    let xmind = new Xmind({ id: '#canvas', data: data })
    let img = document.querySelector('#img')
    img.src = xmind.getBase64Url()
  </script>
</body>

</html>

效果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值