当前位置:首页 > VUE

vue实现动态飞线

2026-01-23 05:17:42VUE

Vue 实现动态飞线效果

动态飞线效果常用于数据可视化或地图展示中,通过动画线条连接两个坐标点。以下是基于 Vue 和 Canvas 的实现方法:

安装依赖

需要安装动画库 gsap 和绘图工具库 fabric.js

npm install gsap fabric

基础实现代码

在 Vue 组件中创建 Canvas 画布并绘制飞线:

<template>
  <div class="container">
    <canvas ref="flylineCanvas" width="800" height="600"></canvas>
  </div>
</template>

<script>
import { fabric } from 'fabric'
import { gsap } from 'gsap'

export default {
  mounted() {
    this.initFlyline()
  },
  methods: {
    initFlyline() {
      const canvas = new fabric.Canvas(this.$refs.flylineCanvas)
      const startPoint = { x: 100, y: 100 }
      const endPoint = { x: 500, y: 400 }

      const line = new fabric.Line(
        [startPoint.x, startPoint.y, startPoint.x, startPoint.y],
        {
          stroke: '#1890ff',
          strokeWidth: 2,
          selectable: false
        }
      )

      canvas.add(line)

      gsap.to(line, {
        duration: 2,
        x1: endPoint.x,
        y1: endPoint.y,
        onUpdate: () => canvas.renderAll(),
        repeat: -1,
        yoyo: true
      })
    }
  }
}
</script>

曲线飞线实现

要实现贝塞尔曲线效果的飞线:

drawCurveFlyline() {
  const canvas = new fabric.Canvas(this.$refs.flylineCanvas)
  const path = new fabric.Path('', {
    stroke: '#ff4d4f',
    fill: '',
    strokeWidth: 2
  })

  canvas.add(path)

  let progress = 0
  const animate = () => {
    progress += 0.01
    if (progress > 1) progress = 0

    const start = { x: 100, y: 100 }
    const end = { x: 700, y: 500 }
    const cp1 = { x: 300, y: 0 }
    const cp2 = { x: 500, y: 600 }

    const point = this.cubicBezier(start, cp1, cp2, end, progress)
    path.set('path', `M ${start.x} ${start.y} Q ${cp1.x} ${cp1.y} ${point.x} ${point.y}`)
    canvas.renderAll()
    requestAnimationFrame(animate)
  }
  animate()
},

cubicBezier(p0, p1, p2, p3, t) {
  const x = Math.pow(1-t,3)*p0.x + 3*Math.pow(1-t,2)*t*p1.x + 3*(1-t)*Math.pow(t,2)*p2.x + Math.pow(t,3)*p3.x
  const y = Math.pow(1-t,3)*p0.y + 3*Math.pow(1-t,2)*t*p1.y + 3*(1-t)*Math.pow(t,2)*p2.y + Math.pow(t,3)*p3.y
  return { x, y }
}

使用第三方库

对于更复杂的效果,可以使用专业的地图飞线库:

  1. 安装 echarts

    npm install echarts
  2. 实现地图飞线:

    vue实现动态飞线

    initEchartsFlyline() {
    const chart = this.$echarts.init(this.$refs.flylineChart)
    const option = {
     series: [{
       type: 'lines',
       coordinateSystem: 'geo',
       effect: {
         show: true,
         period: 6,
         trailLength: 0.7,
         symbolSize: 3
       },
       lineStyle: {
         color: '#ffba00',
         width: 1,
         curveness: 0.2
       },
       data: [{
         coords: [[116.46, 39.92], [113.65, 34.76]],
         lineStyle: { color: '#ff0000' }
       }]
     }]
    }
    chart.setOption(option)
    }

性能优化建议

  1. 使用 requestAnimationFrame 替代 setInterval
  2. 对于大量飞线,考虑使用 WebGL 渲染
  3. 动画结束后及时清除不需要的对象
  4. 使用防抖控制高频渲染

以上方法可根据具体需求调整参数,如飞线颜色、宽度、动画时长等。实际项目中可能需要结合地图坐标系统进行位置转换。

标签: 动态vue
分享给朋友:

相关文章

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue实现Pop

vue实现Pop

Vue 实现 Popover 组件的方法 使用 Vue 内置指令 v-show/v-if 和事件监听 通过 Vue 的指令和事件绑定实现基础的 Popover 功能。定义一个布尔值控制 Popover…