HeatmapLayer.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /* global Uint8ClampedArray */
  41. import { platformApi } from 'zrender/lib/core/platform.js';
  42. var GRADIENT_LEVELS = 256;
  43. var HeatmapLayer =
  44. /** @class */
  45. function () {
  46. function HeatmapLayer() {
  47. this.blurSize = 30;
  48. this.pointSize = 20;
  49. this.maxOpacity = 1;
  50. this.minOpacity = 0;
  51. this._gradientPixels = {
  52. inRange: null,
  53. outOfRange: null
  54. };
  55. var canvas = platformApi.createCanvas();
  56. this.canvas = canvas;
  57. }
  58. /**
  59. * Renders Heatmap and returns the rendered canvas
  60. * @param data array of data, each has x, y, value
  61. * @param width canvas width
  62. * @param height canvas height
  63. */
  64. HeatmapLayer.prototype.update = function (data, width, height, normalize, colorFunc, isInRange) {
  65. var brush = this._getBrush();
  66. var gradientInRange = this._getGradient(colorFunc, 'inRange');
  67. var gradientOutOfRange = this._getGradient(colorFunc, 'outOfRange');
  68. var r = this.pointSize + this.blurSize;
  69. var canvas = this.canvas;
  70. var ctx = canvas.getContext('2d');
  71. var len = data.length;
  72. canvas.width = width;
  73. canvas.height = height;
  74. for (var i = 0; i < len; ++i) {
  75. var p = data[i];
  76. var x = p[0];
  77. var y = p[1];
  78. var value = p[2]; // calculate alpha using value
  79. var alpha = normalize(value); // draw with the circle brush with alpha
  80. ctx.globalAlpha = alpha;
  81. ctx.drawImage(brush, x - r, y - r);
  82. }
  83. if (!canvas.width || !canvas.height) {
  84. // Avoid "Uncaught DOMException: Failed to execute 'getImageData' on
  85. // 'CanvasRenderingContext2D': The source height is 0."
  86. return canvas;
  87. } // colorize the canvas using alpha value and set with gradient
  88. var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  89. var pixels = imageData.data;
  90. var offset = 0;
  91. var pixelLen = pixels.length;
  92. var minOpacity = this.minOpacity;
  93. var maxOpacity = this.maxOpacity;
  94. var diffOpacity = maxOpacity - minOpacity;
  95. while (offset < pixelLen) {
  96. var alpha = pixels[offset + 3] / 256;
  97. var gradientOffset = Math.floor(alpha * (GRADIENT_LEVELS - 1)) * 4; // Simple optimize to ignore the empty data
  98. if (alpha > 0) {
  99. var gradient = isInRange(alpha) ? gradientInRange : gradientOutOfRange; // Any alpha > 0 will be mapped to [minOpacity, maxOpacity]
  100. alpha > 0 && (alpha = alpha * diffOpacity + minOpacity);
  101. pixels[offset++] = gradient[gradientOffset];
  102. pixels[offset++] = gradient[gradientOffset + 1];
  103. pixels[offset++] = gradient[gradientOffset + 2];
  104. pixels[offset++] = gradient[gradientOffset + 3] * alpha * 256;
  105. } else {
  106. offset += 4;
  107. }
  108. }
  109. ctx.putImageData(imageData, 0, 0);
  110. return canvas;
  111. };
  112. /**
  113. * get canvas of a black circle brush used for canvas to draw later
  114. */
  115. HeatmapLayer.prototype._getBrush = function () {
  116. var brushCanvas = this._brushCanvas || (this._brushCanvas = platformApi.createCanvas()); // set brush size
  117. var r = this.pointSize + this.blurSize;
  118. var d = r * 2;
  119. brushCanvas.width = d;
  120. brushCanvas.height = d;
  121. var ctx = brushCanvas.getContext('2d');
  122. ctx.clearRect(0, 0, d, d); // in order to render shadow without the distinct circle,
  123. // draw the distinct circle in an invisible place,
  124. // and use shadowOffset to draw shadow in the center of the canvas
  125. ctx.shadowOffsetX = d;
  126. ctx.shadowBlur = this.blurSize; // draw the shadow in black, and use alpha and shadow blur to generate
  127. // color in color map
  128. ctx.shadowColor = '#000'; // draw circle in the left to the canvas
  129. ctx.beginPath();
  130. ctx.arc(-r, r, this.pointSize, 0, Math.PI * 2, true);
  131. ctx.closePath();
  132. ctx.fill();
  133. return brushCanvas;
  134. };
  135. /**
  136. * get gradient color map
  137. * @private
  138. */
  139. HeatmapLayer.prototype._getGradient = function (colorFunc, state) {
  140. var gradientPixels = this._gradientPixels;
  141. var pixelsSingleState = gradientPixels[state] || (gradientPixels[state] = new Uint8ClampedArray(256 * 4));
  142. var color = [0, 0, 0, 0];
  143. var off = 0;
  144. for (var i = 0; i < 256; i++) {
  145. colorFunc[state](i / 255, true, color);
  146. pixelsSingleState[off++] = color[0];
  147. pixelsSingleState[off++] = color[1];
  148. pixelsSingleState[off++] = color[2];
  149. pixelsSingleState[off++] = color[3];
  150. }
  151. return pixelsSingleState;
  152. };
  153. return HeatmapLayer;
  154. }();
  155. export default HeatmapLayer;