当前位置:首页 > 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 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue动态组建实现原理

vue动态组建实现原理

Vue 动态组件的实现原理 Vue 的动态组件主要通过 component 标签和 is 属性实现,核心原理涉及组件的动态切换和渲染机制。 动态组件的实现方式 使用 component 标签和 i…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放…