This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.js
52 lines (41 loc) · 1.47 KB
/
tiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var fs = require('fs');
var redis = require('redis');
var Canvas = require('canvas');
var conerr = 0;
var Tiles = function(req, res, cb) {
var produceTile = function(count, high, cb) {
var acanvas = new Canvas(256, 256);
var ctx = acanvas.getContext('2d');
var ptg = 100 - (parseInt(count) / parseInt(high)) * 100;
var color = 'rgba(' + Math.floor(0.5+(255*ptg)/100) + ', ' + Math.floor(0.5+(255*(100-ptg))/100) + ', 0, 0.55)';
// This is for debugging only.
// console.log(count + ' / ' + high + ' = ' + ptg/100);
ctx.fillStyle = color;
ctx.fillRect(0, 0, 256, 256);
ctx.font = '36px Impact';
var te = ctx.measureText(count);
ctx.fillText(count, 128-te.width/2, 128);
acanvas.toBuffer(function(err, buf) { cb(err, buf); });
};
var client = redis.createClient();
client.auth();
client.on('error', function(err) {
conerr++;
console.error(err + ' (' + conerr + ').');
});
var tile = req.param('tile', '');
var cz = req.param('cz', '');
client.get(tile, function(err, reply) {
var count = 0;
if (reply != null) count = reply;
// console.log(tile + ': ' + reply);
res.writeHead(200, { 'Content-Type': 'image/png', 'X-Count': count, 'max-age': 60 });
client.get('zoomlevel' + cz, function(err, high) {
if (high == null) high = 1; // prevent div by 0.
produceTile(count, high, function(err, result) { client.end(); cb(result); });
});
});
};
exports.create = function(req, res, cb) {
return new Tiles(req, res, cb);
};