Created
March 15, 2012 00:20
-
-
Save uberscientist/2040654 to your computer and use it in GitHub Desktop.
Gen. image seq. from dotty Redis data
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
var redis = require('redis'), | |
fs = require('fs'), | |
Canvas = require('canvas'); | |
var db = redis.createClient(), | |
canvas = new Canvas(500, 300), | |
ctx = canvas.getContext('2d'); | |
function pad(number, length) { | |
var str = '' + number; | |
while (str.length < length) { | |
str = '0' + str; | |
} | |
return str; | |
} | |
function draw_dot(dot, i){ | |
//Pad frame # with 6 zeros | |
frame = pad(i, 6); | |
//Draw da dots | |
var obj = JSON.parse(dot); | |
var x = obj.x, | |
y = obj.y, | |
color = obj.color; | |
ctx.beginPath(); | |
ctx.arc(x, y, 8, 0, Math.PI*2, true); | |
ctx.closePath(); | |
ctx.fillStyle = '#'+color; | |
ctx.fill(); | |
//Create PNG stream for current frame context | |
var out = fs.createWriteStream(__dirname + '/frames1/' + frame + '.png'), | |
stream = canvas.createPNGStream(); | |
//Pipe the PNG stream to the write file stream | |
stream.pipe(out); | |
//listen for close event from writeable stream | |
out.on('close', function(){ | |
//hardcoded callback... | |
main(i); | |
console.log(i); | |
}); | |
} | |
function main(i){ | |
if(i<length){ | |
i++; | |
//Look at redis for next range of dots | |
db.lindex('dotty:dots',i, function(err, dot){ | |
if(err) throw err; | |
draw_dot(dot, i); | |
}); | |
} | |
} | |
db.llen('dotty:dots', function(err, len){ | |
length = len; | |
main(-1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment