JointJS与vue集成初体验

清疚 2022-10-12 14:59 240阅读 0赞

将 JointJS 添加到 Vue 项目中

安装需要的包

npm 方式

  1. npm install jointjs

yarn 方式

  1. yarn add jointjs

在 vue 中使用 jointjs

  1. <template>
  2. <div class="canvas" ref="canvas"></div>
  3. </template>
  4. <script>
  5. import * as joint from 'jointjs'
  6. export default {
  7. data () {
  8. return {
  9. paper: null
  10. }
  11. },
  12. mounted() {
  13. this.init()
  14. },
  15. methods: {
  16. init() {
  17. let graph = new joint.dia.Graph
  18. this.paper = new joint.dia.Paper({
  19. el: this.$refs.canvas,
  20. model: graph,
  21. width: 600,
  22. height: 100,
  23. gridSize: 1
  24. })
  25. let rect = new joint.shapes.standard.Rectangle()
  26. rect.position(100, 30)
  27. rect.resize(100, 40)
  28. rect.attr({
  29. body: {
  30. fill: 'blue'
  31. },
  32. label: {
  33. text: 'Hello',
  34. fill: 'white'
  35. }
  36. })
  37. rect.addTo(graph)
  38. let rect2 = rect.clone()
  39. rect2.translate(300, 0)
  40. rect2.attr('label/text', 'World!')
  41. rect2.addTo(graph)
  42. let link = new joint.shapes.standard.Link()
  43. link.source(rect)
  44. link.target(rect2)
  45. link.addTo(graph)
  46. }
  47. }
  48. }
  49. </script>
  50. <style>
  51. </style>

效果图:
在这里插入图片描述
如果我们想给 画布 paper 添加网格背景的话
先引入 css 样式

  1. // 显示网格背景颜色需要引入样式
  2. import 'jointjs/dist/joint.css'
  3. ...
  4. this.paper = new joint.dia.Paper({
  5. el: this.$refs.canvas,
  6. model: graph,
  7. width: 600,
  8. height: 100,
  9. gridSize: 10, // 网格大小
  10. drawGrid: true, // 显示网格
  11. background: {
  12. color: 'rgba(0, 255, 0, 0.3)'
  13. }
  14. })
  15. ...

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue 3.0 体验

    Vue3.0 beta也出来一段时间了,最近一直在看react,对于vue3.0倒是没怎么关注,想着等正式版出来再说,不过最近事情不多,还是抽出了一点时间,试了一下新版的Vue

    相关 体验egg-vue-singal-page

    egg在koa框架的基础上进行了封装,并集合了现今热门的vue、react框架,支持服务端渲染,可以说是全端开发的一个优秀实践。关于demo和api,可以在[官网][Link