Skip to content

Instantly share code, notes, and snippets.

@jwasilgeo
Last active September 14, 2017 21:52
Show Gist options
  • Save jwasilgeo/f6d9f09cfe483fbbbdc6727544962499 to your computer and use it in GitHub Desktop.
Save jwasilgeo/f6d9f09cfe483fbbbdc6727544962499 to your computer and use it in GitHub Desktop.
When the Contours Go Marching In
license: gpl-3.0
border: no

Marching contours!

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
</head>
<body>
<svg width="960" height="500" stroke-linejoin="round" stroke-linecap="round">
</svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-contour.v1.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script>
var svg = d3.select('svg'),
svgWidth = +svg.attr('width'),
randomNumberGenerator = d3.randomNormal(),
n = 96, // data width
m = 50, // data height
values = [];
// fill in the data array with random numbers within a normal distribution
values.length = n * m;
for (var i = 0; i < values.length; ++i) {
// positive values only,
// just for the sake of making contour thresholds easier to experiment with
values[i] = Math.abs(randomNumberGenerator());
}
svg.selectAll('path')
.data(d3.contours()
.size([n, m])
.thresholds(d3.range(0.5, 3.5, 0.5))(values) // this will create 6 steps
)
.enter().append('path')
.attr('d', d3.geoPath(d3.geoIdentity().scale(svgWidth / n)))
.style('fill', 'none')
.style('stroke', function(d, idx) {
return d3.schemeOranges[9][idx]
})
.style('stroke-width', 2.5)
.style('stroke-dasharray', '0, 1000');
// march the contours into existence
svg.selectAll('path')
.transition()
.duration(7000)
.style('stroke-dasharray', '1000, 0');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment