当前位置:首页 > VUE

vue实现动态飞线

2026-01-23 05:17:42VUE

Vue 实现动态飞线效果

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

安装依赖

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

npm install gsap fabric

基础实现代码

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

vue实现动态飞线

<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 }
}

使用第三方库

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

vue实现动态飞线

  1. 安装 echarts

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

    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 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件…

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const Bas…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense…