当前位置:首页 > VUE

vue水波球实现

2026-02-17 07:35:48VUE

Vue 水波球实现方法

水波球(也称为波浪球或液体填充效果)通常用于数据可视化或进度展示。以下是基于 Vue 的实现方法,结合 Canvas 或 SVG 技术。

使用 Canvas 实现

通过 Canvas 动态绘制波浪路径,结合动画实现波浪效果。

模板部分

<template>
  <div class="wave-ball">
    <canvas ref="canvas" :width="size" :height="size"></canvas>
    <div class="progress-text">{{ progress }}%</div>
  </div>
</template>

脚本部分

export default {
  props: {
    size: { type: Number, default: 200 },
    progress: { type: Number, default: 50 },
    color: { type: String, default: '#3eaf7c' }
  },
  mounted() {
    this.drawWave();
  },
  methods: {
    drawWave() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      const radius = this.size / 2;
      let time = 0;

      const animate = () => {
        ctx.clearRect(0, 0, this.size, this.size);

        // 绘制圆形背景
        ctx.beginPath();
        ctx.arc(radius, radius, radius, 0, Math.PI * 2);
        ctx.fillStyle = '#f5f5f5';
        ctx.fill();

        // 绘制波浪路径
        ctx.beginPath();
        for (let x = 0; x < this.size; x++) {
          const y = Math.sin(x * 0.05 + time) * 10 + 
                   (this.size - (this.progress / 100 * this.size));
          ctx.lineTo(x, y);
        }
        ctx.lineTo(this.size, this.size);
        ctx.lineTo(0, this.size);
        ctx.closePath();
        ctx.fillStyle = this.color;
        ctx.fill();

        time += 0.1;
        requestAnimationFrame(animate);
      };
      animate();
    }
  },
  watch: {
    progress() {
      this.drawWave();
    }
  }
};

样式部分

vue水波球实现

.wave-ball {
  position: relative;
}
.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  font-weight: bold;
}

使用 SVG 实现

通过 SVG 路径动态生成波浪效果,适合需要矢量缩放场景。

模板部分

<template>
  <div class="wave-ball">
    <svg :width="size" :height="size" viewBox="0 0 200 200">
      <circle cx="100" cy="100" r="90" fill="#f5f5f5" />
      <path :d="wavePath" :fill="color" />
      <text x="100" y="110" text-anchor="middle" font-size="24">{{ progress }}%</text>
    </svg>
  </div>
</template>

脚本部分

vue水波球实现

export default {
  props: {
    size: { type: Number, default: 200 },
    progress: { type: Number, default: 50 },
    color: { type: String, default: '#3eaf7c' }
  },
  data() {
    return {
      time: 0
    };
  },
  computed: {
    wavePath() {
      const height = 200 - (this.progress / 100 * 200);
      let path = `M 0 ${height + 10}`;

      for (let x = 0; x <= 200; x += 10) {
        const y = Math.sin(x * 0.05 + this.time) * 10 + height;
        path += ` L ${x} ${y}`;
      }

      path += ` L 200 200 L 0 200 Z`;
      return path;
    }
  },
  mounted() {
    setInterval(() => {
      this.time += 0.1;
    }, 100);
  }
};

使用第三方库

对于复杂需求,可考虑以下库:

  1. vue-wavesurfer:专为音频波形设计的库,支持自定义样式。
  2. progressbar.js:轻量级库,支持圆形和波浪进度条。

安装示例

npm install progressbar.js

集成代码

import ProgressBar from 'progressbar.js';

export default {
  mounted() {
    const bar = new ProgressBar.Circle(this.$refs.container, {
      strokeWidth: 6,
      trailColor: '#f5f5f5',
      trailWidth: 1,
      easing: 'easeInOut',
      duration: 1400,
      text: {
        autoStyleContainer: false
      },
      from: { color: '#3eaf7c', width: 1 },
      to: { color: '#3eaf7c', width: 6 },
      step: (state, circle) => {
        circle.path.setAttribute('stroke', state.color);
        circle.path.setAttribute('stroke-width', state.width);
        circle.setText(this.progress + '%');
      }
    });
    bar.animate(this.progress / 100);
  }
};

关键优化点

  • 性能优化:使用 requestAnimationFrame 替代 setInterval 实现平滑动画。
  • 响应式设计:通过 watch 监听进度变化,实时更新视图。
  • 可配置性:通过 props 暴露颜色、尺寸等参数,增强组件复用性。

以上方法可根据项目需求选择,Canvas 适合高性能动态效果,SVG 更适合矢量场景,第三方库能快速实现复杂功能。

标签: 水波vue
分享给朋友:

相关文章

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成列…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…

vue实现slidedown

vue实现slidedown

Vue 实现 SlideDown 动画效果 在 Vue 中实现 SlideDown 效果可以通过 CSS 过渡或动画结合 Vue 的过渡系统完成。以下是几种常见方法: 使用 Vue Transiti…

vue实现tooltips

vue实现tooltips

Vue 实现 Tooltips 的方法 使用原生 HTML title 属性 在 Vue 模板中直接使用 HTML 原生 title 属性实现简单的提示效果。这种方式无需额外依赖,但功能较为基础,无法…

vue实现的setdata

vue实现的setdata

Vue 中实现类似小程序 setData 的方法 在小程序中,setData 方法用于异步更新页面数据并触发视图渲染。Vue 中可以通过以下几种方式实现类似功能: 直接使用 Vue 的响应式系统 V…