当前位置:首页 > JavaScript

js实现圆环

2026-03-13 22:41:18JavaScript

使用 SVG 实现圆环

SVG(Scalable Vector Graphics)是一种基于 XML 的矢量图形格式,适合绘制圆环。以下是一个简单的 SVG 圆环实现:

<svg width="200" height="200">
  <circle cx="100" cy="100" r="80" fill="none" stroke="#ccc" stroke-width="10"/>
  <circle cx="100" cy="100" r="80" fill="none" stroke="#f00" stroke-width="10" stroke-dasharray="251.2" stroke-dashoffset="125.6"/>
</svg>

cxcy 定义圆心坐标,r 是半径。stroke-dasharray 控制虚线样式,stroke-dashoffset 控制起始偏移量。

js实现圆环

使用 Canvas 绘制圆环

Canvas 提供了更灵活的绘制方式,适合动态圆环:

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = 80;
const lineWidth = 10;

// 背景圆环
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
ctx.strokeStyle = '#ccc';
ctx.lineWidth = lineWidth;
ctx.stroke();

// 前景圆环
ctx.beginPath();
ctx.arc(centerX, centerY, radius, -Math.PI/2, Math.PI);
ctx.strokeStyle = '#f00';
ctx.lineWidth = lineWidth;
ctx.stroke();

使用 CSS 实现圆环

纯 CSS 方案利用 border 和 border-radius 属性:

js实现圆环

<div class="ring-container">
  <div class="ring-background"></div>
  <div class="ring-foreground"></div>
</div>

<style>
.ring-container {
  position: relative;
  width: 200px;
  height: 200px;
}
.ring-background {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 10px solid #ccc;
  border-radius: 50%;
  box-sizing: border-box;
}
.ring-foreground {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 10px solid transparent;
  border-top-color: #f00;
  border-radius: 50%;
  box-sizing: border-box;
  transform: rotate(135deg);
}
</style>

动态进度圆环(Canvas)

实现一个可交互的进度圆环:

function drawProgressRing(progress) {
  const canvas = document.getElementById('progressCanvas');
  const ctx = canvas.getContext('2d');
  const centerX = canvas.width / 2;
  const centerY = canvas.height / 2;
  const radius = 80;
  const lineWidth = 10;

  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // 背景
  ctx.beginPath();
  ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
  ctx.strokeStyle = '#eee';
  ctx.lineWidth = lineWidth;
  ctx.stroke();

  // 进度
  ctx.beginPath();
  ctx.arc(centerX, centerY, radius, -Math.PI/2, -Math.PI/2 + Math.PI*2*progress/100);
  ctx.strokeStyle = '#4CAF50';
  ctx.lineWidth = lineWidth;
  ctx.stroke();

  // 文字
  ctx.font = '24px Arial';
  ctx.fillStyle = '#333';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillText(`${progress}%`, centerX, centerY);
}

// 使用示例
drawProgressRing(75);  // 绘制75%的进度

动画效果实现

为圆环添加动画效果:

function animateRing(targetProgress, duration = 1000) {
  let startTime = null;
  let currentProgress = 0;

  function step(timestamp) {
    if (!startTime) startTime = timestamp;
    const elapsed = timestamp - startTime;
    const progress = Math.min(elapsed / duration, 1);
    currentProgress = progress * targetProgress;

    drawProgressRing(currentProgress);

    if (progress < 1) {
      requestAnimationFrame(step);
    }
  }

  requestAnimationFrame(step);
}

// 启动动画
animateRing(80);  // 动画到80%

这些方法提供了从简单到复杂的圆环实现方案,可根据具体需求选择最适合的方式。SVG 适合静态展示,Canvas 适合动态和交互场景,CSS 方案则无需 JavaScript 即可实现基本效果。

标签: 圆环js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document.…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Jav…

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScrip…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…