Skip to content

Instantly share code, notes, and snippets.

@maelp
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maelp/11516815 to your computer and use it in GitHub Desktop.
Save maelp/11516815 to your computer and use it in GitHub Desktop.
Fast prototyping tricks - Transition along invisible path
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
width: 960px;
padding-top: 40px;
margin: auto;
position: relative;
}
svg {
width: 100%;
max-height: 400px;
}
</style>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type='text/javascript'>
function animateGrowingCircle(fromRadius, toRadius) {
var animateFunction = function () {
d3.select(this)
.attr('r', fromRadius)
.transition()
.duration(1000)
.attr('r', toRadius)
.transition()
.duration(1000)
.attr('r', fromRadius)
.each('end', function () {
d3.select(this).each(animateFunction);
});
};
return animateFunction;
}
function translateAlongPath(path, offset) {
var l = path.getTotalLength();
var tx = offset[0],
ty = offset[1];
return function() {
return function(t) {
var p = path.getPointAtLength(t * l);
d3.select(this)
.attr('cx', p.x + tx)
.attr('cy', p.y + ty);
};
};
}
function reload() {
d3.select('svg').remove();
d3.xml('iPhone.svg', 'image/svg+xml', function (error, data) {
d3.select('body').node().appendChild(data.documentElement);
var svg = d3.select('svg');
var mainScreen = svg.select('#MainScreen');
var animationElements = mainScreen.select('#AnimationElements');
animationElements.style('display', 'none');
var replayButton = mainScreen.select('#ReplayButton')
.style('display', 'none')
.style('opacity', 0.0)
.style('cursor', 'pointer')
.on('click', reload);
var circle = mainScreen.select('#Circle');
var offset = d3.transform(animationElements.attr('transform')).translate;
var motionPath = mainScreen.select('#MotionPath');
circle.transition()
.duration(2500)
.ease('bounce')
.tween('translateAlongPath', translateAlongPath(motionPath.node(), offset))
.each('end', function () {
var circleAnimationMaxRadius = animationElements.select('#CircleAnimationMaxRadius');
var r = +circle.attr('r');
var r2 = +circleAnimationMaxRadius.attr('r');
replayButton
.style('display', 'block')
.transition()
.duration(1000)
.style('opacity', 1.0);
d3.select(this)
.each(animateGrowingCircle(r, r2));
});
});
}
window.addEventListener('load', function () {
reload();
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment