我有一个通过更改stroke-dashoffset
路径属性来更新的进度条。但是,每次更新时,我都需要计算海军路径端点(圆边)的 x 和 y 坐标。将来我将在这些点添加标签。一段时间以来,我一直在为这个问题头疼。我尝试过使用类似的东西const x = radius * Math.sin(Math.PI * 2 * angle / 360);
,const y = radius * Math.cos(Math.PI * 2 * angle / 360);
但我认为我没有正确使用它们。有人可以帮助我吗?旁注我只使用 Vanilla JS。
const setStrokeDashOffset = (e) => {
const dashOffset = e.target.getAttribute('data-attr');
document
.getElementById('progress-meter')
.setAttribute('stroke-dashoffset', dashOffset);
}
const btn = document.querySelectorAll('button');
btn.forEach(x => {
x.addEventListener('click', (e) => {
setStrokeDashOffset(e)
});
});
.donut-progress__svg {
transform: scaleX(-1);
}
.donut-progress__circle {
fill: none;
stroke: none;
}
.donut-progress__path-elapsed {
stroke: #aaaaaa;
stroke-width: 10;
}
.donut-progress__path-remaining {
stroke: navy;
stroke-linecap: round;
stroke-width: 10;
transform: rotate(90deg);
transform-origin: center;
transition: 0.3s linear all;
}
.donut-progress__path-start {
stroke: orange;
stroke-linecap: round;
stroke-width: 10;
transform: rotate(90deg);
transform-origin: center;
}
<svg id="donut-progress__svg" class="donut-progress__svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g class="donut-progress__circle">
<circle class="donut-progress__path-elapsed" cx="50" cy="50" r="45"></circle>
<path id="progress-meter" stroke-dasharray="283 283" stroke-dashoffset="165.08333333333334" class="donut-progress__path-remaining" stroke="#4764ae" d="
M 50, 50
m -45, 0
a 45,45 0 1,0 90,0
a 45,45 0 1,0 -90,0
"></path>
<path opacity="1" id="progress-meter-start" class="donut-progress__path-start" stroke-dashoffset="282" stroke-dasharray="283" d="
M 50, 50
m -45, 0
a 45,45 0 1,0 90,0
a 45,45 0 1,0 -90,0
"></path>
</g>
</svg>
<button data-attr="100" class="one">move to 100</button>
<button data-attr="150" class="one">move to 150</button>
<button data-attr="200" class="one">move to 200</button>