带有 SVG 颜色渐变的圆形进度指示器?

2023-11-22

我需要制作一个带有颜色渐变的圆形进度指示器。我还需要将进度圈的“末端”弄圆。这张图片包含了我想要实现的一切:

enter image description here

此代码很接近,但没有颜色渐变:

https://codepen.io/adsfdsfhdsafkhdsafjkdhafskjds/pen/OJybqza

var control = document.getElementById('control');
var progressValue = document.querySelector('.progress__value');

var RADIUS = 54;
var CIRCUMFERENCE = 2 * Math.PI * RADIUS;

function progress(value) {
  var progress = value / 100;
  var dashoffset = CIRCUMFERENCE * (1 - progress);

  console.log('progress:', value + '%', '|', 'offset:', dashoffset)

  progressValue.style.strokeDashoffset = dashoffset;
}

control.addEventListener('input', function(event) {
  progress(event.target.valueAsNumber);
});

progressValue.style.strokeDasharray = CIRCUMFERENCE;
progress(60);
.demo {
  flex-direction: column;
  display: flex;
  width: 120px;
}

.progress {
  transform: rotate(-90deg);
}

.progress__meter,
.progress__value {
  fill: none;
}

.progress__meter {
  stroke: grey;
}

.progress__value {
  stroke: blue;
  stroke-linecap: round;
}
<div class="demo">
  <svg class="progress" width="120" height="120" viewBox="0 0 120 120">
        <circle class="progress__meter" cx="60" cy="60" r="54" stroke-width="12" />
        <circle class="progress__value" cx="60" cy="60" r="54" stroke-width="12" stroke="url(#gradient)" />
    </svg>
  <input id="control" type="range" value="60" />
</div>

It looks like this: enter image description here

我尝试添加一个linear-gradient到中风但没有效果:

stroke: linear-gradient(red, yellow);

我也尝试过stroke="url(#linearColors)",但也没有影响。

<div class="demo">  
  <svg class="progress" width="120" height="120" viewBox="0 0 120 120">
    <linearGradient id="linearColors" x1="0" y1="0" x2="1" y2="1">
      <stop offset="5%" stop-color="#01E400"></stop>
      <stop offset="25%" stop-color="#FEFF01"></stop>
      <stop offset="40%" stop-color="#FF7E00"></stop>
      <stop offset="60%" stop-color="#FB0300"></stop>
      <stop offset="80%" stop-color="#9B004A"></stop>
      <stop offset="100%" stop-color="#7D0022"></stop>
    </linearGradient>
    <circle class="progress__meter" cx="60" cy="60" r="54" stroke-width="12" />
    <circle class="progress__value" cx="60" cy="60" r="54" stroke-width="12" stroke="url(#linearColors)" />
  </svg>
  <input id="control" type="range" value="60" />
</div>

https://jsfiddle.net/yzqmvd16/


您可以使用 100 个圆圈(每个圆圈具有不同的填充)来产生渐变的错觉,而不是使用渐变。我正在使用fill-opacity属性来设置元素完全不透明或完全透明。

我希望它有帮助。

const SVG_NS = 'http://www.w3.org/2000/svg';
const CIRCUMFERENCE = base.getTotalLength()
const UNIT = CIRCUMFERENCE / 100;
let circles=[];//the array of circles

//create 100 circles each with a different fill color to create the illusion of a gradient
for(let i = 0; i<100; i++){
  let pos = base.getPointAtLength(i*UNIT);
  let o = {cx:pos.x,cy:pos.y,r:5.5,'fill-opacity':0,fill:`hsl(220,100%,${50 + (100-i)/2}%)`}
  circles.push(drawCircle(o, progress__value));  
}

progress();

control.addEventListener('input', progress);

function progress(){
  let val = control.valueAsNumber;
  for(let i = 0; i<circles.length; i++){
    if(i<=val){
    circles[i].setAttributeNS(null,'fill-opacity',1)    
    }else{
    circles[i].setAttributeNS(null,'fill-opacity',0)
    }
  } 
}

// a function to create a circle
function drawCircle(o, parent) {
  var circle = document.createElementNS(SVG_NS, 'circle');
  for (var name in o) {
    if (o.hasOwnProperty(name)) {
      circle.setAttributeNS(null, name, o[name]);
    }
  }
  parent.appendChild(circle);
  return circle;
}
svg{border:solid}

.demo {
  flex-direction: column;
  display: flex;
  width: 120px;
}

.progress__meter{
    fill: none;
}

.progress__meter {
    stroke: grey;
}
<div class="demo">  
    <svg class="progress"  viewBox="-2 -2 124 124">
        <path class="progress__meter" id="base" d="M60,6A54,54 0 0 1 60,114A54,54 0 0 1 60,6z"  stroke-width="12" />
      <g id="progress__value"></g>
    </svg>
    <input id="control" type="range" value="60" />
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有 SVG 颜色渐变的圆形进度指示器? 的相关文章

  • 用 Ruby 或 Python 解析 SVG 的库 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 SVG 是一个庞大的标准 它基于 XML 我过去曾将 SVG 解析为 XML 然而 有些事情很难 例如
  • HTML5 svg 标签和 alt 属性

    不是 HTML 方面的专家 也不是检查 HTML 解析器的正确行为的专家 根据 html5 寻找正确的行为不好或预期 我们可以稍后处理 在新的 HTML5 标签中放置 alt 属性是否有效
  • 辅助功能:推荐 SVG 和 MathML 的替代文本约定?

    Overview HTML5 现在允许 http dev w3 org html5 markup syntax html svg mathml
  • 使用 d3 在两个节点之间绘制多条边

    我一直在关注 Mike Bostock 的代码这个例子 http bl ocks org 1153292学习如何在 d3 中绘制有向图 并且想知道如何构建代码 以便可以在图中的两个节点之间添加多个边 例如 如果上例中的数据集定义为 var
  • 将外部 SVG 加载到 DOM 中,当前文档而不是子文档

    我正在尝试将外部 SVG 文档加载到一个简单的网页中 以便我可以将其用作基本的氯罗佩斯地图 然而 使用HTML 中的结果是 SVG 作为子文档加载 基本上我无法使用 jquery 按 ID 查询 SVG 路径 例如 NY css fill
  • 从左到右显示 SVG 动画

    我有两个 SVG 图像 我想将它们设置为动画 如下所示 首先显示Full Screen文本从左到右 然后用第二个 SVG 覆盖该单词Screen显示整个第二个 SVG 因此 最后我将得到第二个 svg 中的单词 Full 黑色空间 以及单词
  • 在视图内调整 SVG 图像的大小

    我有一个 FloatingActionButton 其 SVG 图像绑定到它的 src 属性 但它没有显示我需要的尺寸 如何调整它的大小以显示更大的图像 这是我的画
  • 使 SVG 中的混合模式真正起作用吗?

    我曾多次做过以下事情
  • 创建响应式 SVG 剪辑路径 / 使 SVG 响应式

    我正在尝试使用创建响应式 SVG 剪辑路径
  • 使用 d3 进行多级/分组轴标签

    我想知道是否有一种简单的方法可以在 d3 中添加多级 分层 分组轴标签 例如 如果我有一个折线图 其中 x 轴的月份名称跨越多年 那么我还希望将年份作为月份名称下方的标签 因此它看起来像这样 Oct Nov Dec Jan Feb Mar
  • 镀铬中的 SVG 条带

    I am using a svg file to produce a smooth gradient when I noticed some serious banding issues in Google Chrome 20 Even s
  • SVG 捕获鼠标坐标

    我知道之前已经有人问过有关 svg 鼠标坐标的问题 但我对我当前遇到的行为感到非常困惑 而且似乎没有一个线程能够解决它 我用于捕获坐标的方法 var pt svg createSVGPoint Created once for docume
  • 在 Jade 模板中包含 SVG xml

    是否可以创建一个 Jade mixin 它从文件系统读取文件 并将其回显到渲染的 HTML 中 我试过这个 mixin svg file var fs require fs var xml fs readFileSync file div
  • SVG 在 Firefox 中渲染得很糟糕

    我正在制作带有滑动轮播的信息图表 li 我认为 尽管 FF 中 SVG 的错误已得到解决 但 SVG 在 Firefox 中显示为像素化 有人能看到这个问题的解决办法吗 URL http weaver wp weavertest com r
  • 限制在三角形内

    我正在寻找一段通用代码 javascript 它可以与 jquery UI 一起使用来限制三角形内 div 的移动 拖动 与此类似 http stackoverflow com questions 8515900 how to constr
  • Highcharts - 使用选定的饼图切片获得 3D 效果

    在 highcharts 中 我试图使当用户选择或将鼠标悬停在饼图的切片上时 该切片会产生沿 z 轴 朝向用户 上升的效果 我试图通过 css 设置阴影过滤器并使切片的边框更宽 填充颜色相同 来实现此目的 然而 我面临的问题是切片仍然可以位
  • SVG 文本无法在 Chrome 或 Safari 中呈现

    我有一些 SVG 文本在 Firefox 上运行良好 但在 Chrome 和 Safari 中却没有出现 我努力了 向 svg 容器添加填充 以防文本被隔断 从文本中删除 xml space preserve 添加内联填充颜色
  • 如何在 React Native 上显示 SVG 文件?

    我想显示 svg 文件 我有一堆 svg 图像 但我找不到显示的方式 我尝试使用Image and Use的组成部分反应本机 svg https github com magicismight react native svg但他们不这样做
  • CSS使背景图像使用字体字符

    我想使用字体字符 例如来自 font awesome 作为输入元素的背景图像
  • D3 将现有 SVG 字符串(或元素)追加(插入)到 DIV

    我到处寻找这个问题的答案 并找到了一些我认为可能有用的资源 但最终没有让我找到答案 这里有一些 外部SVG http bl ocks org mbostock 1014829 嵌入SVG https stackoverflow com qu

随机推荐