Last active
January 5, 2017 23:49
-
-
Save paulgb/4790aa01d9295e6112e21be82bfd35f7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<head> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
</head> | |
<body> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
width = 960 | |
height = 505 | |
data = [{ | |
left: 0, | |
top: 0, | |
size: 1024, | |
color: d3.hsl(360 * Math.random(), 0.5, 0.5) | |
}] | |
svg = d3.select('body').append('svg').style('width', width).style('height', height) | |
function perturbColor(color) { | |
return d3.hsl( | |
color.h + (Math.random() - 0.5) * 80, | |
color.s + (Math.random() - 0.5) * 0.1, | |
color.l + (Math.random() - 0.5) * 0.1 | |
) | |
} | |
function splitGroup(datum) { | |
if (datum.size < 16) { | |
return; | |
} | |
group = d3.select(this.parentNode) | |
newSize = datum.size / 2 | |
newGroups = [ | |
{top: datum.top, left: datum.left, size: newSize, color: perturbColor(datum.color)}, | |
{top: datum.top, left: datum.left + newSize, size: newSize, color: perturbColor(datum.color)}, | |
{top: datum.top + newSize, left: datum.left, size: newSize, color: perturbColor(datum.color)}, | |
{top: datum.top + newSize, left: datum.left + newSize, size: newSize, color: perturbColor(datum.color)}, | |
] | |
newGroups = newGroups.filter((d) => d.left < width && d.top < height) | |
group.selectAll('rect.new').data(newGroups).call(fillGroup) | |
} | |
function fillGroup(group) { | |
group.enter().append('rect') | |
.attr('x', (d) => d.left) | |
.attr('y', (d) => d.top) | |
.attr('height', (d) => d.size) | |
.attr('width', (d) => d.size) | |
.attr('fill', (d) => d.color.toString()) | |
.attr('opacity', 0).transition().attr('opacity', 1).transition().on('start', splitGroup) | |
} | |
svg.selectAll('rect.new') | |
.data(data) | |
.call(fillGroup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment