d3.js - D3 translation relative to rotation -
does d3 have built in way translate object location relative rotation?
for example, have rectangle that's 50x50, located @ coordinates (100, 100). rectangle rotated 45 degrees. want move 100 units "forward".
i can trig hand figure out, in fiddle: http://jsfiddle.net/qh9x5/6530/
function toradians (angle) { return angle * (math.pi / 180); } var svg = d3.select("body").append("svg") .attr("width", 500) .attr("height", 500) .append("g"); svg.append('rect').attr('width', 50).attr('height', 50).attr('fill', 'blue') .attr('transform', "translate(100, 100) rotate(45)"); var h = 100; var angle = 45; var o = math.sin(toradians(angle)) * h; var = math.cos(toradians(angle)) * h console.log(o) svg.append('rect').attr('width', 50).attr('height', 50).attr('fill', 'red') .attr('transform', "translate(100, 100) rotate(45)") .transition().duration(1000) .attr('transform', "translate(" + (h + o) + ", " + (h + a) +") rotate(45)");
however seems common use case. before start writing libraries handle calculations i'd need, d3 support through native function, or through popular , maintained addon?
no, d3 doesn't have explicitly this. however, can nesting multiple elements, no need trigonometry:
var g = svg.append("g").attr("transform", "translate(100,100)"); g.append('rect').attr('width', 50).attr('height', 50).attr('fill', 'red') .attr('transform', "rotate(45)"); g.transition().duration(1000) .attr('transform', "translate(200,200)");
complete demo here.
Comments
Post a Comment