[ Launch: Tributary inlet ] 6133395 by tarvaina
-
-
Save tarvaina/6133395 to your computer and use it in GitHub Desktop.
Moving along path practice
This file contains hidden or 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
| {"description":"Moving along path practice","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/biW4n8M.png"} |
This file contains hidden or 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
| var data = [ | |
| {x: 120, y:40}, | |
| {x: 356, y:76}, | |
| {x: 26, y:120}, | |
| {x: 60, y:193}, | |
| {x: 26, y:318} | |
| ]; | |
| var line = d3.svg.line() | |
| .x(function(v) { return v.x; }) | |
| .y(function(v) { return v.y; }); | |
| var svg = d3.select("svg"); | |
| var group = svg.append("g") | |
| .attr({ | |
| transform: "translate(" + [50, 50] + ")" | |
| }); | |
| var path = group.selectAll("path").data([data]).enter() | |
| .append("path") | |
| .attr({ | |
| d: line, | |
| fill: "none", | |
| stroke: "#000000" | |
| }); | |
| var pathLength = path.node().getTotalLength(); | |
| path.attr({ | |
| "stroke-dasharray": pathLength + " " + pathLength, | |
| "stroke-dashoffset": pathLength | |
| }); | |
| var circlePosition = function(t) { | |
| return path.node().getPointAtLength(pathLength * t); | |
| }; | |
| var pos = circlePosition(0); | |
| var circle = group.selectAll("circle").data([data]).enter() | |
| .append("circle") | |
| .attr({ | |
| r: 20, | |
| cx: pos.x, | |
| cy: pos.y | |
| }); | |
| svg.on("click", function() { | |
| var dur = 2000; | |
| var ease = "bounce"; | |
| path | |
| .transition() | |
| .duration(dur) | |
| .ease(ease) | |
| .attrTween( | |
| "stroke-dashoffset", | |
| function(d, i, a) { | |
| return function(t) { | |
| return pathLength * (1 - t); | |
| }; | |
| }); | |
| circle | |
| .transition() | |
| .duration(dur) | |
| .ease(ease) | |
| .attrTween( | |
| "cx", | |
| function(d, i, a) { | |
| return function(t) { return circlePosition(t).x }; | |
| }) | |
| .attrTween( | |
| "cy", | |
| function(d, i, a) { | |
| return function(t) { return circlePosition(t).y }; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment