Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created April 17, 2012 20:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/2408813 to your computer and use it in GitHub Desktop.
Save tmcw/2408813 to your computer and use it in GitHub Desktop.
var Canvas = require('canvas'),
fs = require('fs');
var c = new Canvas(1000, 400);
var ctx = c.getContext('2d');
var w = 1000;
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, 1000, 400);
ctx.fillStyle = '#000';
var f = fs.readFileSync('keystrokes.log', 'utf8');
var lines = f.split('\n').slice(1);
var minutely = {};
for (var i = 0; i < lines.length; i++) {
var pts = lines[i].split(',');
var d = new Date(pts[0] * 1000);
var n = (d.getHours() * 60) + d.getMinutes();
if (!minutely[n]) minutely[n] = [];
minutely[n].push(+pts[1]);
}
var minutes = [];
for (var x in minutely) {
minutes.push([+x, minutely[x]]);
}
minutes = minutes.sort(function(a, b) { return a[0] - b[0] });
for (var i = 0; i < 24; i++) {
ctx.fillText(i + ':00', 10 + (i / 24) * w, 370);
}
ctx.fillText('keystroke activity over the course of four months', 10, 385);
minutes.map(function(m) {
m[1].map(function(j) {
ctx.fillRect(~~((m[0] / 1440) * w), ~~(350 - j / 4), 1, 1);
});
});
fs.writeFileSync('daygram.png', c.toBuffer());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment