svg - use d3.js to animate a gauge needle -
i trying use d3.js animate gauge needle, end weird animation. have done search internet, couldn't figure out solution can use fix problem.
function createneedle(sampleangle){ topx = centerx - math.cos(sampleangle) * trilength topy = centery - math.sin(sampleangle) * trilength leftx = centerx - 10 * math.cos(sampleangle - math.pi / 2) lefty = centery - 10 * math.sin(sampleangle - math.pi / 2) rightx = centerx - 10 * math.cos(sampleangle + math.pi / 2) righty = centery - 10 * math.sin(sampleangle + math.pi / 2) return " m " + leftx + " " + lefty + " l " + topx + " " + topy + " l " + rightx + " " + righty; } //animate needle d3.select('.moveneedle') .attr('d', createneedle(sampleangle1)) .transition() .duration(2000) .attr('d', createneedle(sampleangle2));
you can make life easier if apply transform="rotate()"
instead of redrawing path.
nonetheless, need supply custom tween function, standard d3.interpolatetransformsvg acts in unexpected ways.
var topx = centerx - trilength, topy = centery, leftx = centerx, lefty = centery + 10, rightx = centerx, righty = centery - 10; function rotateneedle(sampleangle){ return "rotate(" + sampleangle + "," + centerx + "," + centery + ")"; } d3.select('.moveneedle') // draw once .attr('d', "m" + leftx + " " + lefty + " " + topx + " " + topy + " " + rightx + " " + righty) // supply angles in degrees! .attr('transform', rotateneedle(sampleangle1)) .transition() .duration(2000) .attrtween('transform', function () { var = d3.interpolate(sampleangle1, sampleangle2) return function (t) { return rotateneedle(i(t)); }; });
Comments
Post a Comment