1 | import { __extends } from "tslib";
|
2 | import { AbstractCanvas } from '@antv/g-base';
|
3 | import { getShape } from './util/hit';
|
4 | import * as Shape from './shape';
|
5 | import Group from './group';
|
6 | import { each, getPixelRatio, requestAnimationFrame, clearAnimationFrame } from './util/util';
|
7 | import { applyAttrsToContext, drawChildren, getMergedRegion, mergeView, checkRefresh, clearChanged } from './util/draw';
|
8 | var Canvas = (function (_super) {
|
9 | __extends(Canvas, _super);
|
10 | function Canvas() {
|
11 | return _super !== null && _super.apply(this, arguments) || this;
|
12 | }
|
13 | Canvas.prototype.getDefaultCfg = function () {
|
14 | var cfg = _super.prototype.getDefaultCfg.call(this);
|
15 |
|
16 | cfg['renderer'] = 'canvas';
|
17 |
|
18 | cfg['autoDraw'] = true;
|
19 |
|
20 | cfg['localRefresh'] = true;
|
21 | cfg['refreshElements'] = [];
|
22 |
|
23 | cfg['clipView'] = true;
|
24 |
|
25 | cfg['quickHit'] = false;
|
26 | return cfg;
|
27 | };
|
28 | |
29 |
|
30 |
|
31 |
|
32 | Canvas.prototype.onCanvasChange = function (changeType) {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | if (changeType === 'attr' || changeType === 'sort' || changeType === 'changeSize') {
|
40 | this.set('refreshElements', [this]);
|
41 | this.draw();
|
42 | }
|
43 | };
|
44 | Canvas.prototype.getShapeBase = function () {
|
45 | return Shape;
|
46 | };
|
47 | Canvas.prototype.getGroupBase = function () {
|
48 | return Group;
|
49 | };
|
50 | |
51 |
|
52 |
|
53 | Canvas.prototype.getPixelRatio = function () {
|
54 | var pixelRatio = this.get('pixelRatio') || getPixelRatio();
|
55 |
|
56 | return pixelRatio >= 1 ? Math.ceil(pixelRatio) : 1;
|
57 | };
|
58 | Canvas.prototype.getViewRange = function () {
|
59 | return {
|
60 | minX: 0,
|
61 | minY: 0,
|
62 | maxX: this.cfg.width,
|
63 | maxY: this.cfg.height,
|
64 | };
|
65 | };
|
66 |
|
67 | Canvas.prototype.createDom = function () {
|
68 | var element = document.createElement('canvas');
|
69 | var context = element.getContext('2d');
|
70 |
|
71 | this.set('context', context);
|
72 | return element;
|
73 | };
|
74 | Canvas.prototype.setDOMSize = function (width, height) {
|
75 | _super.prototype.setDOMSize.call(this, width, height);
|
76 | var context = this.get('context');
|
77 | var el = this.get('el');
|
78 | var pixelRatio = this.getPixelRatio();
|
79 | el.width = pixelRatio * width;
|
80 | el.height = pixelRatio * height;
|
81 |
|
82 | if (pixelRatio > 1) {
|
83 | context.scale(pixelRatio, pixelRatio);
|
84 | }
|
85 | };
|
86 |
|
87 | Canvas.prototype.clear = function () {
|
88 | _super.prototype.clear.call(this);
|
89 | this._clearFrame();
|
90 | var context = this.get('context');
|
91 | var element = this.get('el');
|
92 | context.clearRect(0, 0, element.width, element.height);
|
93 | };
|
94 | Canvas.prototype.getShape = function (x, y) {
|
95 | var shape;
|
96 | if (this.get('quickHit')) {
|
97 | shape = getShape(this, x, y);
|
98 | }
|
99 | else {
|
100 | shape = _super.prototype.getShape.call(this, x, y, null);
|
101 | }
|
102 | return shape;
|
103 | };
|
104 |
|
105 | Canvas.prototype._getRefreshRegion = function () {
|
106 | var elements = this.get('refreshElements');
|
107 | var viewRegion = this.getViewRange();
|
108 | var region;
|
109 |
|
110 | if (elements.length && elements[0] === this) {
|
111 | region = viewRegion;
|
112 | }
|
113 | else {
|
114 | region = getMergedRegion(elements);
|
115 | if (region) {
|
116 | region.minX = Math.floor(region.minX);
|
117 | region.minY = Math.floor(region.minY);
|
118 | region.maxX = Math.ceil(region.maxX);
|
119 | region.maxY = Math.ceil(region.maxY);
|
120 | region.maxY += 1;
|
121 | var clipView = this.get('clipView');
|
122 |
|
123 | if (clipView) {
|
124 | region = mergeView(region, viewRegion);
|
125 | }
|
126 | }
|
127 | }
|
128 | return region;
|
129 | };
|
130 | |
131 |
|
132 |
|
133 |
|
134 | Canvas.prototype.refreshElement = function (element) {
|
135 | var refreshElements = this.get('refreshElements');
|
136 | refreshElements.push(element);
|
137 |
|
138 |
|
139 |
|
140 | };
|
141 |
|
142 | Canvas.prototype._clearFrame = function () {
|
143 | var drawFrame = this.get('drawFrame');
|
144 | if (drawFrame) {
|
145 |
|
146 | clearAnimationFrame(drawFrame);
|
147 | this.set('drawFrame', null);
|
148 | this.set('refreshElements', []);
|
149 | }
|
150 | };
|
151 |
|
152 | Canvas.prototype.draw = function () {
|
153 | var drawFrame = this.get('drawFrame');
|
154 | if (this.get('autoDraw') && drawFrame) {
|
155 | return;
|
156 | }
|
157 | this._startDraw();
|
158 | };
|
159 |
|
160 | Canvas.prototype._drawAll = function () {
|
161 | var context = this.get('context');
|
162 | var element = this.get('el');
|
163 | var children = this.getChildren();
|
164 | context.clearRect(0, 0, element.width, element.height);
|
165 | applyAttrsToContext(context, this);
|
166 | drawChildren(context, children);
|
167 |
|
168 | this.set('refreshElements', []);
|
169 | };
|
170 |
|
171 | Canvas.prototype._drawRegion = function () {
|
172 | var context = this.get('context');
|
173 | var refreshElements = this.get('refreshElements');
|
174 | var children = this.getChildren();
|
175 | var region = this._getRefreshRegion();
|
176 |
|
177 |
|
178 | if (region) {
|
179 |
|
180 | context.clearRect(region.minX, region.minY, region.maxX - region.minX, region.maxY - region.minY);
|
181 |
|
182 | context.save();
|
183 | context.beginPath();
|
184 | context.rect(region.minX, region.minY, region.maxX - region.minX, region.maxY - region.minY);
|
185 | context.clip();
|
186 | applyAttrsToContext(context, this);
|
187 |
|
188 | checkRefresh(this, children, region);
|
189 |
|
190 | drawChildren(context, children, region);
|
191 | context.restore();
|
192 | }
|
193 | else if (refreshElements.length) {
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | clearChanged(refreshElements);
|
200 | }
|
201 | each(refreshElements, function (element) {
|
202 | if (element.get('hasChanged')) {
|
203 |
|
204 |
|
205 | element.set('hasChanged', false);
|
206 | }
|
207 | });
|
208 | this.set('refreshElements', []);
|
209 | };
|
210 |
|
211 | Canvas.prototype._startDraw = function () {
|
212 | var _this = this;
|
213 | var drawFrame = this.get('drawFrame');
|
214 | if (!drawFrame) {
|
215 | drawFrame = requestAnimationFrame(function () {
|
216 | if (_this.get('localRefresh')) {
|
217 | _this._drawRegion();
|
218 | }
|
219 | else {
|
220 | _this._drawAll();
|
221 | }
|
222 | _this.set('drawFrame', null);
|
223 | });
|
224 | this.set('drawFrame', drawFrame);
|
225 | }
|
226 | };
|
227 | Canvas.prototype.skipDraw = function () { };
|
228 | return Canvas;
|
229 | }(AbstractCanvas));
|
230 | export default Canvas;
|
231 |
|
\ | No newline at end of file |