Built with blockbuilder.org
Created
May 14, 2016 20:27
-
-
Save anonymous/103e8a8dd7290ba9a6cad38bbd9ca694 to your computer and use it in GitHub Desktop.
fresh block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
svg { width:100%; height: 100% } | |
path { stroke: black; fill: none; } | |
</style> | |
</head> | |
<body> | |
<script> | |
// Feel free to change or delete any of the code you see! | |
var svg = d3.select("body").append("svg") | |
var points = [ | |
{ | |
'anchor': [0,0], | |
'control': [200,200] | |
}, | |
{ | |
'anchor': [-63,300], | |
'control': [200,140] | |
} | |
] | |
function translate(arr) { | |
return "translate(" + arr[0] + "," + arr[1] + ")"; | |
} | |
function svgPath(points) { | |
var path = 'M'; | |
points.forEach(function(d, i) { | |
if (i == 0) { | |
path += d.anchor[0] + ',' + d.anchor[1]; | |
path += ' C ' + d.control[0] + ',' + d.control[1]; | |
} else { | |
path += d.anchor[0] + ',' + d.anchor[1]; | |
path += ' ' + d.control[0] + ',' + d.control[1]; | |
} | |
}) | |
return path; | |
} | |
console.log(svgPath(points)); | |
var drag = d3.behavior.drag() | |
.on('drag', function(d, i) { | |
d3.select(this).attr('transform', translate([d3.event.x, d3.event.y])); | |
}) | |
var path = svg.append('path') | |
.attr('d', svgPath(points)) | |
var points = svg.selectAll('g.point') | |
.data(points) | |
.enter().append('g') | |
.attr('class', 'point'); | |
var anchors = points.append('circle') | |
.attr('r', 5) | |
.attr('transform', function(d) { | |
return translate(d.anchor) | |
}); | |
var controls = points.append('circle') | |
.attr('r', 5) | |
.attr('transform', function(d) { | |
return translate(d.control) | |
}).on('drag', function(d) { | |
}); | |
console.log("you are now rocking with d3", d3); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment