Vue3实现SVG矢量图绘制拖拽效果

<script setup>
import { reactive, computed } from 'vue'
import dynamics from 'https://cdn.skypack.dev/dynamics.js'

const headerHeight = 120

let isDragging = false
const start = { x: 0, y: 0 }
const c = ref({ x: headerHeight, y: headerHeight })

// svg是基于XML语法的图像格式,全称:Scalable Vector Graphics,即可缩放矢量图。
// 参考:https://blog.csdn.net/seevc/article/details/132669663
// svg路径起始点绘制
// M命令,moveto缩写,表示绘制的起点坐标
// L命令,lineto的缩写,表示绘制一条直线,如:L50,10,表示从上一个结束点到(50,10)绘制一条直线;
// Q命令,绘制二次贝塞尔曲线,要定义控制点和终点坐标,如:Q150,-300 300,0,表示控制点是(150,-300),终点坐标(300,0)
// 动态修改Q命令中的控制点坐标
const headerPath = computed(() => {
    //从(0,0)坐标作为绘制起点坐标,直线左侧起始点(0,640),右侧终点(640,120),坐标形式(x,y),水平方向为x坐标,垂直方向为y坐标
    //贝塞尔曲线控制点为动态值(c.x,c.y),终点为(0,120)
    return `M0,0 L640,0 640,${headerHeight}Q${c.x},${c.y} 0,${headerHeight}`
})

const contentPosition = computed(() => {
    const dy = c.y - headerHeight
    const dampen = dy > 0 ? 2 : 4
    return {
        transform: `translate(0,${dy / dampen}px)`
    }
})

function startDrag(e) {
    e = e.changedTouches ? e.changedTouches[0] : e
    isDragging = true
    start.x = e.pageX
    start.y = e.pageY
}

function onDrag(e) {
    e = e.changedTouches ? e.changedTouches[0] : e
    if (isDragging) {
        c.x = headerHeight + (e.pageX - start.x)
        const dy = e.pageY - start.y
        const dampen = dy > 0 ? 1.5 : 4
        c.y = headerHeight + dy / dampen
    }
}

function stopDrag() {
    if (isDragging) {
        isDragging = false
        dynamics.animate(
            c,
            { x: headerHeight, y: headerHeight },
            { type: dynamics.spring, duration: 700, friction: 280 }
        )
    }
}
</script>

<template>
    <div class="draggable" @mousedown="startDrag"  @mousemove="onDrag" @mouseup="stopDrag" @mouseleave="stopDrag"
          @touchstart="startDrag" @touchmove="onDrag" @touchend="stopDrag" >
        <svg class="bg" width="640" height="560">
            <path :d="headerPath" fill="#3F51B5"></path>
        </svg>
        <div class="header">Drag Me</div>
        <div class="content" :style="contentPosition">Hello</div>
    </div>
</template>

<style scoped>
.draggable {
    background-color: #fff;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    width: 640px;
    height: 480px;
    overflow: hidden;
    margin: 30px auto;
    position: relative;
    text-align: center;
    font-size: 14px;
    font-weight: 300;
    user-select: none;
    border-radius: 8px;
}

.bg {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 0;
}

.header,
.content {
    position: relative;
    z-index: 1;
    padding: 30px;
    box-sizing: border-box;
}

.header {
    color: #fff;
    height: 120px;
    font-size: 2em;
    font-weight: bold;
}
</style>

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue.js是一个基于JavaScript的开源框架,用于构建用户界面。而D3.js是一个用于在网页上创建可交互数据可视化的JavaScript库。结合Vue.js和D3.js可以很方便地绘制流程图。 首先,我们需要在Vue.js项目中安装D3.js,可以通过npm安装。在终端中运行以下命令: npm install d3 安装完成后,在Vue.js组件中引入D3.js库: import * as d3 from 'd3'; 接下来,我们可以在Vue.js组件的生命周期函数中创建一个D3.js绘制流程图的函数。比如,在mounted钩子函数中: mounted() { this.drawFlowChart(); } 然后,在drawFlowChart函数中,我们可以使用D3.js提供的方法和API来绘制流程图。这个过程可以分为以下几步: 1. 创建svg元素,用于承载流程图的绘制: const svg = d3.select('body') .append('svg') .attr('width', '100%') .attr('height', '100%'); 2. 定义绘制流程图所需的数据,比如节点和连接线的位置和样式信息: const nodes = [ { id: 1, name: 'Node 1' }, { id: 2, name: 'Node 2' }, { id: 3, name: 'Node 3' }, // ... ]; const links = [ { source: 1, target: 2 }, { source: 2, target: 3 }, // ... ]; 3. 创建节点和连接线的元素,并设置其位置和样式: const node = svg.selectAll('.node') .data(nodes) .enter() .append('g') .attr('class', 'node') .attr('transform', d => `translate(${d.x}, ${d.y})`); node.append('rect') .attr('width', 100) .attr('height', 50); const link = svg.selectAll('.link') .data(links) .enter() .append('line') .attr('class', 'link') .attr('x1', d => d.source.x) .attr('y1', d => d.source.y) .attr('x2', d => d.target.x) .attr('y2', d => d.target.y); 4. 可以根据需要添加交互功能,比如节点的点击事件、鼠标悬停效果等等。 至此,我们使用Vue.js和D3.js成功绘制了一个流程图。通过Vue.js的数据绑定和D3.js的数据驱动视图的特性,可以很方便地更新流程图的数据和样式,实现交互和动态效果。同时,D3.js也提供了丰富的API可以用于进行复杂的数据可视化操作,比如节点之间的动画过渡效果、缩放和拖拽等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南腔北调-pilmar

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值