Skip to content

Instantly share code, notes, and snippets.

@denisemauldin
Last active June 30, 2017 16:47
Show Gist options
  • Save denisemauldin/0de3bedc47e00a84e635be498a584516 to your computer and use it in GitHub Desktop.
Save denisemauldin/0de3bedc47e00a84e635be498a584516 to your computer and use it in GitHub Desktop.
Draw a path when click on the red point
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
var svgSelection = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 500)
var pointOne = d3.select("svg")
.append("circle")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 10);
var pointTwo = d3.select("svg")
.append("circle")
.attr("cx", 250)
.attr("cy", 250)
.attr("r", 10)
.style("fill", "red");
var simplePath = svgSelection.append("path")
.attr("d", "M50 50 L 250 250")
.attr("stroke", "steelblue")
.attr("stroke-width", "0")
.attr("fill", "none");
var pathTotalLength = simplePath.node().getTotalLength();
pointTwo.on("click", function() {
simplePath
.attr("stroke-dasharray", pathTotalLength + " " + pathTotalLength)
.attr("stroke-dashoffset", pathTotalLength)
.transition()
.duration(2000)
.attr("stroke-dashoffset", 0)
.attr("stroke-width", "2");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment