当前位置:首页 > VUE

vue实现画圆弧并着色

2026-03-06 19:56:39VUE

使用 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 centerX = 100;
      const centerY = 100;
      const radius = 80;
      const startAngle = 0;
      const endAngle = Math.PI * 1.5; // 270度
      const start = this.polarToCartesian(centerX, centerY, radius, endAngle);
      const end = this.polarToCartesian(centerX, centerY, radius, startAngle);
      const largeArcFlag = endAngle - startAngle <= Math.PI ? "0" : "1";
      return `M ${start.x} ${start.y} A ${radius} ${radius} 0 ${largeArcFlag} 0 ${end.x} ${end.y}`;
    }
  },
  methods: {
    polarToCartesian(centerX, centerY, radius, angleInRadians) {
      return {
        x: centerX + radius * Math.cos(angleInRadians),
        y: centerY + radius * Math.sin(angleInRadians)
      };
    }
  }
};
</script>

使用 Canvas 绘制动态圆弧

通过 Vue 的 ref 和生命周期钩子访问 Canvas 上下文,实现动态圆弧绘制和着色。

vue实现画圆弧并着色

<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");
      const centerX = 100;
      const centerY = 100;
      const radius = 80;

      ctx.beginPath();
      ctx.arc(centerX, centerY, radius, 0, Math.PI * 1.5, false);
      ctx.strokeStyle = "#FF5722";
      ctx.lineWidth = 10;
      ctx.stroke();

      // 填充扇形(可选)
      ctx.beginPath();
      ctx.moveTo(centerX, centerY);
      ctx.arc(centerX, centerY, radius, 0, Math.PI * 0.5, false);
      ctx.fillStyle = "rgba(33, 150, 243, 0.5)";
      ctx.fill();
    }
  }
};
</script>

使用第三方库(如 D3.js)

对于复杂圆弧需求(如仪表盘),D3.js 提供更高级的弧形生成器。

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

<script>
import * as d3 from "d3";
export default {
  mounted() {
    this.renderArc();
  },
  methods: {
    renderArc() {
      const svg = d3.select(this.$refs.svg);
      const arcGenerator = d3.arc()
        .innerRadius(50)
        .outerRadius(80)
        .startAngle(0)
        .endAngle(Math.PI * 1.2);

      svg.append("path")
        .attr("d", arcGenerator())
        .attr("fill", "#9C27B0")
        .attr("stroke", "#673AB7")
        .attr("stroke-width", 2);
    }
  }
};
</script>

关键参数说明

  • SVG 路径命令A rx ry x-axis-rotation large-arc-flag sweep-flag x y

    vue实现画圆弧并着色

    • large-arc-flag:1 表示大于 180 度的弧,0 表示小弧
    • sweep-flag:1 表示顺时针方向,0 表示逆时针
  • Canvas 的 arc() 方法
    arc(x, y, radius, startAngle, endAngle, anticlockwise)

    • 角度单位为弧度(0 到 2π)
    • anticlockwisetrue 时逆时针绘制
  • 动态响应:在 Vue 中可将圆弧参数设为 dataprops,通过 watch 触发重绘。

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

相关文章

vue实现列表显示

vue实现列表显示

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

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

antd vue 实现

antd vue 实现

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

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…