10,000 uniform random samples of Van Gogh’s The Starry Night. Compare to poisson-disc samples.
Last active
February 9, 2016 01:54
-
-
Save mbostock/0c44b0dc4e79af5a0a6e to your computer and use it in GitHub Desktop.
Uniform Random II
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var sample = uniformRandomSampler(width, height, 10000), | |
samples = [], | |
s; | |
while (s = sample()) samples.push(s); | |
var voronoi = d3.geom.voronoi() | |
.clipExtent([[0, 0], [width, height]]); | |
var canvas = d3.select("body").append("canvas") | |
.attr("width", width) | |
.attr("height", height); | |
var context = canvas.node().getContext("2d"); | |
var image = new Image; | |
image.src = "starry-night.jpg"; | |
image.onload = start; | |
function start() { | |
context.drawImage(image, 0, 0); | |
image = context.getImageData(0, 0, width, height); | |
voronoi(samples).forEach(function(cell) { | |
var x = Math.floor(cell.point[0]), | |
y = Math.floor(cell.point[1]), | |
i = (y * width + x) << 2; | |
context.fillStyle = d3.rgb(image.data[i + 0], image.data[i + 1], image.data[i + 2]) + ""; | |
context.beginPath(); | |
context.moveTo(cell[0][0], cell[0][1]); | |
for (var i = 1, n = cell.length; i < n; ++i) context.lineTo(cell[i][0], cell[i][1]); | |
context.closePath(); | |
context.fill(); | |
}); | |
} | |
function uniformRandomSampler(width, height, numSamplesMax) { | |
var numSamples = 0; | |
return function() { | |
if (++numSamples > numSamplesMax) return; | |
return [Math.random() * width, Math.random() * height]; | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment