Git Product home page Git Product logo

heatcanvas's Introduction

heatcanvas

Note that this project is no longer active maintained. Please let me know(file an issue or send me email) if you are interested in taking over it.

CDNJS

This is a simple heatmap api based on HTML5 canvas. A heat map is a graphical representation of data where the values taken by a variable in a two-dimensional table are represented as colors, according to Wikipedia.

You can find an interactive demo at http://sunng87.github.io/heatcanvas

Available via bower

bower install heatcanvas

Webpack Build

npm run webpack

This will generate a build in the /dist folder. This includes the main heatcanvas.js, the heatcanvas-worker.js Web Worker script, and specific versions for 51 Maps, Baidu Maps, Google Maps, Leaflet Maps, and OpenLayers.

To use, import into an HTML document using a script tag. The imports will be available at HeatCanvas, HeatCanvas51Layer, HeatCanvasBaiduLayer, HeatCanvasOverlayView, HeatCanvasLeaflet, and HeatCanvasOpenLayers respectively.

Usage

1. Create the HeatCanvas object

You can pass the canvas element object or its id to the constructor:

var heatmap = new HeatCanvas("canvasId");

2. Add some data

Add value to point (x,y) in canvas coordinate system.

heatmap.push(x, y, value);

3. Render the map

Call the render function on heatmap to draw it.

heatmap.render();

We use a simple formula to determine value of a pixel, by its distance to a point that holds data:

v = f(d)

The first two optional parameters of render define the formula.

  • step

  • degree

    v = Σ(datai - step * ddegree)

A set of constants are predefined for degree:

  • HeatCanvas.LINEAR
  • HeatCanvas.QUAD
  • HeatCanvas.CUBIC

For the third parameter of render, you can define a custom function to define color of pixels. For instance, we can use a mono-hue color scheme by this function:

var colorscheme = function(value){
    return [0.3, 0.75, value, 1];
}
heatmap.render(null, null, colorscheme);

The value for this function is guaranteed in (0,1].

4. Remove everything we just created

Call clear to erase the canvas and remove all data cached in heatmap instance.

heatmap.clear();

GoogleMap extension

HeatCanvas can be used as an OverlayView in GoogleMaps API V3.

Simply use the Map instance to create an HeatCanvasOverlayView

var heatmap = new HeatCanvasOverlayView(map, options);

Additional options available:

  • step, same as described in HeatCanvas.render
  • degree, same as described in HeatCanvas.render
  • colorscheme, same as described in HeatCanvas.render
  • opacity, the opacity of overlay view, [0,1]

Add data to map:

heatmap.pushData(latitude, longitude, value);

The map will be rendered automatically.

OpenLayers extension

Also we have a OpenLayer extension for you to embed heat map in your custom map application and OpenStreetMap.

The usage is still similar to GoogleMaps. First, construct your heat map layer with a name, OpenLayers map instance, layer options and HeatCanvas options:

var heatmap = new OpenLayers.Layer.HeatCanvas("HeatCanvas", map, {},
        {'step':0.3, 'degree':HeatCanvas.QUAD, 'opacity':0.8});

Add data to layer:

heatmap.pushData(latitude, longitude, value);

Add layer to map:

map.addLayer(heatmap);

Other extensions

There are also HeatCanvas extensions for:

  • Baidu Map (demo)
  • Cloudmade Leaflet (demo)

These extensions share similar API mentioned above. You can browse the source code of our demos to get detail.

License

HeatCanvas is released according to MIT License.

Thanks

  • @lbt05 for his patches on GoogleMap extension and BaiduMap implementation
  • @dazuma for his suggestion to speed up canvas rendering.

heatcanvas's People

Contributors

bozdoz avatar codefor avatar johansen avatar kennynaoh avatar ktopping avatar qbit avatar se-ti avatar stuporglue avatar sunng87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

heatcanvas's Issues

How to refresh layer with Leaflet/OSM

How do I refresh my heatmap with new data in a Leaflet/OSM environment?

I can successfully build the map using:
heatmap = new L.TileLayer.HeatCanvas...

and can load the map using:
for (var i in MapData) {
console.log('data');
heatmap.pushData(MapData[i][0], MapData[i][1], MapData[i][2])
}

My problem is I do not know how to clear the heatmap and load new data.

I have tried using heatmap.clear(), but this seems to have no effect as everytime I push data into the heatmap it simply adds to the existing data.

Does anyone know how to basically clear the data from a heatmap, load new data in, and redraw the map?

Black to Transparent Background

Sunng87,

I really love your work with this heatcanvas. The results look fantastic on our maps. I have one thing I need a little help with:

Similar to #13, I really need to overlay your heat canvas over a map where its features are visible. We place other features on our maps, such as custom pins and polygons, that need to work in conjunction with the heatmap functionality.

We've had some success stemming from implementing the suggestions found in #13, but this is the best we've done:

2013-09-17_06-52-30

What we need is something, that in the end, looks more like this:

2013-09-17_06-57-10

