123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- "use strict";
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
- var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
- var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
- var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
- var _emit = _interopRequireDefault(require("./event/emit"));
- var _controller = _interopRequireDefault(require("./event/controller"));
- var _canvasElement = _interopRequireDefault(require("./canvas-element"));
- function _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
- var Util = require('../util/common');
- var Container = require('./container');
- var Group = require('./group');
- var _require = require('./util/requestAnimationFrame'),
- requestAnimationFrame = _require.requestAnimationFrame;
- var Canvas = /*#__PURE__*/function (_EventEmit) {
- (0, _inheritsLoose2["default"])(Canvas, _EventEmit);
- var _super = _createSuper(Canvas);
- var _proto = Canvas.prototype;
- _proto.get = function get(name) {
- return this._attrs[name];
- };
- _proto.set = function set(name, value) {
- this._attrs[name] = value;
- };
- function Canvas(cfg) {
- var _this;
- _this = _EventEmit.call(this) || this;
- _this._attrs = Util.mix({
- type: 'canvas',
- children: []
- }, cfg);
- _this._initPixelRatio();
- _this._initCanvas();
- return _this;
- }
- _proto._initPixelRatio = function _initPixelRatio() {
- var pixelRatio = this.get('pixelRatio');
- if (!pixelRatio) {
- this.set('pixelRatio', Util.getPixelRatio());
- }
- };
- _proto.beforeDraw = function beforeDraw() {
- var context = this._attrs.context;
- var el = this._attrs.el;
- context && context.clearRect && context.clearRect(0, 0, el.width, el.height);
- };
- _proto._initCanvas = function _initCanvas() {
- var self = this;
- var el = self.get('el');
- var context = self.get('context');
- if (!el && !context) {
- throw new Error('Please specify the id, el or context of the chart!');
- }
- var canvas;
- if (el) {
- // DOMElement or String
- canvas = Util.isString(el) ? Util.getDomById(el) : el;
- } else {
- // 说明没有指定el
- canvas = _canvasElement["default"].create(context);
- }
- if (context && canvas && !canvas.getContext) {
- canvas.getContext = function () {
- return context;
- };
- }
- var width = self.get('width');
- if (!width) {
- width = Util.getWidth(canvas);
- }
- var height = self.get('height');
- if (!height) {
- height = Util.getHeight(canvas);
- }
- self.set('canvas', this);
- self.set('el', canvas);
- self.set('context', context || canvas.getContext('2d'));
- self.changeSize(width, height); // 初始化事件控制器
- var eventController = new _controller["default"]({
- canvas: this,
- el: canvas
- });
- self.set('eventController', eventController);
- };
- _proto.changeSize = function changeSize(width, height) {
- var pixelRatio = this.get('pixelRatio');
- var canvasDOM = this.get('el'); // HTMLCanvasElement or canvasElement
- // 浏览器环境设置style样式
- if (canvasDOM.style) {
- canvasDOM.style.width = width + 'px';
- canvasDOM.style.height = height + 'px';
- }
- if (Util.isCanvasElement(canvasDOM)) {
- canvasDOM.width = width * pixelRatio;
- canvasDOM.height = height * pixelRatio;
- if (pixelRatio !== 1) {
- var ctx = this.get('context');
- ctx.scale(pixelRatio, pixelRatio);
- }
- }
- this.set('width', width);
- this.set('height', height);
- };
- _proto.getWidth = function getWidth() {
- var pixelRatio = this.get('pixelRatio');
- var width = this.get('width');
- return width * pixelRatio;
- };
- _proto.getHeight = function getHeight() {
- var pixelRatio = this.get('pixelRatio');
- var height = this.get('height');
- return height * pixelRatio;
- };
- _proto.getPointByClient = function getPointByClient(clientX, clientY) {
- var el = this.get('el');
- var bbox = el.getBoundingClientRect();
- var width = bbox.right - bbox.left;
- var height = bbox.bottom - bbox.top;
- return {
- x: (clientX - bbox.left) * (el.width / width),
- y: (clientY - bbox.top) * (el.height / height)
- };
- };
- _proto._beginDraw = function _beginDraw() {
- this._attrs.toDraw = true;
- };
- _proto._endDraw = function _endDraw() {
- this._attrs.toDraw = false;
- };
- _proto.draw = function draw() {
- var self = this;
- function drawInner() {
- self.set('animateHandler', requestAnimationFrame(function () {
- self.set('animateHandler', undefined);
- if (self.get('toDraw')) {
- drawInner();
- }
- }));
- self.beforeDraw();
- try {
- var context = self._attrs.context;
- var children = self._attrs.children;
- for (var i = 0, len = children.length; i < len; i++) {
- var child = children[i];
- child.draw(context);
- } // 支付宝,微信小程序,需要调context.draw才能完成绘制, 所以这里直接判断是否有.draw方法
- if (context.draw) {
- context.draw();
- }
- } catch (ev) {
- console.warn('error in draw canvas, detail as:');
- console.warn(ev);
- self._endDraw();
- }
- self._endDraw();
- }
- if (self.get('destroyed')) {
- return;
- }
- if (self.get('animateHandler')) {
- this._beginDraw();
- } else {
- drawInner();
- }
- };
- _proto.destroy = function destroy() {
- if (this.get('destroyed')) {
- return;
- } // 需要清理 canvas 画布内容,否则会导致 spa 应用 ios 下 canvas 白屏
- // https://stackoverflow.com/questions/52532614/total-canvas-memory-use-exceeds-the-maximum-limit-safari-12
- // https://github.com/antvis/F2/issues/630
- var el = this.get('el');
- el.width = 0;
- el.height = 0;
- this.clear();
- this._attrs = {};
- this.set('destroyed', true);
- };
- _proto.isDestroyed = function isDestroyed() {
- return this.get('destroyed');
- };
- return Canvas;
- }(_emit["default"]);
- Util.mix(Canvas.prototype, Container, {
- getGroupClass: function getGroupClass() {
- return Group;
- }
- });
- module.exports = Canvas;
|