Heatmap 3D application based on HTML5

Source: Internet
Author: User

Heatmap 3D application based on HTML5

Heatmap heat maps converge into visual color effects through various data points. Heat maps have been widely used in meteorological forecasting, medical imaging, data center temperature monitoring, and other industries, it is even used for data analysis in the field of competitive sports.

Many articles have shared the Heatmap generation principle. For details, refer to How to make heat maps and How to make heat maps in Flex. This article introduces the implementation method based on the HTML5 technology, mainly based on the application of 2D and 3D HTML5 technologies such as Cavans and WebGL, the interface effects and operation videos implemented in the final example are as follows:


Heatmapjs is a well-known open-source js library for implementing Heatmap. This framework has been developed for more than two years. The author Patrick Wied recently decided to provide paid commercial support services based on the open-source architecture, this is a good thing. Most open-source project authors on earth haven't updated this many years after creating an available primary version of barely, which of the following products can be used online without the need to continuously improve, enhance scalability, and provide special customized services, considering that the author has invested so much in the past two years (Over the last 2 years, I was tedmore than 500 hoursof work to improving heatmap. js to make it a truly great library .), I hope this article will help the author publicize the role in China.

Heatmapjs uses the Canvas 2D rendering method. This CPU-based rendering method is applicable to hundreds of thousands of points. However, if you need to calculate the effects of thousands of nodes in real time, we still have to rely on the more powerful concurrency GPU method. If we use HTML5, we can only use the WebGL solution. Fortunately, Florian Boesch provides the WebGL-based heatmap method in the "High Performance JS heatmaps" blog, and open-source.

Back to the example we want to implement, I will use heatmapjs to calculate the heat map in the memory, combined with the hightopo HT for Web 3D engine, A 3D network topology with a heap of node connections. A node represents a heat source. The closer the node is to the ground, the higher the ground temperature, in this way, the xz coordinate information of each node is used as the two-dimensional coordinate information of the Point xy to be passed into heatmapjs, and the elevation of the three-dimensional node is also the Y axis information, which is used as the distance information from the ground, the larger the distance, the smaller the value to be passed into heatmapjs. At last, start the Three-Dimensional Topology Auto-layout elastic algorithm of HT for Web, in this way, you can intuitively observe the temperature and heat map changes of the floor when the node is dynamically changing in different spatial locations.

The core of the code is to reload forceLayout. the onRelaxed function builds the information of all heat source nodes into the data required by heatmap during each Automatic Layout Process, and uses ht. default. setImage ('hm-bottom ', heatmap. _ renderer. canvas); register the canvas of the heat map as the HT image, and the floor elements of the floor are bound to the registered 'hm-bottom 'image, so that the canvas can be drawn in memory, then, we use the 3D engine of HT for Web to dynamically present the effect of Cavnas as texture information to 3D scenes.

The entire implementation code is less than a hundred lines below. You can also use commit!

 
 
  1. MAX = 500;
  2. WIDTH = 1024;
  3. HEIGHT = 512;
  4. function init() {
  5. dataModel = new ht.DataModel();
  6. g3d = new ht.graph3d.Graph3dView(dataModel);
  7. g3d.getMoveMode = function(e){ return 'xyz'; };
  8. view = g3d.getView();
  9. view.className = 'main';
  10. document.body.appendChild(view);
  11. window.addEventListener('resize', function (e) { g3d.invalidate(); }, false);
  12. heatmap = h337.create({ width: WIDTH, height: HEIGHT });
  13. ht.Default.setImage('hm-bottom', heatmap._renderer.canvas);
  14. var floor = new ht.Node();
  15. floor.s3(WIDTH, 1, HEIGHT);
  16. floor.s({
  17. '3d.selectable': false,
  18. 'layoutable': false,
  19. 'all.visible': false,
  20. 'top.visible': true,
  21. 'top.image': 'hm-bottom',
  22. 'top.reverse.flip': true,
  23. 'bottom.visible': true,
  24. 'bottom.transparent': true,
  25. 'bottom.opacity': 0.5,
  26. 'bottom.reverse.flip': true
  27. });
  28. dataModel.add(floor);
  29. var root = createNode();
  30. for (var i = 0; i < 3; i++) {
  31. var iNode = createNode();
  32. createEdge(root, iNode);
  33. for (var j = 0; j < 3; j++) {
  34. var jNode = createNode();
  35. createEdge(iNode, jNode);
  36. }
  37. }
  38. forceLayout = new ht.layout.Force3dLayout(g3d);
  39. forceLayout.start();
  40. forceLayout.onRelaxed = function(){
  41. var points = [];
  42. dataModel.each(function(data){
  43. if(data instanceof ht.Node && data !== floor){
  44. var p3 = data.p3();
  45. if(p3[1] > MAX){
  46. p3[1] = MAX;
  47. data.setElevation(MAX);
  48. }
  49. else if(p3[1] < -MAX){
  50. p3[1] = -MAX;
  51. data.setElevation(-MAX);
  52. }
  53. points.push({
  54. x: p3[0] + WIDTH/2,
  55. y: p3[2] + HEIGHT/2,
  56. value: MAX - Math.abs(p3[1])
  57. });
  58. }
  59. });
  60. heatmap.setData({data: points, min: 0, max: MAX});
  61. };
  62. }
  63. function createNode(){
  64. var node = new ht.Node();
  65. node.s({
  66. 'shape3d': 'sphere',
  67. 'shape3d.color': '#E74C3C',
  68. 'shape3d.opacity': 0.8,
  69. 'shape3d.transparent': true,
  70. 'shape3d.reverse.cull': true
  71. });
  72. node.s3(20, 20, 20);
  73. dataModel.add(node);
  74. return node;
  75. }
  76. function createEdge(sourceNode, targetNode){
  77. var edge = new ht.Edge(sourceNode, targetNode);
  78. edge.s({
  79. 'edge.width': 3,
  80. 'edge.offset': 10,
  81. 'shape3d': 'cylinder',
  82. 'shape3d.opacity': 0.7,
  83. 'shape3d.transparent': true,
  84. 'shape3d.reverse.cull': true
  85. });
  86. dataModel.add(edge);
  87. return edge;
  88. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.