(Please note, we took this from heatmap.js examples, which are also quite good. We just prefer your method for various reasons.) Please let us know if you can provide some guidance. Also, please not that we are a professional shop, so we do pay for custom work. If this will be a lot of your time, we would be willing to discuss compensation.

Sincerely,

Jim

baidu地图的demo问题

我直接拷贝demo里面的代码来使用发现覆盖物没有办法正常显示,这是什么原因呢

Speed up canvas rendering using putImageData

Hi,

I had actually written something similar for one of my projects. Just wanted to pass on a suggestion based on what I had done.

Currently, you're rendering by calling context.fillRect() for each pixel. This can actually get quite slow. You might consider manipulating an ImageData directly. Call context.createImageData() to get an offscreen ImageData object. Then you can set pixels simply by setting rgba values in the data array. Finally, render the entire image back to the canvas using context.putImageData(). It generally turns out many, many times faster than the thousands of fillRect calls.

The downside is that you can't use the css-like syntax to set colors; you have to set raw rgba values numerically. But it may be a worthwhile trade-off.

Component.json

Add a component.json to make heatcanvas available to install from bower

Heatmap PNG's don't resize with zoom

On the Leaflet demo: http://sunng87.github.com/heatcanvas/leaflet.html

If you zoom in or out, the map data seems incorrect, because the PNGs cover more or less area. For example: at the initial zoom level, central-west America has no data, once zoomed out the data seems to span all of America, and zoomed in, it seems empty again.

leafletheatmap

There should be a way to resize the PNG appropriately once the map zoom level has changed.

SECURITY_ERR: DOM Exception 18

I was trying to create a small example oh Heatcanvas using Leaflet, however I always get a "SECURITY_ERR: DOM Exception 18" on heatcanvas.js:37 when I try to add the heatmap layer to my map.

I used the exact same code as the Leaflet HeatCanvas demo.
Anyone has any hints on what am I doing wrong?

I'm using Google Chrome v21.
Thanks for any feedback.

Uncaught TypeError: Cannot read property '_leaflet_id' of undefined(…)

Hi,
I have faced this problem with leaflet while using this plugin,Is there any way to fix it?
thanks.
here is error
leaflet.js:5 Uncaught TypeError: Cannot read property '_leaflet_id' of undefined(…)stamp @ leaflet.js:5_on @ leaflet.js:5on @ leaflet.js:5whenReady @ leaflet.js:6addLayer @ leaflet.js:6(anonymous function) @ sc.js:42i

sc.js:42 is this code

map.addLayer(heatmap);

thanks very much

Incorrect use of variable in Leaflet extension

Leaflet extension use a 'map' global variable, which works at demo page, but not real map.
As a result, a following error occurs (from my Chromium JS console):
Uncaught ReferenceError: map is not defined
L.TileLayer.HeatCanvas.L.Class.extend.initialize heatcanvas-leaflet.js:27
NewClass leaflet-src.js:162

// Next part is my page callback
(anonymous function) index.php:96
jQuery.Callbacks.fire jquery.js:1046
jQuery.Callbacks.self.fireWith jquery.js:1164
jQuery.extend.ready jquery.js:435
DOMContentLoaded

Hope it to be fixed soon. Thanks.

Where are docs?

I'm trying to modify settings such as background color, opacity, etc. Where are the docs located? I may have missed them but I don't see them.

thx

missing script webpack!!

I'm trying to install heatcanvas but running npm run webpack gives an error missing script webpack

I tried to npm install webpack but that didn't resolve the earlier issue. Any idea?

Heatmap.leaflet ignores layer controls

In my app I have something like this

App.markerLayer = new L.MarkerClusterGroup();
App.heatmapLayer = new L.TileLayer.HeatCanvas({
  map: App.map
});
.....
App.map.addLayer(App.markerLayer);
App.map.addLayer(App.heatmapLayer);
var mapLayers = {
  "Markers": App.markerLayer,
  "Heatmap": App.heatmapLayer
};
L.control.layers(mapLayers).addTo(App.map);

This adds both the marker layer and heatmap layer to the layer controls which show as radio buttons. When switching to only the Marker layer, the heatmap layer does not disappear (i.e there is no show/hide facility with it)

Screen Shot 2013-01-09 at 10 22 15

Uncaught Error: The provided object is not a Layer. with leaflet 1.3.1

 var heatmap = new L.TileLayer.HeatCanvas({}, {
        'step': 0.5,
        'degree': HeatCanvas.LINEAR,
        'opacity': 0.7
    });
// inside a loop
heatmap.pushData(a.coord.lat, a.coord.lon, a.main.temp);
//
map.addLayer(heatmap);

I have checked in console the heatmap object is there with the data and coordinates, when adding to map it gives the above error.

Why is the rest of the map darkened?

The HeatMap looks great, but when placed over a base layer it significantly darkens the map such that it's difficult to see what's going on underneath. I'm using the default opacity of 0.6.

It seems like the Heatmap.Canvas is writing black for all the places that don't have data. Is that accurate? Is there a way to turn that off?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.