canvas.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import EventEmit from '../event/emit';
  2. import EventController from '../event/controller';
  3. import CanvasElement from './canvas-element';
  4. import { mix, getPixelRatio, isString, getDomById, getWidth, getHeight, isCanvasElement, substitute } from '../../util/common';
  5. import Container from './container';
  6. import Group from './group';
  7. import { requestAnimationFrame } from '../util/requestAnimationFrame';
  8. import { lang } from '../../global';
  9. class Canvas extends EventEmit {
  10. get(name) {
  11. return this._attrs[name];
  12. }
  13. set(name, value) {
  14. this._attrs[name] = value;
  15. }
  16. constructor(cfg) {
  17. super();
  18. var {
  19. title
  20. } = cfg;
  21. var ariaLabel = title ? substitute(lang.general.withTitle, {
  22. title
  23. }) : lang.general.title;
  24. this._attrs = mix({
  25. type: 'canvas',
  26. children: [],
  27. ariaLabel
  28. }, cfg);
  29. this._initPixelRatio();
  30. this._initCanvas();
  31. }
  32. _initPixelRatio() {
  33. var pixelRatio = this.get('pixelRatio');
  34. if (!pixelRatio) {
  35. this.set('pixelRatio', getPixelRatio());
  36. }
  37. }
  38. beforeDraw() {
  39. var context = this._attrs.context;
  40. var el = this._attrs.el;
  41. context && context.clearRect && context.clearRect(0, 0, el.width, el.height);
  42. }
  43. _initCanvas() {
  44. var self = this;
  45. var el = self.get('el');
  46. var context = self.get('context');
  47. if (!el && !context) {
  48. throw new Error('Please specify the id, el or context of the chart!');
  49. }
  50. var canvas;
  51. if (el) {
  52. // DOMElement or String
  53. canvas = isString(el) ? getDomById(el) : el;
  54. } else {
  55. // 说明没有指定el
  56. canvas = CanvasElement.create(context);
  57. }
  58. if (context && canvas && !canvas.getContext) {
  59. canvas.getContext = function () {
  60. return context;
  61. };
  62. }
  63. var width = self.get('width');
  64. if (!width) {
  65. width = getWidth(canvas);
  66. }
  67. var height = self.get('height');
  68. if (!height) {
  69. height = getHeight(canvas);
  70. }
  71. self.set('canvas', this);
  72. self.set('el', canvas);
  73. self.set('context', context || canvas.getContext('2d'));
  74. self.changeSize(width, height); // 初始化事件控制器
  75. var eventController = new EventController({
  76. canvas: this,
  77. el: canvas
  78. });
  79. self.set('eventController', eventController);
  80. }
  81. changeSize(width, height) {
  82. var pixelRatio = this.get('pixelRatio');
  83. var canvasDOM = this.get('el'); // HTMLCanvasElement or canvasElement
  84. // 浏览器环境设置style样式
  85. if (canvasDOM.style) {
  86. canvasDOM.style.width = width + 'px';
  87. canvasDOM.style.height = height + 'px';
  88. }
  89. if (isCanvasElement(canvasDOM)) {
  90. canvasDOM.width = width * pixelRatio;
  91. canvasDOM.height = height * pixelRatio;
  92. if (pixelRatio !== 1) {
  93. var ctx = this.get('context');
  94. ctx.scale(pixelRatio, pixelRatio);
  95. }
  96. }
  97. this.set('width', width);
  98. this.set('height', height);
  99. }
  100. getWidth() {
  101. var pixelRatio = this.get('pixelRatio');
  102. var width = this.get('width');
  103. return width * pixelRatio;
  104. }
  105. getHeight() {
  106. var pixelRatio = this.get('pixelRatio');
  107. var height = this.get('height');
  108. return height * pixelRatio;
  109. }
  110. getPointByClient(clientX, clientY) {
  111. var el = this.get('el');
  112. var bbox = el.getBoundingClientRect();
  113. var width = bbox.right - bbox.left;
  114. var height = bbox.bottom - bbox.top;
  115. return {
  116. x: (clientX - bbox.left) * (el.width / width),
  117. y: (clientY - bbox.top) * (el.height / height)
  118. };
  119. }
  120. _beginDraw() {
  121. this._attrs.toDraw = true;
  122. }
  123. _endDraw() {
  124. this._attrs.toDraw = false;
  125. }
  126. draw() {
  127. var self = this;
  128. function drawInner() {
  129. self.set('animateHandler', requestAnimationFrame(function () {
  130. self.set('animateHandler', undefined);
  131. if (self.get('toDraw')) {
  132. drawInner();
  133. }
  134. }));
  135. self.beforeDraw();
  136. try {
  137. var context = self._attrs.context;
  138. self.drawChildren(context); // 支付宝,微信小程序,需要调context.draw才能完成绘制, 所以这里直接判断是否有.draw方法
  139. if (context.draw) {
  140. context.draw();
  141. } // 设置无障碍文本
  142. self.setAriaLabel();
  143. } catch (ev) {
  144. console.warn('error in draw canvas, detail as:');
  145. console.warn(ev);
  146. self._endDraw();
  147. }
  148. self._endDraw();
  149. }
  150. if (self.get('destroyed')) {
  151. return;
  152. }
  153. if (self.get('animateHandler')) {
  154. this._beginDraw();
  155. } else {
  156. drawInner();
  157. }
  158. } // 设置无障碍文本
  159. setAriaLabel() {
  160. var {
  161. el
  162. } = this._attrs;
  163. var ariaLabel = this._getAriaLabel();
  164. if (ariaLabel && el.setAttribute) {
  165. el.setAttribute('aria-label', ariaLabel);
  166. }
  167. }
  168. destroy() {
  169. if (this.get('destroyed')) {
  170. return;
  171. } // 需要清理 canvas 画布内容,否则会导致 spa 应用 ios 下 canvas 白屏
  172. // https://stackoverflow.com/questions/52532614/total-canvas-memory-use-exceeds-the-maximum-limit-safari-12
  173. // https://github.com/antvis/F2/issues/630
  174. var el = this.get('el');
  175. el.width = 0;
  176. el.height = 0;
  177. this.clear();
  178. this._attrs = {};
  179. this.set('destroyed', true);
  180. }
  181. isDestroyed() {
  182. return this.get('destroyed');
  183. }
  184. }
  185. mix(Canvas.prototype, Container, {
  186. getGroupClass() {
  187. return Group;
  188. }
  189. });
  190. export default Canvas;