voronoi test

This commit is contained in:
javi 2015-07-31 18:12:08 +02:00
parent 973aa487dc
commit d2f6137eae
2 changed files with 24 additions and 34 deletions

View File

@ -225,13 +225,15 @@ L.TorqueLayer = L.CanvasLayer.extend({
}
pos = this.getTilePos(tile.coord);
ctx.setTransform(1, 0, 0, 1, pos.x, pos.y);
//ctx.setTransform(1, 0, 0, 1, pos.x, pos.y);
if (tile._tileCache) {
// when the tile has a cached image just render it and avoid to render
// all the points
this.renderer._ctx.drawImage(tile._tileCache, 0, 0);
} else {
// hack
this.renderer._realPos = pos;
this.renderer.renderTile(tile, this.key);
}
}

View File

@ -57,6 +57,10 @@ var Filters = require('./torque_filters');
this.TILE_SIZE = 256;
this._style = null;
this._gradients = {};
this._vertices = []
this._voronoi = d3.geom.voronoi()
this._forcePoints = false;
}
@ -64,7 +68,10 @@ var Filters = require('./torque_filters');
torque.extend(PointRenderer.prototype, torque.Event, {
clearCanvas: function() {
var canvas = this._canvas;
this._voronoi.clipExtent([[0, 0], [canvas.width, canvas.height]]);
this._vertices = []
var color = this._Map['-torque-clear-color']
// shortcut for the default value
if (color === "rgba(255, 255, 255, 0)" || !color) {
@ -269,7 +276,8 @@ var Filters = require('./torque_filters');
if (sp) {
var x = tile.x[posIdx]- (sp.width >> 1) + anchor;
var y = tileMax - tile.y[posIdx] + anchor; // flip mercator
ctx.drawImage(sp, x, y - (sp.height >> 1));
this._vertices.push([this._realPos.x + x, this._realPos.y + y]);
//ctx.drawImage(sp, x, y - (sp.height >> 1));
}
}
}
@ -403,41 +411,21 @@ var Filters = require('./torque_filters');
},
applyFilters: function(){
if(this._style){
if(this._style['image-filters']){
function gradientKey(imf){
var hash = ""
for(var i = 0; i < imf.args.length; i++){
var rgb = imf.args[i].rgb;
hash += rgb[0] + ":" + rgb[1] + ":" + rgb[2];
if (this._style) {
this._ctx.beginPath();
this._ctx.strokeStyle = '#000';
var polygons = this._voronoi(this._vertices);
// needs to be iterated by key because it's not an array, at least it does not start at 0
for (var p in polygons) {
var poly = polygons[p];
if (poly[0]) {
this._ctx.moveTo(poly[0][0], poly[0][1]);
for (var v = 1; v < poly.length; ++v) {
this._ctx.lineTo(poly[v][0], poly[v][1]);
}
return hash;
}
var gradient = this._gradients[gradientKey(this._style['image-filters'])];
if(!gradient){
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
gradient = {};
var colorize = this._style['image-filters'].args;
var increment = 1/colorize.length;
for (var i = 0; i < colorize.length; i++){
var key = increment * i + increment;
var rgb = colorize[i].rgb;
var formattedColor = rgbToHex(rgb[0], rgb[1], rgb[2]);
gradient[key] = formattedColor;
}
this._gradients[gradientKey(this._style['image-filters'])] = gradient;
}
this._filters.gradient(gradient);
this._filters.draw();
}
this._ctx.stroke();
}
}
});