canvas.js 6.6 KB

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