UNPKG

4.91 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var g_base_1 = require("@antv/g-base");
5var Shape = require("./shape");
6var draw_1 = require("./util/draw");
7var util_1 = require("@antv/util");
8var util_2 = require("./util/util");
9var Group = /** @class */ (function (_super) {
10 tslib_1.__extends(Group, _super);
11 function Group() {
12 return _super !== null && _super.apply(this, arguments) || this;
13 }
14 /**
15 * 一些方法调用会引起画布变化
16 * @param {ChangeType} changeType 改变的类型
17 */
18 Group.prototype.onCanvasChange = function (changeType) {
19 draw_1.refreshElement(this, changeType);
20 };
21 Group.prototype.getShapeBase = function () {
22 return Shape;
23 };
24 Group.prototype.getGroupBase = function () {
25 return Group;
26 };
27 // 同 shape 中的方法重复了
28 Group.prototype._applyClip = function (context, clip) {
29 if (clip) {
30 context.save();
31 // 将 clip 的属性挂载到 context 上
32 draw_1.applyAttrsToContext(context, clip);
33 // 绘制 clip 路径
34 clip.createPath(context);
35 context.restore();
36 // 裁剪
37 context.clip();
38 clip._afterDraw();
39 }
40 };
41 // 这个方法以前直接使用的 getCanvasBBox,由于 group 上没有缓存,所以每次重新计算,导致性能开销比较大
42 // 大概能够节省全局渲染 15-20% 的性能,如果不在这里加缓存优化后 10W 个节点无法达到 5-6 ms,大概能够 30-40ms
43 Group.prototype.cacheCanvasBBox = function () {
44 var children = this.cfg.children;
45 var xArr = [];
46 var yArr = [];
47 util_1.each(children, function (child) {
48 var bbox = child.cfg.cacheCanvasBBox;
49 // isInview 的判定是一旦图形或者分组渲染就要计算是否在视图内,
50 // 这个判定 10W 个图形下差不多能够节省 5-6 ms 的开销
51 if (bbox && child.cfg.isInView) {
52 xArr.push(bbox.minX, bbox.maxX);
53 yArr.push(bbox.minY, bbox.maxY);
54 }
55 });
56 var bbox = null;
57 if (xArr.length) {
58 var minX = Math.min.apply(null, xArr);
59 var maxX = Math.max.apply(null, xArr);
60 var minY = Math.min.apply(null, yArr);
61 var maxY = Math.max.apply(null, yArr);
62 bbox = {
63 minX: minX,
64 minY: minY,
65 x: minX,
66 y: minY,
67 maxX: maxX,
68 maxY: maxY,
69 width: maxX - minX,
70 height: maxY - minY,
71 };
72 var canvas = this.cfg.canvas;
73 if (canvas) {
74 var viewRange = canvas.getViewRange();
75 // 如果这个地方判定 isInView == false 设置 bbox 为 false 的话,拾取的性能会更高
76 // 但是目前 10W 图形的拾取在 2-5ms 内,这个优化意义不大,可以后期观察再看
77 this.set('isInView', util_2.intersectRect(bbox, viewRange));
78 }
79 }
80 else {
81 this.set('isInView', false);
82 }
83 this.set('cacheCanvasBBox', bbox);
84 };
85 Group.prototype.draw = function (context, region) {
86 var children = this.cfg.children;
87 var allowDraw = region ? this.cfg.refresh : true; // 局部刷新需要判定
88 // 这个地方需要判定,在 G6 的场景每个 group 都有 transform 的场景下性能会开销非常大
89 // 通过 refresh 的判定,可以不刷新没有发生过变化的分组,不在视窗内的分组等等
90 // 如果想进一步提升局部渲染性能,可以进一步优化 refresh 的判定,依然有潜力
91 if (children.length && allowDraw) {
92 context.save();
93 // group 上的矩阵和属性也会应用到上下文上
94 // 先将 attrs 应用到上下文中,再设置 clip。因为 clip 应该被当前元素的 matrix 所影响
95 draw_1.applyAttrsToContext(context, this);
96 this._applyClip(context, this.getClip());
97 draw_1.drawChildren(context, children, region);
98 context.restore();
99 this.cacheCanvasBBox();
100 }
101 // 这里的成本比较大,如果不绘制则不再
102 // this.set('cacheCanvasBBox', this.getCanvasBBox());
103 this.cfg.refresh = null;
104 // 绘制后,消除更新标记
105 this.set('hasChanged', false);
106 };
107 // 绘制时被跳过,一般发生在分组隐藏时
108 Group.prototype.skipDraw = function () {
109 this.set('cacheCanvasBBox', null);
110 this.set('hasChanged', false);
111 };
112 return Group;
113}(g_base_1.AbstractGroup));
114exports.default = Group;
115//# sourceMappingURL=group.js.map
\No newline at end of file