Skip to content

Instantly share code, notes, and snippets.

@Kcnarf
Last active December 3, 2018 08:28
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 Kcnarf/79062af0fed9ab05ff56e3e194509ec0 to your computer and use it in GitHub Desktop.
Save Kcnarf/79062af0fed9ab05ff56e3e194509ec0 to your computer and use it in GitHub Desktop.
Rotating Distance-limited Voronoi - d3v4 - canvas
license: gpl-3.0
<meta charset="utf-8">
<style>
body {
background: #000;
}
path {
fill: #fff;
stroke: #000;
stroke-width: 1.5px;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://raw.githack.com/Kcnarf/d3-distanceLimitedVoronoi/master/distance-limited-voronoi.js"></script>
<script>
var width = 960,
height = 500;
var start = Date.now(),
points = [];
var maxDistance = 400;
circle(0, 0, 120, 96, -.001);
circle(0, 0, 30, 10, .03);
circle(0, 0, 60, 3, -.05);
circle(0, 0, 15, 4, -.02);
circle(0, 0, 0, 1, -.02);
circle(240, -120, 80, 4, -.02);
circle(240, -120, 0, 1, -.02);
circle(280, +120, 40, 8, .02);
circle(280, +120, 20, 8, -.02);
circle(280, +120, 0, 1, .02);
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var context = canvas.node().getContext("2d");
context.translate(width/2, height/2);
var limitedVoronoi = d3.distanceLimitedVoronoi()
limitedVoronoi.extent([[-width / 2, -height / 2],[+width / 2, +height / 2]]).limit(maxDistance).context(context);
d3.timer(function(elapsed) {
//update limit
limitedVoronoi.limit(2+maxDistance*Math.pow(Math.cos((elapsed++)/5000), 4));
//delete previous rendering
context.fillStyle = 'black';
context.fillRect(-width/2, -height/2, width, height);
//renders new distance-limited voronoi
context.strokeStyle = 'black';
context.fillStyle = 'white';
context.beginPath();
limitedVoronoi(points);
context.fill();
context.stroke();
});
function circle(cx, cy, r, n, δθ) {
d3.range(1e-6, 2 * Math.PI, 2 * Math.PI / n).map(function(θ, i) {
var point = [cx + Math.cos(θ) * r, cy + Math.sin(θ) * r];
d3.timer(function(elapsed) {
var angle = θ + δθ * elapsed / 60;
point[0] = cx + Math.cos(angle) * r;
point[1] = cy + Math.sin(angle) * r;
});
points.push(point);
return point;
});
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment