繁星coding
2023-05-25 16:26:15
我正在嘗試在 d3.js 中的路徑上進行轉換。當用戶點擊一個按鈕時,路徑上應該有一個從開始到結束的過渡。我嘗試用這段代碼來做到這一點:const lineGraph = d3.select("svg") .append("path") .attr("d", "M 10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80");const length = lineGraph.node().getTotalLength();lineGraph .attr("stroke-dasharray", length + " " + length) .transition() .duration(2000) .ease(d3.easeLinear);path { stroke: black; stroke-width: 1px; fill: none;}<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script><svg></svg>路徑顯示在屏幕上,但沒有過渡。
1 回答

森欄
TA貢獻1810條經驗 獲得超5個贊
那是因為你需要從某物過渡到另一物。在使路徑出現的情況下,即從 stroke-dasharray0 length到length length:
const lineGraph = d3.select("svg")
.append("path")
.attr("d", "M 10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80");
const length = lineGraph.node().getTotalLength();
lineGraph
.attr("stroke-dasharray", "0 " + length)
.transition()
.duration(2000)
.ease(d3.easeLinear)
.attr("stroke-dasharray", length + " " + length);
path {
stroke: black;
stroke-width: 1px;
fill: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg></svg>
添加回答
舉報
0/150
提交
取消