Last active
January 7, 2016 00:58
-
-
Save tobiasburri/2eba02dcab9d557713b6 to your computer and use it in GitHub Desktop.
Chromatic Square Loop
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"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> | |
<script src="http://underscorejs.org/underscore.js"></script> | |
<style> | |
body { | |
background-color:white; | |
overflow:hidden; | |
} | |
#rootElement { | |
width:600px; | |
height:600px; | |
margin-top: 10%; | |
background-color:white; | |
text-align: center; | |
z-index:-1 | |
} | |
.wrapper { | |
width:600px; | |
position:relative; | |
margin:auto; | |
} | |
</style> | |
<body> | |
<div class="wrapper"> | |
<div id='rootElement'> </div> | |
</div> | |
<script type="text/javascript"> | |
var | |
width = 400, | |
height = 400, | |
numberOfCells = 1, | |
rootElem = d3.select('#rootElement'), | |
colors = ['black','#9a9a9a'] | |
svg = rootElem.append('svg') | |
.attr("width", width) | |
.attr("height", height); | |
var determineSizeofCells = function(nCells) { | |
var sizeOfCell = width / nCells; | |
return sizeOfCell; | |
}; | |
var createRangeIndeces = function(nCells) { | |
var container = [] | |
_.range(nCells).forEach(function (i) { | |
return _.range(nCells).forEach(function (j) { | |
container.push({'x':j,'y':i}); | |
}); | |
}); | |
return container; | |
}; | |
var endAll = function (transition, callback) { | |
if (transition.size() === 0) { callback() } | |
var n = 0; | |
transition | |
.each(function() { ++n; }) | |
.each("end", function() { if (!--n) callback.apply(this, arguments); }); | |
} | |
var createColorScale = function(domain) { | |
var colorScale = d3.scale.linear() | |
.domain([ | |
0, | |
1/6*domain, | |
2/6*domain, | |
3/6*domain, | |
4/6*domain, | |
5/6*domain]) | |
.range(["red", "orange", "yellow", "green", "blue", "purple"]); | |
return colorScale; | |
}; | |
var render = function(nCells) { | |
d3.selectAll('.square').remove(); | |
var size = determineSizeofCells(nCells); | |
var rangeIndex = createRangeIndeces(nCells); | |
var cScale = createColorScale(nCells * nCells) | |
svg.selectAll('placeholder') | |
.data(rangeIndex) | |
.enter() | |
.append('rect') | |
.style('opacity', 0) | |
.attr('class', 'square') | |
.attr('x', function(d,i) {return size * d.x}) | |
.attr('y', function(d,i) {return size * d.y}) | |
.style('fill', function(d,i) {return cScale(i)}) | |
.style('stroke-width', 1) | |
.style('stroke', 'white') | |
.transition(400).delay(function(d,i) {return i * 50 }) | |
.attr('width', size - 2) | |
.attr('height', size - 2) | |
.style('opacity', 1) | |
.call(endAll, function() { render(nCells + 1) }); | |
} | |
render(numberOfCells); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment