BoostAttach.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* *
  2. *
  3. * Copyright (c) 2019-2020 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 GLRenderer from './WGLRenderer.js';
  14. import H from '../../Core/Globals.js';
  15. var doc = H.doc;
  16. import U from '../../Core/Utilities.js';
  17. var error = U.error;
  18. import '../../Core/Series/Series.js';
  19. var mainCanvas = doc.createElement('canvas');
  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 (!target.renderTarget) {
  50. target.canvas = mainCanvas;
  51. // Fall back to image tag if foreignObject isn't supported,
  52. // or if we're exporting.
  53. if (chart.renderer.forExport || !foSupported) {
  54. target.renderTarget = chart.renderer.image('', 0, 0, width, height)
  55. .addClass('highcharts-boost-canvas')
  56. .add(targetGroup);
  57. target.boostClear = function () {
  58. target.renderTarget.attr({ href: '' });
  59. };
  60. target.boostCopy = function () {
  61. target.boostResizeTarget();
  62. target.renderTarget.attr({
  63. href: target.canvas.toDataURL('image/png')
  64. });
  65. };
  66. }
  67. else {
  68. target.renderTargetFo = chart.renderer
  69. .createElement('foreignObject')
  70. .add(targetGroup);
  71. target.renderTarget = doc.createElement('canvas');
  72. target.renderTargetCtx =
  73. target.renderTarget.getContext('2d');
  74. target.renderTargetFo.element.appendChild(target.renderTarget);
  75. target.boostClear = function () {
  76. target.renderTarget.width =
  77. target.canvas.width;
  78. target.renderTarget.height =
  79. target.canvas.height;
  80. };
  81. target.boostCopy = function () {
  82. target.renderTarget.width =
  83. target.canvas.width;
  84. target.renderTarget.height =
  85. target.canvas.height;
  86. target.renderTargetCtx
  87. .drawImage(target.canvas, 0, 0);
  88. };
  89. }
  90. target.boostResizeTarget = function () {
  91. width = chart.chartWidth;
  92. height = chart.chartHeight;
  93. (target.renderTargetFo || target.renderTarget)
  94. .attr({
  95. x: 0,
  96. y: 0,
  97. width: width,
  98. height: height
  99. })
  100. .css({
  101. pointerEvents: 'none',
  102. mixedBlendMode: 'normal',
  103. opacity: alpha
  104. });
  105. if (target instanceof H.Chart) {
  106. target.markerGroup.translate(chart.plotLeft, chart.plotTop);
  107. }
  108. };
  109. target.boostClipRect = chart.renderer.clipRect();
  110. (target.renderTargetFo || target.renderTarget)
  111. .clip(target.boostClipRect);
  112. if (target instanceof H.Chart) {
  113. target.markerGroup = target.renderer.g().add(targetGroup);
  114. target.markerGroup.translate(series.xAxis.pos, series.yAxis.pos);
  115. }
  116. }
  117. target.canvas.width = width;
  118. target.canvas.height = height;
  119. target.boostClipRect.attr(chart.getBoostClipRect(target));
  120. target.boostResizeTarget();
  121. target.boostClear();
  122. if (!target.ogl) {
  123. target.ogl = GLRenderer(function () {
  124. if (target.ogl.settings.debug.timeBufferCopy) {
  125. console.time('buffer copy'); // eslint-disable-line no-console
  126. }
  127. target.boostCopy();
  128. if (target.ogl.settings.debug.timeBufferCopy) {
  129. console.timeEnd('buffer copy'); // eslint-disable-line no-console
  130. }
  131. });
  132. if (!target.ogl.init(target.canvas)) {
  133. // The OGL renderer couldn't be inited.
  134. // This likely means a shader error as we wouldn't get to this point
  135. // if there was no WebGL support.
  136. error('[highcharts boost] - unable to init WebGL renderer');
  137. }
  138. // target.ogl.clear();
  139. target.ogl.setOptions(chart.options.boost || {});
  140. if (target instanceof H.Chart) {
  141. target.ogl.allocateBuffer(chart);
  142. }
  143. }
  144. target.ogl.setSize(width, height);
  145. return target.ogl;
  146. }
  147. export default createAndAttachRenderer;