首页 > 其他分享 >vue3+jointjs 使用模板添加元素

vue3+jointjs 使用模板添加元素

时间:2023-04-25 09:59:26浏览次数:40  
标签:const paper ui jointjs vue3 import 节点 模板

关于如何在 Vue3 和 JointJS 中使用拖拽模板来创建节点元素,可以按照以下步骤进行:

1. 安装 JointJS

使用 npm 进行安装:

```
npm install jointjs
```

2. 在 Vue3 中创建 JointJS 容器

在 Vue3 中创建一个组件,用于创建 JointJS 的画布和节点。在组件的生命周期方法 `mounted` 中,创建 JointJS 的 `Paper` 和 `Graph` 对象:

```vue
<template>
  <div class="jointjs-container"></div>
</template>

<script>
import { Paper, Graph } from 'jointjs'

export default {
  mounted() {
    const paper = new Paper({
      el: '.jointjs-container',
      width: 800,
      height: 600,
      model: new Graph(),
      gridSize: 10,
      drawGrid: true,
      background: {
        color: '#f9f9f9'
      }
    })
  }
}
</script>

<style scoped>
.jointjs-container {
  border: 1px solid #ccc;
}
</style>
```

3. 创建节点模板

在 JointJS 中,可以使用 `joint.shapes` 来创建节点模板。创建一个名为 `my-custom-shape` 的节点模板:

```javascript
const myCustomShape = new joint.shapes.standard.Circle({
  position: { x: 100, y: 100 },
  size: { width: 50, height: 50 },
  attrs: {
    body: {
      fill: 'lightblue'
    },
    label: {
      text: 'My Custom Shape'
    }
  }
})
```

这个模板是一个圆形节点,填充色为浅蓝色,显示一个标签文本。

4. 定义拖拽逻辑

在组件中定义拖拽逻辑,可以使用 `jquery-ui` 的拖拽和放置插件。首先,需要在组件的 `mounted` 方法中引入 `jquery-ui`:

```vue
<script>
import { Paper, Graph } from 'jointjs'
import $ from 'jquery'
import 'jquery-ui/ui/widgets/draggable'
import 'jquery-ui/ui/widgets/droppable'

export default {
  mounted() {
    const paper = new Paper({
      // ...
    })

    $(paper.el).droppable({
      tolerance: 'fit',
      drop: (event, ui) => {
        const x = ui.offset.left
        const y = ui.offset.top
        // 在这里创建新的节点元素
      }
    })

    $('#my-custom-shape').draggable({
      cursor: 'move',
      helper: 'clone',
      stop: (event, ui) => {
        // 在这里拖动结束时删除复制的节点
        ui.helper.remove()
      }
    })
  }
}
</script>
```

在 `drop` 回调函数中,获取鼠标落下位置的坐标,接下来可以在这里创建新的节点元素。

在 `draggable` 方法中,定义允许拖拽和放置的节点模板。在这里使用 `helper: 'clone'`,允许复制原节点,而不是移动节点。

5. 创建新节点

在 `drop` 回调函数中,通过 `paper.model.addCell()` 方法创建新节点,然后将节点添加到画布中:

```vue
<script>
export default {
  mounted() {
    const paper = new Paper({
      // ...
    })

    $(paper.el).droppable({
      // ...
      drop: (event, ui) => {
        const x = ui.offset.left
        const y = ui.offset.top
        const cell = myCustomShape.clone()
        cell.position(x, y)
        paper.model.addCell(cell)
      }
    })

    $('#my-custom-shape').draggable({
      // ...
    })
  }
}
</script>
```

使用 `myCustomShape.clone()` 复制节点模板,然后使用 `cell.position(x, y)` 设置新节点的位置坐标。最后,使用 `paper.model.addCell(cell)` 添加新节点到画布中。

完整代码如下:

