Skip to content

Instantly share code, notes, and snippets.

@ninestar09
Created February 26, 2016 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninestar09/39e32b697d31e979040d to your computer and use it in GitHub Desktop.
Save ninestar09/39e32b697d31e979040d to your computer and use it in GitHub Desktop.
Line Drawing
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: #000;
stroke-width: 3px;
stroke-linejoin: round;
stroke-linecap: round;
}
</style>
<svg width="960" height="500">
<rect style="fill:#fff;" width="100%" height="100%"></rect>
</svg>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var activeLine;
var renderPath = d3.svg.line()
.x(function(d) { return d[0]; })
.y(function(d) { return d[1]; })
.interpolate("basis");
var svg = d3.select("svg")
.call(d3.behavior.drag()
.on("dragstart", dragstarted)
.on("drag", dragged)
.on("dragend", dragended));
function dragstarted() {
activeLine = svg.append("path").datum([]).attr("class", "line");
}
function dragged() {
activeLine.datum().push(d3.mouse(this));
activeLine.attr("d", renderPath);
}
function dragended() {
activeLine = null;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment