使用joinjs繪製流程圖(二)-Paper物件的屬性和方法

^Mao^發表於2024-04-29

效果

程式碼

<template>
  <div class="app">
    <div ref="myholder" id="paper"></div>
  </div>
</template>

<script>
import * as joint from '@joint/core'
export default {
  data() {
    return {
      graph: null,
    }
  },
  mounted() {
    const namespace = joint.shapes
    var graph = new joint.dia.Graph({}, { cellNamespace: namespace })
    this.graph = graph

    var paper = new joint.dia.Paper({
      el: this.$refs.myholder,
      model: this.graph,
      width: 600,
      height: 300,
      cellViewNamespace: namespace,
      drawGrid: true,
      // 網格大小
      gridSize: 10,
      // 背景顏色
      background: {
        color: 'rgba(0, 255, 0, 0.3)',
      },
    })

    const node1 = this.drawRect({ x: 100, y: 30 }, 'Hello')
    const node2 = this.drawRect({ x: 250, y: 30 }, 'World!')
    const node3 = this.drawRect({ x: 250, y: 100 }, '哈哈哈')
    this.drawLine(node1, node2)
    this.drawLine(node1, node3)

    // 縮放
    // paper.scale(0.5, 0.5)

    // 移動,不改變畫布中元素相對位置
    // paper.translate(100, 50)
  },
  methods: {
    drawRect({ x, y }, text) {
      var rect = new joint.shapes.standard.Rectangle()
      rect.position(x, y)
      // 矩形的大小
      rect.resize(100, 40)
      rect.attr({
        body: {
          fill: 'blue',
        },
        label: {
          text,
          fill: 'white',
        },
      })
      rect.addTo(this.graph)
      return rect
    },
    drawLine(node1, node2) {
      var link = new joint.shapes.standard.Link()
      link.source(node1)
      link.target(node2)
      link.addTo(this.graph)
    },
  },
}
</script>

<style lang="less" scoped>
#paper {
  border: 1px solid;
}
</style>

相關文章