svg-mapper
Version:
Tool to slice big, annotated SVG into WebMercator tiles
78 lines (77 loc) • 2.98 kB
JavaScript
// Generated by LiveScript 1.2.0
/*
Slices provides sourceCanvas to tiles dimensioned tileWidth x tileHeight
Emits Tile event with tile number (web mercator - zoomlevel/x/y) and Canvas
containing the tile imagery
*/
(function(){
var Canvas, EventEmitter, TileMaker, log2;
Canvas = require('canvas');
EventEmitter = require('events').EventEmitter;
module.exports = TileMaker = (function(superclass){
var prototype = extend$((import$(TileMaker, superclass).displayName = 'TileMaker', TileMaker), superclass).prototype, constructor = TileMaker;
function TileMaker(sourceCanvas, arg$, zoomLevel){
this.sourceCanvas = sourceCanvas;
this.tileWidth = arg$[0], this.tileHeight = arg$[1];
this.zoomLevel = zoomLevel;
}
prototype.makeTiles = function(){
var sourceContext, cols, rows, tileCount, z, this$ = this;
sourceContext = this.sourceCanvas.getContext('2d');
cols = Math.ceil(this.sourceCanvas.width / this.tileWidth);
rows = Math.ceil(this.sourceCanvas.height / this.tileHeight);
tileCount = cols * rows;
z = this.zoomLevel;
process.nextTick(function(){
var tile, tileCtx, i$, ref$, len$, x, lresult$, j$, ref1$, len1$, y, offX, offY, imageData, results$ = [];
tile = new Canvas(this$.tileWidth, this$.tileHeight);
tileCtx = tile.getContext('2d');
for (i$ = 0, len$ = (ref$ = (fn$())).length; i$ < len$; ++i$) {
x = ref$[i$];
lresult$ = [];
for (j$ = 0, len1$ = (ref1$ = (fn1$())).length; j$ < len1$; ++j$) {
y = ref1$[j$];
offX = x * this$.tileWidth;
offY = y * this$.tileHeight;
imageData = sourceContext.getImageData(offX, offY, this$.tileWidth, this$.tileHeight);
tileCtx.clearRect(0, 0, this$.tileWidth, this$.tileHeight);
tileCtx.putImageData(imageData, 0, 0);
lresult$.push(this$.emit('tile', z, x, y, tile));
}
results$.push(lresult$);
}
return results$;
function fn$(){
var i$, to$, results$ = [];
for (i$ = 0, to$ = cols; i$ < to$; ++i$) {
results$.push(i$);
}
return results$;
}
function fn1$(){
var i$, to$, results$ = [];
for (i$ = 0, to$ = rows; i$ < to$; ++i$) {
results$.push(i$);
}
return results$;
}
});
return tileCount;
};
return TileMaker;
}(EventEmitter));
log2 = function(val){
return Math.log(val / Math.LN2);
};
function extend$(sub, sup){
function fun(){} fun.prototype = (sub.superclass = sup).prototype;
(sub.prototype = new fun).constructor = sub;
if (typeof sup.extended == 'function') sup.extended(sub);
return sub;
}
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}
}).call(this);