Skip to content

Instantly share code, notes, and snippets.

@pvernier
Last active March 11, 2018 19:40
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 pvernier/bfc4e0d06a5751a3495f46b92c77876f to your computer and use it in GitHub Desktop.
Save pvernier/bfc4e0d06a5751a3495f46b92c77876f to your computer and use it in GitHub Desktop.
Pong - infinite transition with easing
license: gpl-3.0
height: 200

A circle moving in a box, pong style. Same as this block but an easing is added to the transition (line 30) making it smoother.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
svg {
background-color: #4d4d4d;
width: 200px;
height: 200px;
}
circle {
fill: #b2182b;
}
</style>
</head>
<body>
<svg></svg>
</body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-timer.v1.min.js"></script>
<script type="text/javascript">
var circle = d3.select("svg")
.append("circle")
.attr("cx", 190)
.attr("cy", 100)
.attr("r", 10)
d3.interval(function(elapsed) {
circle.transition()
.ease(d3.easeLinear)
.attr("cx", 95)
.attr("cy", 190)
.transition()
.attr("cx", 10)
.attr("cy", 100)
.transition()
.attr("cx", 95)
.attr("cy", 10)
.transition()
.attr("cx", 190)
.attr("cy", 100)
}, 1000);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment