Last active
December 27, 2015 21:49
-
-
Save jonypawks/7394271 to your computer and use it in GitHub Desktop.
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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
</style> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script type="text/coffeescript" src="script.coffee"></script> | |
<script src="http://coffeescript.org/extras/coffee-script.js"></script> | |
</body> | |
</html> |
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
class LCG | |
constructor: (@seed) -> | |
@modulus = 4294967295 | |
@multiplier = 1664525 | |
@increment = 1013904223 | |
unless @seed? && 0 <= seed < @modulus | |
@seed = (new Date().valueOf() * new Date().getMilliseconds()) % @modulus | |
randn: -> | |
@seed = (@multiplier * @seed + @increment) % @modulus | |
randf: -> | |
@randn() / @modulus | |
rand: (n) -> | |
Math.floor(@randf() * n) | |
rand2: (min, max) -> | |
min + @rand(max - min) | |
genData = -> | |
data = [] | |
for j in [0...numRows] | |
for i in [0...numColumns] | |
data.push | |
key: "#{i},#{j}" | |
x: i | |
y: j | |
value: random.rand(255) | |
data | |
random = new LCG | |
boxSize = 32 | |
width = 960 | |
height = 480 | |
numColumns = width / boxSize | |
numRows = height / boxSize | |
initialData = genData() | |
svg = d3.select('body').append('svg') | |
.attr('width', 960) | |
.attr('height', 480) | |
update = -> | |
rects = svg.selectAll('rect') | |
.data(genData(), (d) -> d.key) | |
# tag the existing elements to update | |
rects.attr('class', 'update') | |
.transition() | |
.duration(750) | |
.style('opacity', (d) -> d.value / 255) | |
# create the new elements | |
rects.enter().append('rect') | |
.attr('class', 'enter') | |
.attr('x', (d) -> d.x * boxSize) | |
.attr('y', (d) -> d.y * boxSize) | |
.attr('width', boxSize) | |
.attr('height', boxSize) | |
.style('opacity', 1e-6) | |
.transition() | |
.duration(750) | |
.style('opacity', (d) -> d.value / 255) | |
rects.exit() | |
.attr('class', 'exit') | |
.remove() | |
update() | |
# Run every 1.5 secodns for 15 seconds | |
_interval = setInterval update, 1500 | |
setTimeout (-> clearInterval _interval), 15000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment