Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created August 1, 2013 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/6131107 to your computer and use it in GitHub Desktop.
Save tmcw/6131107 to your computer and use it in GitHub Desktop.
var Canvas = require('canvas'),
glob = require('glob'),
fs = require('fs'),
rainbowDash = require('rainbow-dash'),
leftpad = require('leftpad'),
width = 1000,
height = 1000,
radius = 100,
c = new Canvas(width, height),
ctx = c.getContext('2d');
console.log('parsing');
// var pos = glob.sync('data/v0/bus/positions/*.geojson').slice(0, 1000).map(function(p) {
var pos = glob.sync('data/v0/bus/positions/*.geojson').map(function(p) {
var collection = JSON.parse(fs.readFileSync(p));
return [+p.match(/(\d+).geojson/)[1], collection.features.length];
});
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, width, height);
console.log('done parsing');
ctx.fillStyle = '#333';
ctx.globalAlpha = 0.2;
ctx.lineTo(width/2, height/2);
pos.forEach(function(p) {
if (p[0] - last > 1) {
// console.log('break');
ctx.lineTo(width/2, height/2);
ctx.fill();
ctx.beginPath();
} else {
// console.log('nonbreak');
}
ctx.lineTo.apply(ctx, xy(p));
last = p[0];
});
ctx.fill();
ctx.globalAlpha = 1;
var last = -Infinity;
pos.forEach(function(p) {
if (p[0] - last > 1) {
// console.log('break');
ctx.stroke();
ctx.beginPath();
} else {
// console.log('nonbreak');
}
ctx.lineTo.apply(ctx, xy(p));
last = p[0];
});
ctx.stroke();
ctx.beginPath();
ctx.font = '15px Monaco';
ctx.strokeStyle = '#ccc';
for (var i = 0; i < 24; i++) {
var p = hr(i - 6, 410);
ctx.fillText(i + 1, p[0], p[1]);
ctx.beginPath();
ctx.moveTo(width/2, height/2);
var p2 = hr(i - 6, 1010);
ctx.lineTo(p2[0], p2[1]);
ctx.stroke();
}
ctx.beginPath();
function hr(i, radius) {
var angle = (i / 24) * Math.PI * 2;
return [
(width / 2) + (Math.cos(angle) * radius),
(height / 2) + (Math.sin(angle) * radius)
];
}
ctx.strokeStyle = '#000';
pos.forEach(function(p) {
if (p[0] - last > 1) {
// console.log('break');
ctx.stroke();
ctx.beginPath();
} else {
// console.log('nonbreak');
}
ctx.lineTo.apply(ctx, xy(p));
last = p[0];
});
function xy(p) {
var d = new Date(p[0] * 1000 * 60),
hrs = d.getHours() * 60,
mins = d.getMinutes(),
angle = ((hrs + mins) / (60 * 24)) * Math.PI * 2;
return [
(width / 2) + (Math.cos(angle) * radius * (p[1] / 100)),
(height / 2) + (Math.sin(angle) * radius * (p[1] / 100))
];
}
fs.writeFileSync('supply.png', c.toBuffer());
// fs.writeFileSync('supply.json', JSON.stringify(doub));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment