Skip to content

Instantly share code, notes, and snippets.

Forked from mbostock/.block
Last active January 26, 2022 14:32
Show Gist options
  • Save mbostock/1642989 to your computer and use it in GitHub Desktop.
Save mbostock/1642989 to your computer and use it in GitHub Desktop.
Spline Transition
license: gpl-3.0

This example is the third of three in the Path Transitions tutorial; see the previous example for context.

When transitioning a transform on a path using basis interpolation, you must clip the path by two additional control points so that the change in tangent is not visible while the path slides left.

<!DOCTYPE html>
<meta charset="utf-8">
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
<svg width="960" height="500"></svg>
<script src="//"></script>
var n = 40,
random = d3.randomNormal(0, .2),
data = d3.range(n).map(random);
var svg ="svg"),
margin = {top: 20, right: 20, bottom: 20, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + + ")");
var x = d3.scaleLinear()
.domain([1, n - 2])
.range([0, width]);
var y = d3.scaleLinear()
.domain([-1, 1])
.range([height, 0]);
var line = d3.line()
.x(function(d, i) { return x(i); })
.y(function(d, i) { return y(d); });
.attr("id", "clip")
.attr("width", width)
.attr("height", height);
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y(0) + ")")
.attr("class", "axis axis--y")
.attr("clip-path", "url(#clip)")
.attr("class", "line")
.on("start", tick);
function tick() {
// Push a new data point onto the back.
// Redraw the line.
.attr("d", line)
.attr("transform", null);
// Slide it to the left.
.attr("transform", "translate(" + x(0) + ",0)")
.on("start", tick);
// Pop the old data point off the front.
Copy link

praveenuics commented Jun 29, 2017

Is this code available in v3 at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment