I've been searching around for a way to use D3 and a hand drawn pencil like capability. Is this something that is doable? I've looked around the different examples on mbostock site, but couldn't find anything that allowed you to manually draw within the zoomable area.
I like @coolblue's solution but here's an alternative. It uses a path element to really feel like drawing:
<!DOCTYPE html>
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<script>
var svg = d3.select('body')
.append('svg')
.attr('width', 1000)
.attr('height', 1000);
var color = d3.scale.category20();
var line = d3.svg.line()
.interpolate("basis");
var drawObj = {
isDown: false,
dataPoints: [],
currentPath: null,
color: 0
}
svg.on("mousedown", function(){
drawObj.isDown = true;
});
svg.on("mousemove", function(){
if (drawObj.isDown){
drawObj.dataPoints.push(
[d3.event.x, d3.event.y]
);
if (!drawObj.currentPath){
drawObj.currentPath = svg.append("path")
.attr("class","currentPath")
.style("stroke-width", 1)
.style("stroke",color(drawObj.color))
.style("fill", "none");
}
drawObj.currentPath
.datum(drawObj.dataPoints)
.attr("d", line);
}
});
svg.on("mouseup", function(){
drawObj.isDown = false;
drawObj.currentPath.attr("class","oldPath");
drawObj.dataPoints = [];
drawObj.currentPath = null;
if (++drawObj.color > 19) {
drawObj.color = 0;
}
})
</script>
</body>
</html>
Plunker here.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments