D3和React Hooks:沿沿着一条弧移动元素

nwnhqdif  于 12个月前  发布在  React
关注(0)|答案(1)|浏览(146)

我正在创建一个带有三角形小标记的 Jmeter ,我已经在这里工作了:https://codesandbox.io/s/blazing-sun-teo9oe?file=/src/Gauge.tsx
除了标记过渡,我需要它沿着 Jmeter 弧移动,所以当组件 prop 从40变为80时,标记将从40移动到80,现在它总是从50或弧的中间开始,然后移动到最终值。
随意更改index.tsx文件中的Gauge prop value以查看问题。过渡持续时间为10秒,以更容易地查看问题。

hfwmuf9z

hfwmuf9z1#

D3和React =300 kB(缩小!)
你可以在原生JavaScript中实现:

<svg-gauge value="35" color="red"></svg-gauge>
    <svg-gauge value="50" color="hotpink"></svg-gauge>
    <svg-gauge value="75" color="blue"></svg-gauge>

字符串
x1c 0d1x的数据

customElements.define("svg-gauge", class extends HTMLElement {
    connectedCallback() {
      let arc = "m20 45a35 35 0 1170 0";
      let col = this.getAttribute("color")||"green";
      this.innerHTML = 
      `<input type="range" min="0" max="100" step="5" value="0"`+ // delete 2 lines
      ` oninput="this.parentNode.percent=this.value" /><br>`+ // just for demo
      `<svg viewbox="0 0 100 100">
         <path d="${arc}" stroke="grey" stroke-width="6" fill="none" 
               stroke-linecap="round"></path>
         <path id="P" d="${arc}" stroke="${col}" stroke-width="8" pathLength="100" 
               fill="none" stroke-linecap="round" stroke-dasharray="75 35"></path>
         <circle cx="0" cy="0" fill="#fff" r="4" stroke="${col}" stroke-width="3">
           <animateMotion dur="0.5s" keyPoints="0;0.75" 
                          fill="freeze" keyTimes="0;1" calcMode="linear" path="${arc}">
           </animateMotion></circle>
         <text x="55%" y="40%" text-anchor="middle"/>
       </svg>`;
      this.style.display='inline-block';
      this.percent = this.getAttribute("value") || "50";
    }
    set percent(val = 0) {
      this.setAttribute("value", val);
      let dash = val + " " + (105 - val);
      this.querySelector("#P").setAttribute('stroke-dasharray', dash);
      this.querySelector("animateMotion").setAttribute('keyPoints', '0;'+val/100);
      this.querySelector("text").innerHTML =`${val} %`;
      this.querySelector("animateMotion").beginElement();
      this.querySelector("input").value = val;
    }
  })
svg-gauge { width:180px }
<h3>Drag sliders:</h3>
<svg-gauge value="35" color="red"></svg-gauge>
<svg-gauge value="50" color="hotpink"></svg-gauge>
<svg-gauge value="75" color="blue"></svg-gauge>

相关问题