当前位置:首页 > VUE

vue实现画圆弧并着色

2026-01-12 07:01:32VUE

使用 SVG 绘制圆弧并着色

在 Vue 中可以通过 SVG 的 <path><circle> 元素绘制圆弧。SVG 的路径命令(如 A)可以精确控制圆弧的半径、起始角度和结束角度。

<template>
  <svg width="200" height="200" viewBox="0 0 200 200">
    <path 
      :d="arcPath" 
      fill="none" 
      stroke="#4CAF50" 
      stroke-width="10"
    />
  </svg>
</template>

<script>
export default {
  computed: {
    arcPath() {
      const radius = 80;
      const startAngle = 0;
      const endAngle = Math.PI * 1.5; // 270度
      const startX = 100 + radius * Math.cos(startAngle);
      const startY = 100 + radius * Math.sin(startAngle);
      const endX = 100 + radius * Math.cos(endAngle);
      const endY = 100 + radius * Math.sin(endAngle);
      const largeArcFlag = endAngle - startAngle <= Math.PI ? 0 : 1;

      return `M ${startX} ${startY} A ${radius} ${radius} 0 ${largeArcFlag} 1 ${endX} ${endY}`;
    }
  }
};
</script>

使用 Canvas 绘制圆弧

通过 Canvas 的 arc() 方法可以动态绘制圆弧,结合 Vue 的 ref 和生命周期钩子实现。

<template>
  <canvas ref="canvas" width="200" height="200"></canvas>
</template>

<script>
export default {
  mounted() {
    this.drawArc();
  },
  methods: {
    drawArc() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');
      ctx.beginPath();
      ctx.arc(100, 100, 80, 0, Math.PI * 1.5); // 圆心(100,100),半径80,0到270度
      ctx.strokeStyle = '#FF5722';
      ctx.lineWidth = 10;
      ctx.stroke();
    }
  }
};
</script>

渐变色圆弧

在 Canvas 中可以使用 createLinearGradientcreateRadialGradient 实现渐变色效果。

drawGradientArc() {
  const canvas = this.$refs.canvas;
  const ctx = canvas.getContext('2d');
  const gradient = ctx.createLinearGradient(0, 0, 200, 200);
  gradient.addColorStop(0, '#FF9800');
  gradient.addColorStop(1, '#E91E63');

  ctx.beginPath();
  ctx.arc(100, 100, 80, 0, Math.PI * 1.5);
  ctx.strokeStyle = gradient;
  ctx.lineWidth = 10;
  ctx.stroke();
}

动态圆弧动画

通过 Vue 的响应式数据和 requestAnimationFrame 实现动画效果。

<template>
  <canvas ref="canvas" width="200" height="200"></canvas>
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      animationId: null
    };
  },
  mounted() {
    this.animateArc();
  },
  beforeUnmount() {
    cancelAnimationFrame(this.animationId);
  },
  methods: {
    animateArc() {
      const canvas = this.$refs.canvas;
      const ctx = canvas.getContext('2d');

      const drawFrame = () => {
        ctx.clearRect(0, 0, 200, 200);
        ctx.beginPath();
        ctx.arc(100, 100, 80, 0, Math.PI * 2 * this.progress);
        ctx.strokeStyle = '#3F51B5';
        ctx.lineWidth = 10;
        ctx.stroke();

        this.progress += 0.01;
        if (this.progress > 1) this.progress = 0;
        this.animationId = requestAnimationFrame(drawFrame);
      };

      drawFrame();
    }
  }
};
</script>

第三方库支持

使用 vue-arc-progress 等现成库快速实现圆弧进度条。

npm install vue-arc-progress
<template>
  <arc-progress
    :progress="75"
    :thickness="8"
    :size="200"
    fill-color="#009688"
  />
</template>

<script>
import ArcProgress from 'vue-arc-progress';
export default {
  components: { ArcProgress }
};
</script>

vue实现画圆弧并着色

标签: 圆弧vue
分享给朋友:

相关文章

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…