BoostAttach.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* *
  2. *
  3. * Copyright (c) 2019-2021 Highsoft AS
  4. *
  5. * Boost module: stripped-down renderer for higher performance
  6. *
  7. * License: highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import Chart from '../../Core/Chart/Chart.js';
  14. import GLRenderer from './WGLRenderer.js';
  15. import H from '../../Core/Globals.js';
  16. var doc = H.doc;
  17. import U from '../../Core/Utilities.js';
  18. var error = U.error;
  19. var mainCanvas;
  20. /**
  21. * Create a canvas + context and attach it to the target
  22. *
  23. * @private
  24. * @function createAndAttachRenderer
  25. *
  26. * @param {Highcharts.Chart} chart
  27. * the chart
  28. *
  29. * @param {Highcharts.Series} series
  30. * the series
  31. *
  32. * @return {Highcharts.BoostGLRenderer}
  33. * the canvas renderer
  34. */
  35. function createAndAttachRenderer(chart, series) {
  36. var width = chart.chartWidth, height = chart.chartHeight, target = chart, targetGroup = chart.seriesGroup || series.group, alpha = 1, foSupported = doc.implementation.hasFeature('www.http://w3.org/TR/SVG11/feature#Extensibility', '1.1');
  37. if (chart.isChartSeriesBoosting()) {
  38. target = chart;
  39. }
  40. else {
  41. target = series;
  42. }
  43. // Support for foreignObject is flimsy as best.
  44. // IE does not support it, and Chrome has a bug which messes up
  45. // the canvas draw order.
  46. // As such, we force the Image fallback for now, but leaving the
  47. // actual Canvas path in-place in case this changes in the future.
  48. foSupported = false;
  49. if (!mainCanvas) {
  50. mainCanvas = doc.createElement('canvas');
  51. }
  52. if (!target.renderTarget) {
  53. target.canvas = mainCanvas;
  54. // Fall back to image tag if foreignObject isn't supported,
  55. // or if we're exporting.
  56. if (chart.renderer.forExport || !foSupported) {
  57. target.renderTarget = chart.renderer.image('', 0, 0, width, height)
  58. .addClass('highcharts-boost-canvas')
  59. .add(targetGroup);
  60. target.boostClear = function () {
  61. target.renderTarget.attr({ href: '' });
  62. };
  63. target.boostCopy = function () {
  64. target.boostResizeTarget();
  65. target.renderTarget.attr({
  66. href: target.canvas.toDataURL('image/png')
  67. });
  68. };
  69. }
  70. else {
  71. target.renderTargetFo = chart.renderer
  72. .createElement('foreignObject')
  73. .add(targetGroup);
  74. target.renderTarget = doc.createElement('canvas');
  75. target.renderTargetCtx =
  76. target.renderTarget.getContext('2d');
  77. target.renderTargetFo.element.appendChild(target.renderTarget);
  78. target.boostClear = function () {
  79. target.renderTarget.width =
  80. target.canvas.width;
  81. target.renderTarget.height =
  82. target.canvas.height;
  83. };
  84. target.boostCopy = function () {
  85. target.renderTarget.width =
  86. target.canvas.width;
  87. target.renderTarget.height =
  88. target.canvas.height;
  89. target.renderTargetCtx
  90. .drawImage(target.canvas, 0, 0);
  91. };
  92. }
  93. target.boostResizeTarget = function () {
  94. width = chart.chartWidth;
  95. height = chart.chartHeight;
  96. (target.renderTargetFo || target.renderTarget)
  97. .attr({
  98. x: 0,
  99. y: 0,
  100. width: width,
  101. height: height
  102. })
  103. .css({
  104. pointerEvents: 'none',
  105. mixedBlendMode: 'normal',
  106. opacity: alpha
  107. });
  108. if (target instanceof Chart) {
  109. target.markerGroup.translate(chart.plotLeft, chart.plotTop);
  110. }
  111. };
  112. target.boostClipRect = chart.renderer.clipRect();
  113. (target.renderTargetFo || target.renderTarget)
  114. .clip(target.boostClipRect);
  115. if (target instanceof Chart) {
  116. target.markerGroup = target.renderer.g().add(targetGroup);
  117. target.markerGroup.translate(series.xAxis.pos, series.yAxis.pos);
  118. }
  119. }
  120. target.canvas.width = width;
  121. target.canvas.height = height;
  122. target.boostClipRect.attr(chart.getBoostClipRect(target));
  123. target.boostResizeTarget();
  124. target.boostClear();
  125. if (!target.ogl) {
  126. target.ogl = GLRenderer(function () {
  127. if (target.ogl.settings.debug.timeBufferCopy) {
  128. console.time('buffer copy'); // eslint-disable-line no-console
  129. }
  130. target.boostCopy();
  131. if (target.ogl.settings.debug.timeBufferCopy) {
  132. console.timeEnd('buffer copy'); // eslint-disable-line no-console
  133. }
  134. });
  135. if (!target.ogl.init(target.canvas)) {
  136. // The OGL renderer couldn't be inited.
  137. // This likely means a shader error as we wouldn't get to this point
  138. // if there was no WebGL support.
  139. error('[highcharts boost] - unable to init WebGL renderer');
  140. }
  141. // target.ogl.clear();
  142. target.ogl.setOptions(chart.options.boost || {});
  143. if (target instanceof Chart) {
  144. target.ogl.allocateBuffer(chart);
  145. }
  146. }
  147. target.ogl.setSize(width, height);
  148. return target.ogl;
  149. }
  150. export default createAndAttachRenderer;