```vue
<template>
  <div class="jointjs-container"></div>
</template>

<script>
import { Paper, Graph } from 'jointjs'
import $ from 'jquery'
import 'jquery-ui/ui/widgets/draggable'
import 'jquery-ui/ui/widgets/droppable'

const myCustomShape = new joint.shapes.standard.Circle({
  position: { x: 100, y: 100 },
  size: { width: 50, height: 50 },
  attrs: {
    body: {
      fill: 'lightblue'
    },
    label: {
      text: 'My Custom Shape'
    }
  }
})

export default {
  mounted() {
    const paper = new Paper({
      el: '.jointjs-container',
      width: 800,
      height: 600,
      model: new Graph(),
      gridSize: 10,
      drawGrid: true,
      background: {
        color: '#f9f9f9'
      }
    })

    $(paper.el).droppable({
      tolerance: 'fit',
      drop: (event, ui) => {
        const x = ui.offset.left
        const y = ui.offset.top
        const cell = myCustomShape.clone()
        cell.position(x, y)
        paper.model.addCell(cell)
      }
    })

    $('#my-custom-shape').draggable({
      cursor: 'move',
      helper: 'clone',
      stop: (event, ui) => {
        ui.helper.remove()
      }
    })
  }
}
</script>

<style scoped>
.jointjs-container {
  border: 1px solid #ccc;
}
</style>
```

 

标签:const,paper,ui,jointjs,vue3,import,节点,模板
From: https://www.cnblogs.com/lljboke/p/17351698.html

相关文章

  • IDEA 类头注释模板配置
    File–>settings–>Editor–>FileandCodeTemplates–>Files修改头文件/***desc**@Author红尘过客*@DateTime${YEAR}-${MONTH}-${DAY}${HOUR}:${MINUTE}:${SECOND}*/......
  • vue3+jointjs demo
    下面是使用Vue3和JointJS添加元素的示例代码:1.安装JointJS```terminalnpminstalljointjs--save```2.创建JointJS图形```javascriptimport{ref,onMounted}from'vue';import*asjointfrom'jointjs';exportdefault{setup(){constgraphC......
  • Vue3 Vue3中其他的改变
    视频六、其他1.全局API的转移Vue2.x有许多全局API和配置。例如:注册全局组件、注册全局指令等。//注册全局组件Vue.component('MyButton',{data:()=>({count:0}),template:'<button@click="count++">Clicked{{count}}times.</button>......
  • ai问答:使用 Vue3 组合式API 和 TS 配置 axios 拦截器 http错误状态
    通过axios.create()可以创建一个axios实例axiosInstance,参数如下:baseURL:请求前缀timeout:超时时间headers:请求头默认配置:import{defineComponent}from'vue'importaxiosfrom'axios'exportdefaultdefineComponent({setup(){//实例-默认配置......
  • Django4全栈进阶之路16 template模板的基础模板
    <!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>{%blocktitle%}MySite{%endblock%}</title>{%blockstyles%}{%endblock%}</head><body><header><h1>MySite</h......
  • vue3 自定义组件双向绑定(modelValue)
    参考链接:https://huaweicloud.csdn.net/638edf68dacf622b8df8d152.html父组件:<Customabcref="editor"v-model="data.introduction":min-height="400"name="职能"placeholder="请编辑"/>子组件<divclass="tinymc......
  • 记录在vue3项目中使用wangeditor富文本编译器以及微信小程序中的渲染
    首先,管理后台中的使用npminstallwangeditor//f封装成了组件,以下是组件中的内容<template>  <divstyle="border:1pxsolid#ccc;maxwidth:600px">   <!--工具栏-->   <Toolbar    style="border-bottom:1pxsolid#ccc"    :......
  • Django4全栈进阶之路13 template模板
    在Django4中,模板是用于生成HTML响应的文件。模板将动态数据和静态HTML组合在一起,以便最终呈现给用户的页面。Django4中的模板使用Django模板语言(DjangoTemplateLanguage,简称DT)编写。DT与HTML类似,但包含一些特殊的标记和语法,用于动态地生成HTML内容。以下是一......
  • ai问答:使使用 Vue3 组合式API 和 TypeScript 父子组件demo
    这是一个使用Vue3组合式API和TypeScript的简单父子组件示例父组件Parent.vue:<template><div><p>{{msg}}</p><Child/></div></template><scriptlang="ts">import{ref}from'vue'import......
  • 二分模板 不会乱的
    (29条消息)不需要考虑mid+1、mid-1的二分查找模板,希望大家都能学会_二分查找如果lightmid不加1_一支彩色铅笔的博客-CSDN博客非常好的博客,爱来自中国二分查找为什么总是写错?_哔哩哔哩_bilibili非常好的视频,爱来自中国......