Sunng's Canvas based Heatmap API

Glad to announce my works this morning: A simple heatmap API based on HTML5 canvas.
canvas

The programming interface is rather simple now. To create such a heatmap, you just new a heatmap object by:

<canvas width="300" height="215" id="canv"></canvas>
heatmap = new HeatMap("canv");

Then read your dataset and push data into the heatmap:

heatmap.push(x, y, value);

At last, spread the data and get the canvas rendered:

heatmap.spread();
heatmap.render();

Now you got it.

For advanced usage, you can specify the resolution of heatmap by px:

heatmap = new HeatMap("canv", 2);

Large value will gain performance with low image quality.

Also, you can specify a value to define the attenuation value of each px.

heatmap.spread(5);

Finally, there is an option to use your own color schema for heatmap generation.

heatmap.render(function(value, maxValue){
    var light = value / maxValue * 100;
    return "hsl(20, 75%, "+light+"%)";
});

The parameters passed in are current pixel value and max value in whole context.

That’s all the toolkit. More functionality and options might be added in future. Grab it from my bitbucket page:
http://bitbucket.org/sunng/daily-coding/src/tip/canvas-heatmap/

There is a demo page which you can test the api by clicking canvas:
canvas-raw
then click heatmap button!
canvas2

This is the new year gift for my readers and my dear friends !

26 thoughts on “Sunng's Canvas based Heatmap API

  1. 然后我用gwt-canvas还有一个问题,就是产生fillStyle的时候hsl表达式无效,改成rgba是可以的,不知道是什么原因。

  2. 对了,建议把衰减函数改成:
    double value = data.get(s) / maxValue – Math.log1p(distance / maxRadius) / Math.log(2);

    然后fillStyle就是”hsl(” + (1.0 – value) * 240 + “, 50%, 50%)”;

    这样看上去衰减要舒服一些,然后maxRadius比step要好理解。

  3. 我是在gwt里写的,就是log(1+x),对了好像这么算值域还是不对,这样貌似才对:

    (value/maxValue – Math.log1p(distance/radius) / Math.log(2) + 1) / 2;

    这样才能落到[0,1)……

  4. log1p()/log2是为了改变变化趋势,我是由gwt的源码里animation的实现为了使变化平滑改变了间隔频率想到的,不过效果貌似不是很明显……

  5. 试验了几个发现对数拉伸还不如直接取0……
    final double r = value/maxValue – distance/maxRadius;
    return r > 0 ? r : 0;

    • 不错不错,我看到了

      有必要看一下性能瓶颈在spread还是render,如果是在spread,可以在pan的时候直接translate那个坐标。如果是render就只好换浏览器了。

      我昨天下班还在想把它做到OpenLayers里作为一个Layer,最近要评估一下看看可行性如何

      • 应该是spread,pan的时候就是直接调整offset的,但是mouseup的时候会map panel调用commit机制,这时候所有图层都会重绘保证数据的一致性,就是在这个时候会卡一下。包括切换工具、打开关闭图层都会commit保证一致性,所以如果打开了heatmap这些时候都会卡一下。

      • 现在看问题应该不是在HashMap,GWT1.5之后HashMap性能有极大的提高(http://development.lombardi.com/?p=797),render应该改没问题的,在gnome上绘制都比较慢的,就不知道在spread的哪个环节出问题了……

  6. gwt确实是个好东西,java的开发效率比js高太多了,而且也可以用jsni直接调用raw js,或者通过jsni暴露出js api。如果需要有ext那样的完备效果,也有ext gwt这样的wrapper。

  7. Pingback: HeatCanvas hits 1.0, and public available

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>