当前位置:首页 > 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实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一…

vue循环数组实现

vue循环数组实现

vue循环数组的实现方法 在Vue中循环数组主要通过v-for指令实现,以下是几种常见用法: 基本数组循环 使用v-for指令遍历数组,可以通过(item, index)的语法获取当前项和索引值:…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Refle…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-li…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…