Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make new HeatCanvas leaflet layer that uses all data for rendering, n…
…ot just the current bounds.
- Loading branch information
Showing
4 changed files
with
124 additions
and
12 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/shareabouts_client/static/js/Leaflet.ImageOverlay.HeatCanvas.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* L.ImageOverlay.Canvas is used to overlay HTML5 canvases over the map (to specific geographical bounds). | ||
*/ | ||
|
||
L.ImageOverlay.HeatCanvas = L.ImageOverlay.Canvas.extend({ | ||
options: { | ||
step: 1, | ||
degree: HeatCanvas.LINEAR, | ||
opacity: 0.6, | ||
colorscheme: null | ||
}, | ||
|
||
initialize: function (data, options) { // ([[latLng, value], ...], Object) | ||
var i, len; | ||
|
||
this._bounds = L.latLngBounds([data[0][0], data[0][0]]); | ||
|
||
for (i=1, len=data.length; i<len; i++) { | ||
this._bounds.extend(data[i][0]); | ||
} | ||
|
||
this.data = data; | ||
L.setOptions(this, options); | ||
}, | ||
|
||
_initImage: function () { | ||
L.ImageOverlay.Canvas.prototype._initImage.call(this); | ||
|
||
//this.canvas is a thing | ||
this.heatCanvas = new HeatCanvas(this.canvas); | ||
this.heatCanvas.bgcolor = this.options.bgcolor; | ||
}, | ||
|
||
_reset: function () { | ||
var canvas = this.canvas, | ||
topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()), | ||
size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft), | ||
i, len, pixel; | ||
|
||
L.DomUtil.setPosition(canvas, topLeft); | ||
|
||
this.heatCanvas.resize(size.x, size.y); | ||
|
||
this.heatCanvas.clear(); | ||
for (i=0, len=this.data.length; i<len; i++) { | ||
pixel = this._map.latLngToLayerPoint(this.data[i][0]); | ||
this.heatCanvas.push( | ||
Math.floor(pixel.x - topLeft.x), | ||
Math.floor(pixel.y - topLeft.y), | ||
this.data[i][1]); | ||
} | ||
this.heatCanvas.render(this.options.step, this.options.degree, this.options.colorscheme); | ||
} | ||
}); | ||
|
||
L.imageOverlay.heatCanvas = function (data, options) { | ||
return new L.ImageOverlay.HeatCanvas(data, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/shareabouts_client/static/lib/Leaflet.ImageOverlay.Canvas.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* L.ImageOverlay.Canvas is used to overlay HTML5 canvases over the map (to specific geographical bounds). | ||
*/ | ||
|
||
L.ImageOverlay.Canvas = L.ImageOverlay.extend({ | ||
initialize: function (bounds, options) { // (LatLngBounds, Object) | ||
this._bounds = L.latLngBounds(bounds); | ||
|
||
L.setOptions(this, options); | ||
}, | ||
|
||
_initImage: function () { | ||
var topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()), | ||
size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft); | ||
|
||
this._image = this.canvas = L.DomUtil.create('canvas', 'leaflet-image-layer'); | ||
this._image.width = size.x; | ||
this._image.height = size.y; | ||
|
||
if (this._map.options.zoomAnimation && L.Browser.any3d) { | ||
L.DomUtil.addClass(this._image, 'leaflet-zoom-animated'); | ||
} else { | ||
L.DomUtil.addClass(this._image, 'leaflet-zoom-hide'); | ||
} | ||
|
||
this._updateOpacity(); | ||
|
||
L.extend(this._image, { | ||
galleryimg: 'no', | ||
onselectstart: L.Util.falseFn, | ||
onmousemove: L.Util.falseFn, | ||
onload: L.bind(this._onImageLoad, this) | ||
}); | ||
}, | ||
|
||
_reset: function () { | ||
var image = this._image, | ||
topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()), | ||
size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft); | ||
|
||
L.DomUtil.setPosition(image, topLeft); | ||
|
||
image.style.width = size.x + 'px'; | ||
image.style.height = size.y + 'px'; | ||
}, | ||
|
||
_onImageLoad: function () { | ||
this.fire('load'); | ||
} | ||
}); | ||
|
||
L.imageOverlay.canvas = function (bounds, options) { | ||
return new L.ImageOverlay.Canvas(bounds, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters