javascript - Closing path for transition animation d3.js -
i've made line path creating shape of bottle. i'm trying figure out if can close path in order transition it. ideally have bottle animate across canvas, same way circle or rectangle would. possible? i'm still learning d3.
var width = 900, height = 800; var canvas = d3.select('body') .append('svg') .attr('width', width) .attr('height', height); var dataarray = [ {x:51,y:44}, {x:51,y:49}, {x:53,y:50}, {x:53,y:53}, {x:52,y:53}, {x:52,y:60}, {x:70,y:85}, {x:71,y:160}, {x:64,y:181}, {x:54,y:181}, {x:47,y:170}, {x:43,y:170}, {x:36,y:181}, {x:26,y:181}, {x:19,y:160}, {x:19,y:85}, {x:39,y:60}, {x:39,y:53}, {x:38,y:53}, {x:38,y:50}, {x:40,y:49}, {x:40,y:44}, {x:51,y:44} ]; var interpolate = d3.curvecardinal.tension(0.35); var line = d3.line() .x(function(d,i){ return d.x/1.05 }) .y(function(d,i){ return d.y }) .curve(interpolate); var group = canvas.append('g') .attr('transform','translate(0,0)'); var bottle = group.selectall('path') .data([dataarray]) .enter() .append('path') .attr('fill','#ed5545') .attr('stroke','#aa2731') .attr('stroke-width','2') .attr('id','bottleimage') .attr('d',line); bottle.transition() .attr('x',300);
a svg path has no x
attribute. simplest solution, therefore, applying transition group:
group.transition() .delay(500) .duration(2000) .attr('transform', 'translate(300,0)');
here demo:
var width = 400, height = 300; var canvas = d3.select('body') .append('svg') .attr('width', width) .attr('height', height); var dataarray = [{ x: 51, y: 44 }, { x: 51, y: 49 }, { x: 53, y: 50 }, { x: 53, y: 53 }, { x: 52, y: 53 }, { x: 52, y: 60 }, { x: 70, y: 85 }, { x: 71, y: 160 }, { x: 64, y: 181 }, { x: 54, y: 181 }, { x: 47, y: 170 }, { x: 43, y: 170 }, { x: 36, y: 181 }, { x: 26, y: 181 }, { x: 19, y: 160 }, { x: 19, y: 85 }, { x: 39, y: 60 }, { x: 39, y: 53 }, { x: 38, y: 53 }, { x: 38, y: 50 }, { x: 40, y: 49 }, { x: 40, y: 44 }, { x: 51, y: 44 }]; var interpolate = d3.curvecardinal.tension(0.35); var line = d3.line() .x(function(d, i) { return d.x / 1.05 }) .y(function(d, i) { return d.y }) .curve(interpolate); var group = canvas.append('g') .attr('transform', 'translate(0,0)'); var bottle = group.selectall('path') .data([dataarray]) .enter() .append('path') .attr('fill', '#ed5545') .attr('stroke', '#aa2731') .attr('stroke-width', '2') .attr('id', 'bottleimage') .attr('d', line); group.transition() .delay(500) .duration(2000) .attr('transform', 'translate(300,0)');
<script src="https://d3js.org/d3.v4.min.js"></script>
Comments
Post a Comment