Skip to content

Instantly share code, notes, and snippets.

@toja
Last active June 2, 2016 16:09
Show Gist options
  • Save toja/ac978bbcb132cce2146089ba7c85b901 to your computer and use it in GitHub Desktop.
Save toja/ac978bbcb132cce2146089ba7c85b901 to your computer and use it in GitHub Desktop.
4096 Random Colors (HSL)
height: 480
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 480;
var sample = colorSampler(4096);
var x = d3.scale.linear()
.domain([0, 360])
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var dots = svg.append("g");
var num = svg.append("text")
.attr("x", 5)
.attr("y", 15);
var counter = 0;
d3.timer(function() {
var hsl = sample();
if (!hsl) return true;
dots.append("circle")
.attr("cx", x(hsl.h))
.attr("cy", y(hsl.l))
.attr("r", 0)
.style("fill", hsl.toString())
.transition()
.attr("r", 4);
num.text(counter);
});
function colorSampler(numSamplesMax) {
return function() {
if (++counter > numSamplesMax) return;
return d3.hsl(
Math.random() * 360,
Math.random(),
Math.random()
);
};
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment