d3.js - Pencil-like functionality -
i've been searching around way use d3 , hand drawn pencil capability. doable? i've looked around different examples on mbostock site, couldn't find allowed manually draw within zoomable area.
i @coolblue's solution here's alternative. uses path element feel 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.
Comments
Post a Comment