javascript - d3.js 中的路径转换不起作用
问题描述
我正在尝试在 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>
路径显示在屏幕上,但没有过渡。
解决方案
那是因为你需要从某事过渡到其他事。在出现路径的情况下,即从 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>
推荐阅读
- keras - 为什么提取的特征有很多零 i keras vgg16?
- python - Q媒体播放器。如何播放具有多个音频的视频?
- python-3.x - 如何使用 Constrained np.polyfit 或 curve_fit 学习或查找权重向量并删除所有数据点的异常值?
- verilog - 始终在 0 时使用 posedge 触发以某种方式工作
- java - 线程主异常->霍夫曼树解码
- node.js - 如何将节点应用程序转换为 VS Code 扩展程序?
- c++ - Windows 中的“找不到标识符”错误,但 Mac 中没有
- c# - Docusign.Esign.dll 继承 RestSharp 依赖的依赖问题
- modelica - 在 modelica 中找不到 DLL(使用 VS 2015 构建)
- html - 如何隐藏列数中的第一项而不使第一列消失?