@antv/g-canvas 2.0.51 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.esm.js +2643 -20
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +2651 -38
- package/dist/index.js.map +1 -1
- package/dist/index.umd.min.js +8 -31
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +7 -11
- package/types/index.d.ts +7 -8
- package/types/index.d.ts.map +1 -1
- package/types/plugins/canvas-renderer/CanvasRendererPlugin.d.ts +55 -0
- package/types/plugins/canvas-renderer/CanvasRendererPlugin.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/index.d.ts +17 -0
- package/types/plugins/canvas-renderer/index.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/interfaces.d.ts +5 -0
- package/types/plugins/canvas-renderer/interfaces.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/Default.d.ts +14 -0
- package/types/plugins/canvas-renderer/shapes/styles/Default.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/Image.d.ts +19 -0
- package/types/plugins/canvas-renderer/shapes/styles/Image.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/OptimizedDefault.d.ts +37 -0
- package/types/plugins/canvas-renderer/shapes/styles/OptimizedDefault.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/Text.d.ts +19 -0
- package/types/plugins/canvas-renderer/shapes/styles/Text.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/helper.d.ts +5 -0
- package/types/plugins/canvas-renderer/shapes/styles/helper.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/index.d.ts +13 -0
- package/types/plugins/canvas-renderer/shapes/styles/index.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/shapes/styles/interfaces.d.ts +8 -0
- package/types/plugins/canvas-renderer/shapes/styles/interfaces.d.ts.map +1 -0
- package/types/plugins/canvas-renderer/utils/math.d.ts +8 -0
- package/types/plugins/canvas-renderer/utils/math.d.ts.map +1 -0
- package/types/plugins/path-generator/index.d.ts +11 -0
- package/types/plugins/path-generator/index.d.ts.map +1 -0
- package/types/plugins/path-generator/interfaces.d.ts +6 -0
- package/types/plugins/path-generator/interfaces.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Circle.d.ts +3 -0
- package/types/plugins/path-generator/paths/Circle.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Ellipse.d.ts +3 -0
- package/types/plugins/path-generator/paths/Ellipse.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Line.d.ts +3 -0
- package/types/plugins/path-generator/paths/Line.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Path.d.ts +3 -0
- package/types/plugins/path-generator/paths/Path.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Polygon.d.ts +3 -0
- package/types/plugins/path-generator/paths/Polygon.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Polyline.d.ts +3 -0
- package/types/plugins/path-generator/paths/Polyline.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/Rect.d.ts +3 -0
- package/types/plugins/path-generator/paths/Rect.d.ts.map +1 -0
- package/types/plugins/path-generator/paths/index.d.ts +8 -0
- package/types/plugins/path-generator/paths/index.d.ts.map +1 -0
- package/types/plugins/picker/CanvasPickerPlugin.d.ts +23 -0
- package/types/plugins/picker/CanvasPickerPlugin.d.ts.map +1 -0
- package/types/plugins/picker/Circle.d.ts +3 -0
- package/types/plugins/picker/Circle.d.ts.map +1 -0
- package/types/plugins/picker/Ellipse.d.ts +3 -0
- package/types/plugins/picker/Ellipse.d.ts.map +1 -0
- package/types/plugins/picker/Image.d.ts +3 -0
- package/types/plugins/picker/Image.d.ts.map +1 -0
- package/types/plugins/picker/Line.d.ts +3 -0
- package/types/plugins/picker/Line.d.ts.map +1 -0
- package/types/plugins/picker/Path.d.ts +3 -0
- package/types/plugins/picker/Path.d.ts.map +1 -0
- package/types/plugins/picker/Polygon.d.ts +3 -0
- package/types/plugins/picker/Polygon.d.ts.map +1 -0
- package/types/plugins/picker/Polyline.d.ts +3 -0
- package/types/plugins/picker/Polyline.d.ts.map +1 -0
- package/types/plugins/picker/Rect.d.ts +4 -0
- package/types/plugins/picker/Rect.d.ts.map +1 -0
- package/types/plugins/picker/Text.d.ts +3 -0
- package/types/plugins/picker/Text.d.ts.map +1 -0
- package/types/plugins/picker/index.d.ts +7 -0
- package/types/plugins/picker/index.d.ts.map +1 -0
- package/types/plugins/picker/utils/math.d.ts +8 -0
- package/types/plugins/picker/utils/math.d.ts.map +1 -0
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @antv/g-canvas
|
|
3
3
|
* @description A renderer implemented by Canvas 2D API
|
|
4
|
-
* @version 2.0
|
|
5
|
-
* @date 12/
|
|
4
|
+
* @version 2.1.0
|
|
5
|
+
* @date 12/22/2025, 3:03:16 AM
|
|
6
6
|
* @author AntVis
|
|
7
7
|
* @docs https://g.antv.antgroup.com/
|
|
8
8
|
*/
|
|
@@ -13,39 +13,2643 @@ var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
|
13
13
|
var _callSuper = require('@babel/runtime/helpers/callSuper');
|
|
14
14
|
var _inherits = require('@babel/runtime/helpers/inherits');
|
|
15
15
|
var gLite = require('@antv/g-lite');
|
|
16
|
-
var
|
|
17
|
-
var
|
|
18
|
-
var
|
|
19
|
-
var
|
|
20
|
-
var
|
|
21
|
-
var
|
|
16
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
17
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
18
|
+
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
19
|
+
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
|
|
20
|
+
var _classPrivateFieldLooseBase = require('@babel/runtime/helpers/classPrivateFieldLooseBase');
|
|
21
|
+
var _classPrivateFieldLooseKey = require('@babel/runtime/helpers/classPrivateFieldLooseKey');
|
|
22
|
+
var glMatrix = require('gl-matrix');
|
|
23
|
+
var util = require('@antv/util');
|
|
24
|
+
var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
|
|
22
25
|
var _regeneratorRuntime = require('@babel/runtime/helpers/regeneratorRuntime');
|
|
23
26
|
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
|
|
24
|
-
var
|
|
27
|
+
var gMath = require('@antv/g-math');
|
|
28
|
+
|
|
29
|
+
var _renderState = /*#__PURE__*/_classPrivateFieldLooseKey("renderState");
|
|
30
|
+
/**
|
|
31
|
+
* support 2 modes in rendering:
|
|
32
|
+
* * immediate
|
|
33
|
+
* * delayed: render at the end of frame with dirty-rectangle
|
|
34
|
+
*/
|
|
35
|
+
var CanvasRendererPlugin = /*#__PURE__*/function () {
|
|
36
|
+
/**
|
|
37
|
+
* RBush used in dirty rectangle rendering
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
function CanvasRendererPlugin(canvasRendererPluginOptions // private styleRendererFactory: Record<Shape, StyleRenderer>,
|
|
41
|
+
) {
|
|
42
|
+
_classCallCheck(this, CanvasRendererPlugin);
|
|
43
|
+
this.removedRBushNodeAABBs = [];
|
|
44
|
+
this.renderQueue = [];
|
|
45
|
+
Object.defineProperty(this, _renderState, {
|
|
46
|
+
writable: true,
|
|
47
|
+
value: {
|
|
48
|
+
restoreStack: [],
|
|
49
|
+
prevObject: null,
|
|
50
|
+
currentContext: new Map()
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
this.clearFullScreenLastFrame = false;
|
|
54
|
+
this.clearFullScreen = false;
|
|
55
|
+
/**
|
|
56
|
+
* view projection matrix
|
|
57
|
+
*/
|
|
58
|
+
this.vpMatrix = glMatrix.mat4.create();
|
|
59
|
+
this.dprMatrix = glMatrix.mat4.create();
|
|
60
|
+
this.tmpMat4 = glMatrix.mat4.create();
|
|
61
|
+
this.vec3a = glMatrix.vec3.create();
|
|
62
|
+
this.vec3b = glMatrix.vec3.create();
|
|
63
|
+
this.vec3c = glMatrix.vec3.create();
|
|
64
|
+
this.vec3d = glMatrix.vec3.create();
|
|
65
|
+
this.canvasRendererPluginOptions = canvasRendererPluginOptions;
|
|
66
|
+
}
|
|
67
|
+
return _createClass(CanvasRendererPlugin, [{
|
|
68
|
+
key: "apply",
|
|
69
|
+
value: function apply(context, runtime) {
|
|
70
|
+
var _this = this;
|
|
71
|
+
this.context = context;
|
|
72
|
+
var _this$context = this.context,
|
|
73
|
+
config = _this$context.config,
|
|
74
|
+
camera = _this$context.camera,
|
|
75
|
+
renderingService = _this$context.renderingService,
|
|
76
|
+
renderingContext = _this$context.renderingContext,
|
|
77
|
+
rBushRoot = _this$context.rBushRoot,
|
|
78
|
+
pathGeneratorFactory = _this$context.pathGeneratorFactory;
|
|
79
|
+
var enableRenderingOptimization = config.renderer.getConfig().enableRenderingOptimization;
|
|
80
|
+
config.renderer.getConfig().enableDirtyCheck = false;
|
|
81
|
+
config.renderer.getConfig().enableDirtyRectangleRendering = false;
|
|
82
|
+
this.rBush = rBushRoot;
|
|
83
|
+
this.pathGeneratorFactory = pathGeneratorFactory;
|
|
84
|
+
var contextService = context.contextService;
|
|
85
|
+
var canvas = renderingContext.root.ownerDocument.defaultView;
|
|
86
|
+
var handleUnmounted = function handleUnmounted(e) {
|
|
87
|
+
var object = e.target;
|
|
88
|
+
|
|
89
|
+
// remove r-bush node
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
var rBushNode = object.rBushNode;
|
|
92
|
+
if (rBushNode !== null && rBushNode !== void 0 && rBushNode.aabb) {
|
|
93
|
+
// save removed aabbs for dirty-rectangle rendering later
|
|
94
|
+
_this.removedRBushNodeAABBs.push(rBushNode.aabb);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var handleCulled = function handleCulled(e) {
|
|
98
|
+
var object = e.target;
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
var rBushNode = object.rBushNode;
|
|
101
|
+
if (rBushNode.aabb) {
|
|
102
|
+
// save removed aabbs for dirty-rectangle rendering later
|
|
103
|
+
_this.removedRBushNodeAABBs.push(rBushNode.aabb);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
renderingService.hooks.init.tap(CanvasRendererPlugin.tag, function () {
|
|
107
|
+
canvas.addEventListener(gLite.ElementEvent.UNMOUNTED, handleUnmounted);
|
|
108
|
+
canvas.addEventListener(gLite.ElementEvent.CULLED, handleCulled);
|
|
109
|
+
|
|
110
|
+
// clear fullscreen
|
|
111
|
+
var dpr = contextService.getDPR();
|
|
112
|
+
var width = config.width,
|
|
113
|
+
height = config.height;
|
|
114
|
+
var context = contextService.getContext();
|
|
115
|
+
_this.clearRect(context, 0, 0, width * dpr, height * dpr, config.background);
|
|
116
|
+
});
|
|
117
|
+
renderingService.hooks.destroy.tap(CanvasRendererPlugin.tag, function () {
|
|
118
|
+
canvas.removeEventListener(gLite.ElementEvent.UNMOUNTED, handleUnmounted);
|
|
119
|
+
canvas.removeEventListener(gLite.ElementEvent.CULLED, handleCulled);
|
|
120
|
+
_this.renderQueue = [];
|
|
121
|
+
_this.removedRBushNodeAABBs = [];
|
|
122
|
+
_classPrivateFieldLooseBase(_this, _renderState)[_renderState] = {
|
|
123
|
+
restoreStack: [],
|
|
124
|
+
prevObject: null,
|
|
125
|
+
currentContext: null
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
var beforeDraw = function beforeDraw() {
|
|
129
|
+
var _canvas$context$rende;
|
|
130
|
+
var context = contextService.getContext();
|
|
131
|
+
var dpr = contextService.getDPR();
|
|
132
|
+
var width = config.width,
|
|
133
|
+
height = config.height;
|
|
134
|
+
var _this$canvasRendererP = _this.canvasRendererPluginOptions,
|
|
135
|
+
dirtyObjectNumThreshold = _this$canvasRendererP.dirtyObjectNumThreshold,
|
|
136
|
+
dirtyObjectRatioThreshold = _this$canvasRendererP.dirtyObjectRatioThreshold;
|
|
137
|
+
|
|
138
|
+
// some heuristic conditions such as 80% object changed
|
|
139
|
+
var _renderingService$get = renderingService.getStats(),
|
|
140
|
+
total = _renderingService$get.total,
|
|
141
|
+
rendered = _renderingService$get.rendered;
|
|
142
|
+
var ratio = rendered / total;
|
|
143
|
+
_this.clearFullScreen = _this.clearFullScreenLastFrame ||
|
|
144
|
+
// @ts-ignore
|
|
145
|
+
!((_canvas$context$rende = canvas.context.renderingPlugins[1]) !== null && _canvas$context$rende !== void 0 && _canvas$context$rende.isFirstTimeRenderingFinished) || renderingService.disableDirtyRectangleRendering() || rendered > dirtyObjectNumThreshold && ratio > dirtyObjectRatioThreshold;
|
|
146
|
+
if (!context) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (typeof context.resetTransform === 'function') {
|
|
150
|
+
context.resetTransform();
|
|
151
|
+
} else {
|
|
152
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
153
|
+
}
|
|
154
|
+
if (_this.clearFullScreen) {
|
|
155
|
+
_this.clearRect(context, 0, 0, width * dpr, height * dpr, config.background);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* render objects by z-index
|
|
161
|
+
*
|
|
162
|
+
* - The level of the child node will be affected by the level of the parent node
|
|
163
|
+
*/
|
|
164
|
+
var renderByZIndex = function renderByZIndex(object, context) {
|
|
165
|
+
var stack = [object];
|
|
166
|
+
while (stack.length > 0) {
|
|
167
|
+
var _currentObject$sortab;
|
|
168
|
+
var currentObject = stack.pop();
|
|
169
|
+
if (currentObject.isVisible() && !currentObject.isCulled()) {
|
|
170
|
+
if (enableRenderingOptimization) {
|
|
171
|
+
_this.renderDisplayObjectOptimized(currentObject, context, _this.context, _classPrivateFieldLooseBase(_this, _renderState)[_renderState], runtime);
|
|
172
|
+
} else {
|
|
173
|
+
_this.renderDisplayObject(currentObject, context, _this.context, _classPrivateFieldLooseBase(_this, _renderState)[_renderState], runtime);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
var objects = ((_currentObject$sortab = currentObject.sortable) === null || _currentObject$sortab === void 0 || (_currentObject$sortab = _currentObject$sortab.sorted) === null || _currentObject$sortab === void 0 ? void 0 : _currentObject$sortab.length) > 0 ? currentObject.sortable.sorted : currentObject.childNodes;
|
|
177
|
+
// should account for z-index
|
|
178
|
+
for (var i = objects.length - 1; i >= 0; i--) {
|
|
179
|
+
stack.push(objects[i]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
// render at the end of frame
|
|
185
|
+
renderingService.hooks.endFrame.tap(CanvasRendererPlugin.tag, function () {
|
|
186
|
+
beforeDraw();
|
|
187
|
+
|
|
188
|
+
// Skip rendering.
|
|
189
|
+
if (renderingContext.root.childNodes.length === 0) {
|
|
190
|
+
_this.clearFullScreenLastFrame = true;
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
enableRenderingOptimization = config.renderer.getConfig().enableRenderingOptimization;
|
|
194
|
+
|
|
195
|
+
// init
|
|
196
|
+
_classPrivateFieldLooseBase(_this, _renderState)[_renderState] = {
|
|
197
|
+
restoreStack: [],
|
|
198
|
+
prevObject: null,
|
|
199
|
+
currentContext: _classPrivateFieldLooseBase(_this, _renderState)[_renderState].currentContext
|
|
200
|
+
};
|
|
201
|
+
_classPrivateFieldLooseBase(_this, _renderState)[_renderState].currentContext.clear();
|
|
202
|
+
_this.clearFullScreenLastFrame = false;
|
|
203
|
+
var context = contextService.getContext();
|
|
204
|
+
// clear & clip dirty rectangle
|
|
205
|
+
var dpr = contextService.getDPR();
|
|
206
|
+
glMatrix.mat4.fromScaling(_this.dprMatrix, [dpr, dpr, 1]);
|
|
207
|
+
glMatrix.mat4.multiply(_this.vpMatrix, _this.dprMatrix, camera.getOrthoMatrix());
|
|
208
|
+
if (_this.clearFullScreen) {
|
|
209
|
+
// console.time('renderByZIndex');
|
|
210
|
+
if (enableRenderingOptimization) {
|
|
211
|
+
context.save();
|
|
212
|
+
renderByZIndex(renderingContext.root, context);
|
|
213
|
+
context.restore();
|
|
214
|
+
} else {
|
|
215
|
+
renderByZIndex(renderingContext.root, context);
|
|
216
|
+
}
|
|
217
|
+
// console.timeEnd('renderByZIndex');
|
|
218
|
+
|
|
219
|
+
_this.removedRBushNodeAABBs = [];
|
|
220
|
+
} else {
|
|
221
|
+
// console.log('canvas renderer next...', this.renderQueue);
|
|
222
|
+
// merge removed AABB
|
|
223
|
+
var dirtyRenderBounds = _this.safeMergeAABB.apply(_this, [_this.mergeDirtyAABBs(_this.renderQueue)].concat(_toConsumableArray(_this.removedRBushNodeAABBs.map(function (_ref) {
|
|
224
|
+
var minX = _ref.minX,
|
|
225
|
+
minY = _ref.minY,
|
|
226
|
+
maxX = _ref.maxX,
|
|
227
|
+
maxY = _ref.maxY;
|
|
228
|
+
var aabb = new gLite.AABB();
|
|
229
|
+
aabb.setMinMax(
|
|
230
|
+
// vec3.fromValues(minX, minY, 0),
|
|
231
|
+
// vec3.fromValues(maxX, maxY, 0),
|
|
232
|
+
[minX, minY, 0], [maxX, maxY, 0]);
|
|
233
|
+
return aabb;
|
|
234
|
+
}))));
|
|
235
|
+
_this.removedRBushNodeAABBs = [];
|
|
236
|
+
if (gLite.AABB.isEmpty(dirtyRenderBounds)) {
|
|
237
|
+
_this.renderQueue = [];
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
var dirtyRect = _this.convertAABB2Rect(dirtyRenderBounds);
|
|
241
|
+
var x = dirtyRect.x,
|
|
242
|
+
y = dirtyRect.y,
|
|
243
|
+
width = dirtyRect.width,
|
|
244
|
+
height = dirtyRect.height;
|
|
245
|
+
var tl = glMatrix.vec3.transformMat4(_this.vec3a, [x, y, 0], _this.vpMatrix);
|
|
246
|
+
var tr = glMatrix.vec3.transformMat4(_this.vec3b, [x + width, y, 0], _this.vpMatrix);
|
|
247
|
+
var bl = glMatrix.vec3.transformMat4(_this.vec3c, [x, y + height, 0], _this.vpMatrix);
|
|
248
|
+
var br = glMatrix.vec3.transformMat4(_this.vec3d, [x + width, y + height, 0], _this.vpMatrix);
|
|
249
|
+
var minx = Math.min(tl[0], tr[0], br[0], bl[0]);
|
|
250
|
+
var miny = Math.min(tl[1], tr[1], br[1], bl[1]);
|
|
251
|
+
var maxx = Math.max(tl[0], tr[0], br[0], bl[0]);
|
|
252
|
+
var maxy = Math.max(tl[1], tr[1], br[1], bl[1]);
|
|
253
|
+
var ix = Math.floor(minx);
|
|
254
|
+
var iy = Math.floor(miny);
|
|
255
|
+
var iwidth = Math.ceil(maxx - minx);
|
|
256
|
+
var iheight = Math.ceil(maxy - miny);
|
|
257
|
+
context.save();
|
|
258
|
+
_this.clearRect(context, ix, iy, iwidth, iheight, config.background);
|
|
259
|
+
context.beginPath();
|
|
260
|
+
context.rect(ix, iy, iwidth, iheight);
|
|
261
|
+
context.clip();
|
|
262
|
+
|
|
263
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations
|
|
264
|
+
context.setTransform(_this.vpMatrix[0], _this.vpMatrix[1], _this.vpMatrix[4], _this.vpMatrix[5], _this.vpMatrix[12], _this.vpMatrix[13]);
|
|
265
|
+
|
|
266
|
+
// draw dirty rectangle
|
|
267
|
+
var _config$renderer$getC = config.renderer.getConfig(),
|
|
268
|
+
enableDirtyRectangleRenderingDebug = _config$renderer$getC.enableDirtyRectangleRenderingDebug;
|
|
269
|
+
if (enableDirtyRectangleRenderingDebug) {
|
|
270
|
+
canvas.dispatchEvent(new gLite.CustomEvent(gLite.CanvasEvent.DIRTY_RECTANGLE, {
|
|
271
|
+
dirtyRect: {
|
|
272
|
+
x: ix,
|
|
273
|
+
y: iy,
|
|
274
|
+
width: iwidth,
|
|
275
|
+
height: iheight
|
|
276
|
+
}
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// search objects intersect with dirty rectangle
|
|
281
|
+
var dirtyObjects = _this.searchDirtyObjects(dirtyRenderBounds);
|
|
282
|
+
|
|
283
|
+
// do rendering
|
|
284
|
+
dirtyObjects
|
|
285
|
+
// sort by z-index
|
|
286
|
+
.sort(function (a, b) {
|
|
287
|
+
return a.sortable.renderOrder - b.sortable.renderOrder;
|
|
288
|
+
}).forEach(function (object) {
|
|
289
|
+
// culled object should not be rendered
|
|
290
|
+
if (object && object.isVisible() && !object.isCulled()) {
|
|
291
|
+
_this.renderDisplayObject(object, context, _this.context, _classPrivateFieldLooseBase(_this, _renderState)[_renderState], runtime);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
context.restore();
|
|
295
|
+
|
|
296
|
+
// save dirty AABBs in last frame
|
|
297
|
+
_this.renderQueue.forEach(function (object) {
|
|
298
|
+
_this.saveDirtyAABB(object);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// clear queue
|
|
302
|
+
_this.renderQueue = [];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// pop restore stack, eg. root -> parent -> child
|
|
306
|
+
_classPrivateFieldLooseBase(_this, _renderState)[_renderState].restoreStack.forEach(function () {
|
|
307
|
+
context.restore();
|
|
308
|
+
});
|
|
309
|
+
// clear restore stack
|
|
310
|
+
_classPrivateFieldLooseBase(_this, _renderState)[_renderState].restoreStack = [];
|
|
311
|
+
});
|
|
312
|
+
renderingService.hooks.render.tap(CanvasRendererPlugin.tag, function (object) {
|
|
313
|
+
if (!_this.clearFullScreen) {
|
|
314
|
+
// render at the end of frame
|
|
315
|
+
_this.renderQueue.push(object);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}, {
|
|
320
|
+
key: "clearRect",
|
|
321
|
+
value: function clearRect(context, x, y, width, height, background) {
|
|
322
|
+
// clearRect is faster than fillRect @see https://stackoverflow.com/a/30830253
|
|
323
|
+
context.clearRect(x, y, width, height);
|
|
324
|
+
if (background) {
|
|
325
|
+
context.fillStyle = background;
|
|
326
|
+
context.fillRect(x, y, width, height);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}, {
|
|
330
|
+
key: "renderDisplayObjectOptimized",
|
|
331
|
+
value: function renderDisplayObjectOptimized(object, context, canvasContext, renderState, runtime) {
|
|
332
|
+
var nodeName = object.nodeName;
|
|
333
|
+
var updateTransform = false;
|
|
334
|
+
var clipDraw = false;
|
|
335
|
+
|
|
336
|
+
// @ts-ignore
|
|
337
|
+
var styleRenderer = this.context.styleRendererFactory[nodeName];
|
|
338
|
+
var generatePath = this.pathGeneratorFactory[nodeName];
|
|
339
|
+
|
|
340
|
+
// clip path
|
|
341
|
+
var clipPath = object.parsedStyle.clipPath;
|
|
342
|
+
if (clipPath) {
|
|
343
|
+
updateTransform = !renderState.prevObject || !glMatrix.mat4.exactEquals(clipPath.getWorldTransform(), renderState.prevObject.getWorldTransform());
|
|
344
|
+
if (updateTransform) {
|
|
345
|
+
this.applyWorldTransform(context, clipPath);
|
|
346
|
+
renderState.prevObject = null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// generate path in local space
|
|
350
|
+
var _generatePath = this.pathGeneratorFactory[clipPath.nodeName];
|
|
351
|
+
if (_generatePath) {
|
|
352
|
+
context.save();
|
|
353
|
+
clipDraw = true;
|
|
354
|
+
context.beginPath();
|
|
355
|
+
_generatePath(context, clipPath.parsedStyle);
|
|
356
|
+
context.closePath();
|
|
357
|
+
context.clip();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// fill & stroke
|
|
362
|
+
|
|
363
|
+
if (styleRenderer) {
|
|
364
|
+
updateTransform = !renderState.prevObject || !glMatrix.mat4.exactEquals(object.getWorldTransform(), renderState.prevObject.getWorldTransform());
|
|
365
|
+
if (updateTransform) {
|
|
366
|
+
this.applyWorldTransform(context, object);
|
|
367
|
+
}
|
|
368
|
+
var forceUpdateStyle = !renderState.prevObject;
|
|
369
|
+
if (!forceUpdateStyle) {
|
|
370
|
+
var prevNodeName = renderState.prevObject.nodeName;
|
|
371
|
+
if (nodeName === gLite.Shape.TEXT) {
|
|
372
|
+
forceUpdateStyle = prevNodeName !== gLite.Shape.TEXT;
|
|
373
|
+
} else if (nodeName === gLite.Shape.IMAGE) {
|
|
374
|
+
forceUpdateStyle = prevNodeName !== gLite.Shape.IMAGE;
|
|
375
|
+
} else {
|
|
376
|
+
forceUpdateStyle = prevNodeName === gLite.Shape.TEXT || prevNodeName === gLite.Shape.IMAGE;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
styleRenderer.applyStyleToContext(context, object, forceUpdateStyle, renderState);
|
|
380
|
+
renderState.prevObject = object;
|
|
381
|
+
}
|
|
382
|
+
if (generatePath) {
|
|
383
|
+
context.beginPath();
|
|
384
|
+
generatePath(context, object.parsedStyle);
|
|
385
|
+
if (nodeName !== gLite.Shape.LINE && nodeName !== gLite.Shape.PATH && nodeName !== gLite.Shape.POLYLINE) {
|
|
386
|
+
context.closePath();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// fill & stroke
|
|
391
|
+
if (styleRenderer) {
|
|
392
|
+
styleRenderer.drawToContext(context, object, _classPrivateFieldLooseBase(this, _renderState)[_renderState], this, runtime);
|
|
393
|
+
}
|
|
394
|
+
if (clipDraw) {
|
|
395
|
+
context.restore();
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// finish rendering, clear dirty flag
|
|
399
|
+
object.dirty(false);
|
|
400
|
+
}
|
|
401
|
+
}, {
|
|
402
|
+
key: "renderDisplayObject",
|
|
403
|
+
value: function renderDisplayObject(object, context, canvasContext, renderState, runtime) {
|
|
404
|
+
var nodeName = object.nodeName;
|
|
405
|
+
|
|
406
|
+
// restore to its ancestor
|
|
407
|
+
|
|
408
|
+
var parent = renderState.restoreStack[renderState.restoreStack.length - 1];
|
|
409
|
+
if (parent && !(object.compareDocumentPosition(parent) & gLite.Node.DOCUMENT_POSITION_CONTAINS)) {
|
|
410
|
+
context.restore();
|
|
411
|
+
renderState.restoreStack.pop();
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// @ts-ignore
|
|
415
|
+
var styleRenderer = this.context.styleRendererFactory[nodeName];
|
|
416
|
+
var generatePath = this.pathGeneratorFactory[nodeName];
|
|
417
|
+
|
|
418
|
+
// clip path
|
|
419
|
+
var clipPath = object.parsedStyle.clipPath;
|
|
420
|
+
if (clipPath) {
|
|
421
|
+
this.applyWorldTransform(context, clipPath);
|
|
422
|
+
|
|
423
|
+
// generate path in local space
|
|
424
|
+
var _generatePath2 = this.pathGeneratorFactory[clipPath.nodeName];
|
|
425
|
+
if (_generatePath2) {
|
|
426
|
+
context.save();
|
|
427
|
+
|
|
428
|
+
// save clip
|
|
429
|
+
renderState.restoreStack.push(object);
|
|
430
|
+
context.beginPath();
|
|
431
|
+
_generatePath2(context, clipPath.parsedStyle);
|
|
432
|
+
context.closePath();
|
|
433
|
+
context.clip();
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// fill & stroke
|
|
438
|
+
|
|
439
|
+
if (styleRenderer) {
|
|
440
|
+
this.applyWorldTransform(context, object);
|
|
441
|
+
context.save();
|
|
442
|
+
|
|
443
|
+
// apply attributes to context
|
|
444
|
+
this.applyAttributesToContext(context, object);
|
|
445
|
+
}
|
|
446
|
+
if (generatePath) {
|
|
447
|
+
context.beginPath();
|
|
448
|
+
generatePath(context, object.parsedStyle);
|
|
449
|
+
if (nodeName !== gLite.Shape.LINE && nodeName !== gLite.Shape.PATH && nodeName !== gLite.Shape.POLYLINE) {
|
|
450
|
+
context.closePath();
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// fill & stroke
|
|
455
|
+
if (styleRenderer) {
|
|
456
|
+
styleRenderer.render(context, object.parsedStyle, object, canvasContext, this, runtime);
|
|
457
|
+
|
|
458
|
+
// restore applied attributes, eg. shadowBlur shadowColor...
|
|
459
|
+
context.restore();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// finish rendering, clear dirty flag
|
|
463
|
+
object.dirty(false);
|
|
464
|
+
}
|
|
465
|
+
}, {
|
|
466
|
+
key: "applyAttributesToContext",
|
|
467
|
+
value: function applyAttributesToContext(context, object) {
|
|
468
|
+
var _ref2 = object.parsedStyle,
|
|
469
|
+
stroke = _ref2.stroke,
|
|
470
|
+
fill = _ref2.fill,
|
|
471
|
+
opacity = _ref2.opacity,
|
|
472
|
+
lineDash = _ref2.lineDash,
|
|
473
|
+
lineDashOffset = _ref2.lineDashOffset;
|
|
474
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/setLineDash
|
|
475
|
+
if (lineDash) {
|
|
476
|
+
context.setLineDash(lineDash);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
|
|
480
|
+
if (!util.isNil(lineDashOffset)) {
|
|
481
|
+
context.lineDashOffset = lineDashOffset;
|
|
482
|
+
}
|
|
483
|
+
if (!util.isNil(opacity)) {
|
|
484
|
+
context.globalAlpha *= opacity;
|
|
485
|
+
}
|
|
486
|
+
if (!util.isNil(stroke) && !Array.isArray(stroke) && !stroke.isNone) {
|
|
487
|
+
context.strokeStyle = object.attributes.stroke;
|
|
488
|
+
}
|
|
489
|
+
if (!util.isNil(fill) && !Array.isArray(fill) && !fill.isNone) {
|
|
490
|
+
context.fillStyle = object.attributes.fill;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}, {
|
|
494
|
+
key: "convertAABB2Rect",
|
|
495
|
+
value: function convertAABB2Rect(aabb) {
|
|
496
|
+
var min = aabb.getMin();
|
|
497
|
+
var max = aabb.getMax();
|
|
498
|
+
// expand the rectangle a bit to avoid artifacts
|
|
499
|
+
// @see https://www.yuque.com/antv/ou292n/bi8nix#ExvCu
|
|
500
|
+
var minX = Math.floor(min[0]);
|
|
501
|
+
var minY = Math.floor(min[1]);
|
|
502
|
+
var maxX = Math.ceil(max[0]);
|
|
503
|
+
var maxY = Math.ceil(max[1]);
|
|
504
|
+
var width = maxX - minX;
|
|
505
|
+
var height = maxY - minY;
|
|
506
|
+
return {
|
|
507
|
+
x: minX,
|
|
508
|
+
y: minY,
|
|
509
|
+
width: width,
|
|
510
|
+
height: height
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* TODO: merge dirty rectangles with some strategies.
|
|
516
|
+
* For now, we just simply merge all the rectangles into one.
|
|
517
|
+
* @see https://idom.me/articles/841.html
|
|
518
|
+
*/
|
|
519
|
+
}, {
|
|
520
|
+
key: "mergeDirtyAABBs",
|
|
521
|
+
value: function mergeDirtyAABBs(dirtyObjects) {
|
|
522
|
+
// merge into a big AABB
|
|
523
|
+
// TODO: skip descendant if ancestor is caculated, but compareNodePosition is really slow
|
|
524
|
+
var aabb = new gLite.AABB();
|
|
525
|
+
dirtyObjects.forEach(function (object) {
|
|
526
|
+
var renderBounds = object.getRenderBounds();
|
|
527
|
+
aabb.add(renderBounds);
|
|
528
|
+
var dirtyRenderBounds = object.renderable.dirtyRenderBounds;
|
|
529
|
+
if (dirtyRenderBounds) {
|
|
530
|
+
aabb.add(dirtyRenderBounds);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
return aabb;
|
|
534
|
+
}
|
|
535
|
+
}, {
|
|
536
|
+
key: "searchDirtyObjects",
|
|
537
|
+
value: function searchDirtyObjects(dirtyRectangle) {
|
|
538
|
+
// search in r-tree, get all affected nodes
|
|
539
|
+
var _dirtyRectangle$getMi = dirtyRectangle.getMin(),
|
|
540
|
+
_dirtyRectangle$getMi2 = _slicedToArray(_dirtyRectangle$getMi, 2),
|
|
541
|
+
minX = _dirtyRectangle$getMi2[0],
|
|
542
|
+
minY = _dirtyRectangle$getMi2[1];
|
|
543
|
+
var _dirtyRectangle$getMa = dirtyRectangle.getMax(),
|
|
544
|
+
_dirtyRectangle$getMa2 = _slicedToArray(_dirtyRectangle$getMa, 2),
|
|
545
|
+
maxX = _dirtyRectangle$getMa2[0],
|
|
546
|
+
maxY = _dirtyRectangle$getMa2[1];
|
|
547
|
+
var rBushNodes = this.rBush.search({
|
|
548
|
+
minX: minX,
|
|
549
|
+
minY: minY,
|
|
550
|
+
maxX: maxX,
|
|
551
|
+
maxY: maxY
|
|
552
|
+
});
|
|
553
|
+
return rBushNodes.map(function (_ref3) {
|
|
554
|
+
var displayObject = _ref3.displayObject;
|
|
555
|
+
return displayObject;
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
}, {
|
|
559
|
+
key: "saveDirtyAABB",
|
|
560
|
+
value: function saveDirtyAABB(object) {
|
|
561
|
+
var renderable = object.renderable;
|
|
562
|
+
if (!renderable.dirtyRenderBounds) {
|
|
563
|
+
renderable.dirtyRenderBounds = new gLite.AABB();
|
|
564
|
+
}
|
|
565
|
+
var renderBounds = object.getRenderBounds();
|
|
566
|
+
if (renderBounds) {
|
|
567
|
+
// save last dirty aabb
|
|
568
|
+
renderable.dirtyRenderBounds.update(renderBounds.center, renderBounds.halfExtents);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}, {
|
|
572
|
+
key: "applyWorldTransform",
|
|
573
|
+
value: function applyWorldTransform(context, object, matrix) {
|
|
574
|
+
// apply clip shape's RTS
|
|
575
|
+
if (matrix) {
|
|
576
|
+
glMatrix.mat4.copy(this.tmpMat4, object.getLocalTransform());
|
|
577
|
+
glMatrix.mat4.multiply(this.tmpMat4, matrix, this.tmpMat4);
|
|
578
|
+
glMatrix.mat4.multiply(this.tmpMat4, this.vpMatrix, this.tmpMat4);
|
|
579
|
+
} else {
|
|
580
|
+
// apply RTS transformation in world space
|
|
581
|
+
glMatrix.mat4.copy(this.tmpMat4, object.getWorldTransform());
|
|
582
|
+
glMatrix.mat4.multiply(this.tmpMat4, this.vpMatrix, this.tmpMat4);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations
|
|
586
|
+
context.setTransform(this.tmpMat4[0], this.tmpMat4[1], this.tmpMat4[4], this.tmpMat4[5], this.tmpMat4[12], this.tmpMat4[13]);
|
|
587
|
+
}
|
|
588
|
+
}, {
|
|
589
|
+
key: "safeMergeAABB",
|
|
590
|
+
value: function safeMergeAABB() {
|
|
591
|
+
var merged = new gLite.AABB();
|
|
592
|
+
for (var _len = arguments.length, aabbs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
593
|
+
aabbs[_key] = arguments[_key];
|
|
594
|
+
}
|
|
595
|
+
aabbs.forEach(function (aabb) {
|
|
596
|
+
merged.add(aabb);
|
|
597
|
+
});
|
|
598
|
+
return merged;
|
|
599
|
+
}
|
|
600
|
+
}]);
|
|
601
|
+
}();
|
|
602
|
+
CanvasRendererPlugin.tag = 'CanvasRenderer';
|
|
603
|
+
|
|
604
|
+
function getPattern(pattern, object, context, canvasContext, plugin, runtime, imagePool) {
|
|
605
|
+
var $offscreenCanvas;
|
|
606
|
+
var dpr;
|
|
607
|
+
if (pattern.image.nodeName === 'rect') {
|
|
608
|
+
var _parsedStyle = pattern.image.parsedStyle,
|
|
609
|
+
width = _parsedStyle.width,
|
|
610
|
+
height = _parsedStyle.height;
|
|
611
|
+
dpr = canvasContext.contextService.getDPR();
|
|
612
|
+
var offscreenCanvas = canvasContext.config.offscreenCanvas;
|
|
613
|
+
$offscreenCanvas = runtime.offscreenCanvasCreator.getOrCreateCanvas(offscreenCanvas);
|
|
614
|
+
$offscreenCanvas.width = width * dpr;
|
|
615
|
+
$offscreenCanvas.height = height * dpr;
|
|
616
|
+
var offscreenCanvasContext = runtime.offscreenCanvasCreator.getOrCreateContext(offscreenCanvas);
|
|
617
|
+
var renderState = {
|
|
618
|
+
restoreStack: [],
|
|
619
|
+
prevObject: null,
|
|
620
|
+
currentContext: new Map()
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
// offscreenCanvasContext.scale(1 / dpr, 1 / dpr);
|
|
624
|
+
|
|
625
|
+
pattern.image.forEach(function (object) {
|
|
626
|
+
plugin.renderDisplayObject(object, offscreenCanvasContext, canvasContext, renderState, runtime);
|
|
627
|
+
});
|
|
628
|
+
renderState.restoreStack.forEach(function () {
|
|
629
|
+
offscreenCanvasContext.restore();
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
var canvasPattern = imagePool.getOrCreatePatternSync(object, pattern, context, $offscreenCanvas, dpr, object.getGeometryBounds().min, function () {
|
|
633
|
+
// set dirty rectangle flag
|
|
634
|
+
object.dirty();
|
|
635
|
+
canvasContext.renderingService.dirty();
|
|
636
|
+
});
|
|
637
|
+
return canvasPattern;
|
|
638
|
+
}
|
|
639
|
+
function getColor(parsedColor, object, context, imagePool) {
|
|
640
|
+
var color;
|
|
641
|
+
if (parsedColor.type === gLite.GradientType.LinearGradient || parsedColor.type === gLite.GradientType.RadialGradient) {
|
|
642
|
+
var bounds = object.getGeometryBounds();
|
|
643
|
+
var width = bounds && bounds.halfExtents[0] * 2 || 1;
|
|
644
|
+
var height = bounds && bounds.halfExtents[1] * 2 || 1;
|
|
645
|
+
var min = bounds && bounds.min || [0, 0];
|
|
646
|
+
color = imagePool.getOrCreateGradient(_objectSpread(_objectSpread({
|
|
647
|
+
type: parsedColor.type
|
|
648
|
+
}, parsedColor.value), {}, {
|
|
649
|
+
min: min,
|
|
650
|
+
width: width,
|
|
651
|
+
height: height
|
|
652
|
+
}), context);
|
|
653
|
+
}
|
|
654
|
+
return color;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
var SHADOW_NUMBER_STYLE = ['shadowBlur', 'shadowOffsetX', 'shadowOffsetY'];
|
|
658
|
+
var STROKE_STYLE = ['lineCap', 'lineJoin', 'miterLimit'];
|
|
659
|
+
var DEFAULT_STYLE = {
|
|
660
|
+
// common
|
|
661
|
+
globalAlpha: 1,
|
|
662
|
+
shadowBlur: 0,
|
|
663
|
+
shadowOffsetX: 0,
|
|
664
|
+
shadowOffsetY: 0,
|
|
665
|
+
shadowColor: '#000',
|
|
666
|
+
filter: 'none',
|
|
667
|
+
globalCompositeOperation: 'source-over',
|
|
668
|
+
// stroke/fill
|
|
669
|
+
strokeStyle: '#000',
|
|
670
|
+
strokeOpacity: 1,
|
|
671
|
+
lineWidth: 1,
|
|
672
|
+
lineDash: [],
|
|
673
|
+
lineDashOffset: 0,
|
|
674
|
+
lineCap: 'butt',
|
|
675
|
+
lineJoin: 'miter',
|
|
676
|
+
miterLimit: 10,
|
|
677
|
+
fillStyle: '#000',
|
|
678
|
+
fillOpacity: 1
|
|
679
|
+
|
|
680
|
+
// image
|
|
681
|
+
};
|
|
682
|
+
var defaultParsedStyle = {};
|
|
25
683
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
684
|
+
/**
|
|
685
|
+
* Updating the canvas context is an expensive operation. The state of the context is cached and the actual update operation is performed only when the cache is not hit.
|
|
686
|
+
*
|
|
687
|
+
* In any case, the previous value is returned, which is convenient for temporarily updating the context and restoring it later.
|
|
688
|
+
*/
|
|
689
|
+
function updateContextIfNotHitCache(context, key, value, cache) {
|
|
690
|
+
var prevValue = cache.has(key) ? cache.get(key) : DEFAULT_STYLE[key];
|
|
691
|
+
if (prevValue !== value) {
|
|
692
|
+
// console.log('not hit cache', key, value, prevValue, cache);
|
|
693
|
+
if (key === 'lineDash') {
|
|
694
|
+
context.setLineDash(value);
|
|
695
|
+
} else {
|
|
696
|
+
// @ts-ignore
|
|
697
|
+
context[key] = value;
|
|
698
|
+
}
|
|
699
|
+
cache.set(key, value);
|
|
700
|
+
}
|
|
701
|
+
return prevValue;
|
|
702
|
+
}
|
|
703
|
+
var OptimizedDefaultRenderer = /*#__PURE__*/function () {
|
|
704
|
+
function OptimizedDefaultRenderer(imagePool) {
|
|
705
|
+
_classCallCheck(this, OptimizedDefaultRenderer);
|
|
706
|
+
this.imagePool = imagePool;
|
|
707
|
+
}
|
|
708
|
+
return _createClass(OptimizedDefaultRenderer, [{
|
|
709
|
+
key: "applyAttributesToContext",
|
|
710
|
+
value: function applyAttributesToContext(context, object) {}
|
|
711
|
+
}, {
|
|
712
|
+
key: "render",
|
|
713
|
+
value: function render(context, parsedStyle, object, canvasContext, plugin, runtime) {}
|
|
714
|
+
|
|
715
|
+
// #region common style
|
|
716
|
+
}, {
|
|
717
|
+
key: "applyCommonStyleToContext",
|
|
718
|
+
value: function applyCommonStyleToContext(context, object, forceUpdate, renderState) {
|
|
719
|
+
// const dpr = object.ownerDocument.defaultView.getContextService().getDPR();
|
|
720
|
+
var prevStyle = forceUpdate ? defaultParsedStyle : renderState.prevObject.parsedStyle;
|
|
721
|
+
var style = object.parsedStyle;
|
|
722
|
+
if (forceUpdate || style.opacity !== prevStyle.opacity) {
|
|
723
|
+
updateContextIfNotHitCache(context, 'globalAlpha', !util.isNil(style.opacity) ? style.opacity : DEFAULT_STYLE.globalAlpha, renderState.currentContext);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// TODO blend prop
|
|
727
|
+
// @ts-ignore
|
|
728
|
+
if (forceUpdate || style.blend !== prevStyle.blend) {
|
|
729
|
+
updateContextIfNotHitCache(context, 'globalCompositeOperation',
|
|
730
|
+
// @ts-ignore
|
|
731
|
+
!util.isNil(style.blend) ?
|
|
732
|
+
// @ts-ignore
|
|
733
|
+
style.blend : DEFAULT_STYLE.globalCompositeOperation, renderState.currentContext);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
// #endregion common style
|
|
737
|
+
|
|
738
|
+
// #region stroke/fill style
|
|
739
|
+
}, {
|
|
740
|
+
key: "applyStrokeFillStyleToContext",
|
|
741
|
+
value: function applyStrokeFillStyleToContext(context, object, forceUpdate, renderState) {
|
|
742
|
+
var prevStyle = forceUpdate ? defaultParsedStyle : renderState.prevObject.parsedStyle;
|
|
743
|
+
var style = object.parsedStyle;
|
|
744
|
+
var _style$lineWidth = style.lineWidth,
|
|
745
|
+
lineWidth = _style$lineWidth === void 0 ? DEFAULT_STYLE.lineWidth : _style$lineWidth;
|
|
746
|
+
var hasFill = style.fill && !style.fill.isNone;
|
|
747
|
+
var hasStroke = style.stroke && !style.stroke.isNone && lineWidth > 0;
|
|
748
|
+
if (hasStroke) {
|
|
749
|
+
if (forceUpdate || object.attributes.stroke !== renderState.prevObject.attributes.stroke) {
|
|
750
|
+
var value = !util.isNil(style.stroke) && !Array.isArray(style.stroke) && !style.stroke.isNone ? object.attributes.stroke : DEFAULT_STYLE.strokeStyle;
|
|
751
|
+
updateContextIfNotHitCache(context, 'strokeStyle', value, renderState.currentContext);
|
|
752
|
+
}
|
|
753
|
+
if (forceUpdate || style.lineWidth !== prevStyle.lineWidth) {
|
|
754
|
+
updateContextIfNotHitCache(context, 'lineWidth', !util.isNil(style.lineWidth) ? style.lineWidth : DEFAULT_STYLE.lineWidth, renderState.currentContext);
|
|
755
|
+
}
|
|
756
|
+
if (forceUpdate || style.lineDash !== prevStyle.lineDash) {
|
|
757
|
+
updateContextIfNotHitCache(context, 'lineDash', style.lineDash || DEFAULT_STYLE.lineDash, renderState.currentContext);
|
|
758
|
+
}
|
|
759
|
+
if (forceUpdate || style.lineDashOffset !== prevStyle.lineDashOffset) {
|
|
760
|
+
updateContextIfNotHitCache(context, 'lineDashOffset', !util.isNil(style.lineDashOffset) ? style.lineDashOffset : DEFAULT_STYLE.lineDashOffset, renderState.currentContext);
|
|
761
|
+
}
|
|
762
|
+
for (var i = 0; i < STROKE_STYLE.length; i++) {
|
|
763
|
+
var styleName = STROKE_STYLE[i];
|
|
764
|
+
if (forceUpdate || style[styleName] !== prevStyle[styleName]) {
|
|
765
|
+
updateContextIfNotHitCache(context, styleName, !util.isNil(style[styleName]) ? style[styleName] : DEFAULT_STYLE[styleName], renderState.currentContext);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
if (hasFill && (forceUpdate || object.attributes.fill !== renderState.prevObject.attributes.fill)) {
|
|
770
|
+
var _value = !util.isNil(style.fill) && !Array.isArray(style.fill) && !style.fill.isNone ? object.attributes.fill : DEFAULT_STYLE.fillStyle;
|
|
771
|
+
updateContextIfNotHitCache(context, 'fillStyle', _value, renderState.currentContext);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
// #endregion stroke/fill style
|
|
775
|
+
}, {
|
|
776
|
+
key: "applyStyleToContext",
|
|
777
|
+
value: function applyStyleToContext(context, object, forceUpdate, renderState) {
|
|
778
|
+
var nodeName = object.nodeName;
|
|
779
|
+
this.applyCommonStyleToContext(context, object, forceUpdate, renderState);
|
|
780
|
+
if (nodeName === gLite.Shape.IMAGE) ; else {
|
|
781
|
+
this.applyStrokeFillStyleToContext(context, object, forceUpdate, renderState);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}, {
|
|
785
|
+
key: "applyShadowAndFilterStyleToContext",
|
|
786
|
+
value: function applyShadowAndFilterStyleToContext(context, object, hasShadow, renderState) {
|
|
787
|
+
var style = object.parsedStyle;
|
|
788
|
+
if (hasShadow) {
|
|
789
|
+
updateContextIfNotHitCache(context, 'shadowColor', style.shadowColor.toString(), renderState.currentContext);
|
|
790
|
+
for (var i = 0; i < SHADOW_NUMBER_STYLE.length; i++) {
|
|
791
|
+
var styleName = SHADOW_NUMBER_STYLE[i];
|
|
792
|
+
updateContextIfNotHitCache(context, styleName, style[styleName] || DEFAULT_STYLE[styleName], renderState.currentContext);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
if (style.filter && style.filter.length) {
|
|
796
|
+
updateContextIfNotHitCache(context, 'filter',
|
|
797
|
+
// use raw filter string
|
|
798
|
+
object.attributes.filter, renderState.currentContext);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}, {
|
|
802
|
+
key: "clearShadowAndFilterStyleForContext",
|
|
803
|
+
value: function clearShadowAndFilterStyleForContext(context, hasShadow, hasFilter, renderState) {
|
|
804
|
+
var onlyClearShadowFilter = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
805
|
+
if (hasShadow) {
|
|
806
|
+
updateContextIfNotHitCache(context, 'shadowColor', DEFAULT_STYLE.shadowColor, renderState.currentContext);
|
|
807
|
+
for (var i = 0; i < SHADOW_NUMBER_STYLE.length; i++) {
|
|
808
|
+
var styleName = SHADOW_NUMBER_STYLE[i];
|
|
809
|
+
updateContextIfNotHitCache(context, styleName, DEFAULT_STYLE[styleName], renderState.currentContext);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
if (hasFilter) {
|
|
813
|
+
if (hasShadow && onlyClearShadowFilter) {
|
|
814
|
+
// save drop-shadow filter
|
|
815
|
+
var oldFilter = context.filter;
|
|
816
|
+
if (!util.isNil(oldFilter) && oldFilter.indexOf('drop-shadow') > -1) {
|
|
817
|
+
updateContextIfNotHitCache(context, 'filter', oldFilter.replace(/drop-shadow\([^)]*\)/, '').trim() || DEFAULT_STYLE.filter, renderState.currentContext);
|
|
818
|
+
}
|
|
819
|
+
} else {
|
|
820
|
+
updateContextIfNotHitCache(context, 'filter', DEFAULT_STYLE.filter, renderState.currentContext);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}, {
|
|
825
|
+
key: "fillToContext",
|
|
826
|
+
value: function fillToContext(context, object, renderState, plugin, runtime) {
|
|
827
|
+
var _this = this;
|
|
828
|
+
var _object$parsedStyle = object.parsedStyle,
|
|
829
|
+
fill = _object$parsedStyle.fill,
|
|
830
|
+
fillRule = _object$parsedStyle.fillRule;
|
|
831
|
+
var resetStyle = null;
|
|
832
|
+
if (Array.isArray(fill) && fill.length > 0) {
|
|
833
|
+
fill.forEach(function (gradient) {
|
|
834
|
+
var prevStyle = updateContextIfNotHitCache(context, 'fillStyle', getColor(gradient, object, context, _this.imagePool), renderState.currentContext);
|
|
835
|
+
resetStyle = resetStyle !== null && resetStyle !== void 0 ? resetStyle : prevStyle;
|
|
836
|
+
if (fillRule) {
|
|
837
|
+
context.fill(fillRule);
|
|
838
|
+
} else {
|
|
839
|
+
context.fill();
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
} else {
|
|
843
|
+
if (gLite.isPattern(fill)) {
|
|
844
|
+
var pattern = getPattern(fill, object, context, object.ownerDocument.defaultView.context, plugin, runtime, this.imagePool);
|
|
845
|
+
if (pattern) {
|
|
846
|
+
context.fillStyle = pattern;
|
|
847
|
+
resetStyle = true;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
if (fillRule) {
|
|
851
|
+
context.fill(fillRule);
|
|
852
|
+
} else {
|
|
853
|
+
context.fill();
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
if (resetStyle !== null) {
|
|
857
|
+
updateContextIfNotHitCache(context, 'fillStyle', resetStyle, renderState.currentContext);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}, {
|
|
861
|
+
key: "strokeToContext",
|
|
862
|
+
value: function strokeToContext(context, object, renderState, plugin, runtime) {
|
|
863
|
+
var _this2 = this;
|
|
864
|
+
var stroke = object.parsedStyle.stroke;
|
|
865
|
+
var resetStyle = null;
|
|
866
|
+
if (Array.isArray(stroke) && stroke.length > 0) {
|
|
867
|
+
stroke.forEach(function (gradient) {
|
|
868
|
+
var prevStyle = updateContextIfNotHitCache(context, 'strokeStyle', getColor(gradient, object, context, _this2.imagePool), renderState.currentContext);
|
|
869
|
+
resetStyle = resetStyle !== null && resetStyle !== void 0 ? resetStyle : prevStyle;
|
|
870
|
+
context.stroke();
|
|
35
871
|
});
|
|
872
|
+
} else {
|
|
873
|
+
if (gLite.isPattern(stroke)) {
|
|
874
|
+
var pattern = getPattern(stroke, object, context, object.ownerDocument.defaultView.context, plugin, runtime, this.imagePool);
|
|
875
|
+
if (pattern) {
|
|
876
|
+
var prevStyle = updateContextIfNotHitCache(context, 'strokeStyle', pattern, renderState.currentContext);
|
|
877
|
+
resetStyle = resetStyle !== null && resetStyle !== void 0 ? resetStyle : prevStyle;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
context.stroke();
|
|
881
|
+
}
|
|
882
|
+
if (resetStyle !== null) {
|
|
883
|
+
updateContextIfNotHitCache(context, 'strokeStyle', resetStyle, renderState.currentContext);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}, {
|
|
887
|
+
key: "drawToContext",
|
|
888
|
+
value: function drawToContext(context, object, renderState, plugin, runtime) {
|
|
889
|
+
var _style$fill;
|
|
890
|
+
var nodeName = object.nodeName;
|
|
891
|
+
var style = object.parsedStyle;
|
|
892
|
+
var _style$opacity = style.opacity,
|
|
893
|
+
opacity = _style$opacity === void 0 ? DEFAULT_STYLE.globalAlpha : _style$opacity,
|
|
894
|
+
_style$fillOpacity = style.fillOpacity,
|
|
895
|
+
fillOpacity = _style$fillOpacity === void 0 ? DEFAULT_STYLE.fillOpacity : _style$fillOpacity,
|
|
896
|
+
_style$strokeOpacity = style.strokeOpacity,
|
|
897
|
+
strokeOpacity = _style$strokeOpacity === void 0 ? DEFAULT_STYLE.strokeOpacity : _style$strokeOpacity,
|
|
898
|
+
_style$lineWidth2 = style.lineWidth,
|
|
899
|
+
lineWidth = _style$lineWidth2 === void 0 ? DEFAULT_STYLE.lineWidth : _style$lineWidth2;
|
|
900
|
+
var hasFill = style.fill && !style.fill.isNone;
|
|
901
|
+
var hasStroke = style.stroke && !style.stroke.isNone && lineWidth > 0;
|
|
902
|
+
if (!hasFill && !hasStroke) {
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
var hasShadow = !util.isNil(style.shadowColor) && style.shadowBlur > 0;
|
|
906
|
+
var isInnerShadow = style.shadowType === 'inner';
|
|
907
|
+
var isFillTransparent = ((_style$fill = style.fill) === null || _style$fill === void 0 ? void 0 : _style$fill.alpha) === 0;
|
|
908
|
+
var hasFilter = !!(style.filter && style.filter.length);
|
|
909
|
+
// Shadows can only be applied to fill() or stroke(), the default is fill()
|
|
910
|
+
var shouldDrawShadowWithStroke = hasShadow && hasStroke && (nodeName === gLite.Shape.PATH || nodeName === gLite.Shape.LINE || nodeName === gLite.Shape.POLYLINE || isFillTransparent || isInnerShadow);
|
|
911
|
+
|
|
912
|
+
// TODO https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/paint-order
|
|
913
|
+
|
|
914
|
+
var originGlobalAlpha = null;
|
|
915
|
+
if (hasFill) {
|
|
916
|
+
if (!shouldDrawShadowWithStroke) {
|
|
917
|
+
this.applyShadowAndFilterStyleToContext(context, object, hasShadow, renderState);
|
|
918
|
+
}
|
|
919
|
+
var updateOpacity = opacity * fillOpacity;
|
|
920
|
+
originGlobalAlpha = updateContextIfNotHitCache(context, 'globalAlpha', updateOpacity, renderState.currentContext);
|
|
921
|
+
this.fillToContext(context, object, renderState, plugin, runtime);
|
|
922
|
+
if (!shouldDrawShadowWithStroke) {
|
|
923
|
+
this.clearShadowAndFilterStyleForContext(context, hasShadow, hasFilter, renderState);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
if (hasStroke) {
|
|
927
|
+
var clearShadowAndFilter = false;
|
|
928
|
+
var _updateOpacity = opacity * strokeOpacity;
|
|
929
|
+
var prevOpacity = updateContextIfNotHitCache(context, 'globalAlpha', _updateOpacity, renderState.currentContext);
|
|
930
|
+
originGlobalAlpha = hasFill ? originGlobalAlpha : prevOpacity;
|
|
931
|
+
if (shouldDrawShadowWithStroke) {
|
|
932
|
+
this.applyShadowAndFilterStyleToContext(context, object, hasShadow, renderState);
|
|
933
|
+
clearShadowAndFilter = true;
|
|
934
|
+
if (isInnerShadow) {
|
|
935
|
+
var originBlend = context.globalCompositeOperation;
|
|
936
|
+
context.globalCompositeOperation = 'source-atop';
|
|
937
|
+
this.strokeToContext(context, object, renderState, plugin, runtime);
|
|
938
|
+
context.globalCompositeOperation = originBlend;
|
|
939
|
+
this.clearShadowAndFilterStyleForContext(context, hasShadow, hasFilter, renderState, true);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
this.strokeToContext(context, object, renderState, plugin, runtime);
|
|
943
|
+
if (clearShadowAndFilter) {
|
|
944
|
+
this.clearShadowAndFilterStyleForContext(context, hasShadow, hasFilter, renderState);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// clear
|
|
949
|
+
if (originGlobalAlpha !== null) {
|
|
950
|
+
updateContextIfNotHitCache(context, 'globalAlpha', originGlobalAlpha, renderState.currentContext);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}]);
|
|
954
|
+
}();
|
|
955
|
+
|
|
956
|
+
var DefaultRenderer = /*#__PURE__*/function (_OptimizedDefaultRend) {
|
|
957
|
+
function DefaultRenderer() {
|
|
958
|
+
_classCallCheck(this, DefaultRenderer);
|
|
959
|
+
return _callSuper(this, DefaultRenderer, arguments);
|
|
960
|
+
}
|
|
961
|
+
_inherits(DefaultRenderer, _OptimizedDefaultRend);
|
|
962
|
+
return _createClass(DefaultRenderer, [{
|
|
963
|
+
key: "render",
|
|
964
|
+
value: function render(context, parsedStyle, object, canvasContext, plugin, runtime) {
|
|
965
|
+
var fill = parsedStyle.fill,
|
|
966
|
+
fillRule = parsedStyle.fillRule,
|
|
967
|
+
_parsedStyle$opacity = parsedStyle.opacity,
|
|
968
|
+
opacity = _parsedStyle$opacity === void 0 ? 1 : _parsedStyle$opacity,
|
|
969
|
+
_parsedStyle$fillOpac = parsedStyle.fillOpacity,
|
|
970
|
+
fillOpacity = _parsedStyle$fillOpac === void 0 ? 1 : _parsedStyle$fillOpac,
|
|
971
|
+
stroke = parsedStyle.stroke,
|
|
972
|
+
_parsedStyle$strokeOp = parsedStyle.strokeOpacity,
|
|
973
|
+
strokeOpacity = _parsedStyle$strokeOp === void 0 ? 1 : _parsedStyle$strokeOp,
|
|
974
|
+
_parsedStyle$lineWidt = parsedStyle.lineWidth,
|
|
975
|
+
lineWidth = _parsedStyle$lineWidt === void 0 ? 1 : _parsedStyle$lineWidt,
|
|
976
|
+
lineCap = parsedStyle.lineCap,
|
|
977
|
+
lineJoin = parsedStyle.lineJoin,
|
|
978
|
+
shadowType = parsedStyle.shadowType,
|
|
979
|
+
shadowColor = parsedStyle.shadowColor,
|
|
980
|
+
shadowBlur = parsedStyle.shadowBlur,
|
|
981
|
+
filter = parsedStyle.filter,
|
|
982
|
+
miterLimit = parsedStyle.miterLimit;
|
|
983
|
+
var hasFill = fill && !fill.isNone;
|
|
984
|
+
var hasStroke = stroke && !stroke.isNone && lineWidth > 0;
|
|
985
|
+
var isFillTransparent = (fill === null || fill === void 0 ? void 0 : fill.alpha) === 0;
|
|
986
|
+
var hasFilter = !!(filter && filter.length);
|
|
987
|
+
var hasShadow = !util.isNil(shadowColor) && shadowBlur > 0;
|
|
988
|
+
var nodeName = object.nodeName;
|
|
989
|
+
var isInnerShadow = shadowType === 'inner';
|
|
990
|
+
var shouldDrawShadowWithStroke = hasStroke && hasShadow && (nodeName === gLite.Shape.PATH || nodeName === gLite.Shape.LINE || nodeName === gLite.Shape.POLYLINE || isFillTransparent || isInnerShadow);
|
|
991
|
+
if (hasFill) {
|
|
992
|
+
context.globalAlpha = opacity * fillOpacity;
|
|
993
|
+
if (!shouldDrawShadowWithStroke) {
|
|
994
|
+
setShadowAndFilter(object, context, hasShadow);
|
|
995
|
+
}
|
|
996
|
+
applyFill(context, object, fill, fillRule, canvasContext, plugin, runtime, this.imagePool);
|
|
997
|
+
if (!shouldDrawShadowWithStroke) {
|
|
998
|
+
this.clearShadowAndFilter(context, hasFilter, hasShadow);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
if (hasStroke) {
|
|
1002
|
+
context.globalAlpha = opacity * strokeOpacity;
|
|
1003
|
+
context.lineWidth = lineWidth;
|
|
1004
|
+
if (!util.isNil(miterLimit)) {
|
|
1005
|
+
context.miterLimit = miterLimit;
|
|
1006
|
+
}
|
|
1007
|
+
if (!util.isNil(lineCap)) {
|
|
1008
|
+
context.lineCap = lineCap;
|
|
1009
|
+
}
|
|
1010
|
+
if (!util.isNil(lineJoin)) {
|
|
1011
|
+
context.lineJoin = lineJoin;
|
|
1012
|
+
}
|
|
1013
|
+
if (shouldDrawShadowWithStroke) {
|
|
1014
|
+
if (isInnerShadow) {
|
|
1015
|
+
context.globalCompositeOperation = 'source-atop';
|
|
1016
|
+
}
|
|
1017
|
+
setShadowAndFilter(object, context, true);
|
|
1018
|
+
if (isInnerShadow) {
|
|
1019
|
+
applyStroke(context, object, stroke, canvasContext, plugin, runtime, this.imagePool);
|
|
1020
|
+
context.globalCompositeOperation = DEFAULT_STYLE.globalCompositeOperation;
|
|
1021
|
+
this.clearShadowAndFilter(context, hasFilter, true);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
applyStroke(context, object, stroke, canvasContext, plugin, runtime, this.imagePool);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}, {
|
|
1028
|
+
key: "clearShadowAndFilter",
|
|
1029
|
+
value: function clearShadowAndFilter(context, hasFilter, hasShadow) {
|
|
1030
|
+
if (hasShadow) {
|
|
1031
|
+
context.shadowColor = 'transparent';
|
|
1032
|
+
context.shadowBlur = 0;
|
|
1033
|
+
}
|
|
1034
|
+
if (hasFilter) {
|
|
1035
|
+
// save drop-shadow filter
|
|
1036
|
+
var oldFilter = context.filter;
|
|
1037
|
+
if (!util.isNil(oldFilter) && oldFilter.indexOf('drop-shadow') > -1) {
|
|
1038
|
+
context.filter = oldFilter.replace(/drop-shadow\([^)]*\)/, '').trim() || 'none';
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}]);
|
|
1043
|
+
}(OptimizedDefaultRenderer);
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* apply before fill and stroke but only once
|
|
1047
|
+
*/
|
|
1048
|
+
function setShadowAndFilter(object, context, hasShadow) {
|
|
1049
|
+
var _object$parsedStyle = object.parsedStyle,
|
|
1050
|
+
filter = _object$parsedStyle.filter,
|
|
1051
|
+
shadowColor = _object$parsedStyle.shadowColor,
|
|
1052
|
+
shadowBlur = _object$parsedStyle.shadowBlur,
|
|
1053
|
+
shadowOffsetX = _object$parsedStyle.shadowOffsetX,
|
|
1054
|
+
shadowOffsetY = _object$parsedStyle.shadowOffsetY;
|
|
1055
|
+
if (filter && filter.length) {
|
|
1056
|
+
// use raw filter string
|
|
1057
|
+
context.filter = object.style.filter;
|
|
1058
|
+
}
|
|
1059
|
+
if (hasShadow) {
|
|
1060
|
+
context.shadowColor = shadowColor.toString();
|
|
1061
|
+
context.shadowBlur = shadowBlur || 0;
|
|
1062
|
+
context.shadowOffsetX = shadowOffsetX || 0;
|
|
1063
|
+
context.shadowOffsetY = shadowOffsetY || 0;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
function applyFill(context, object, fill, fillRule, canvasContext, plugin, runtime, imagePool) {
|
|
1067
|
+
var skipFill = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : false;
|
|
1068
|
+
if (Array.isArray(fill)) {
|
|
1069
|
+
fill.forEach(function (gradient) {
|
|
1070
|
+
context.fillStyle = getColor(gradient, object, context, imagePool);
|
|
1071
|
+
if (!skipFill) {
|
|
1072
|
+
fillRule ? context.fill(fillRule) : context.fill();
|
|
1073
|
+
}
|
|
1074
|
+
});
|
|
1075
|
+
} else {
|
|
1076
|
+
if (gLite.isPattern(fill)) {
|
|
1077
|
+
context.fillStyle = getPattern(fill, object, context, canvasContext, plugin, runtime, imagePool);
|
|
1078
|
+
}
|
|
1079
|
+
if (!skipFill) {
|
|
1080
|
+
fillRule ? context.fill(fillRule) : context.fill();
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
function applyStroke(context, object, stroke, canvasContext, plugin, runtime, imagePool) {
|
|
1085
|
+
var skipStroke = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : false;
|
|
1086
|
+
if (Array.isArray(stroke)) {
|
|
1087
|
+
stroke.forEach(function (gradient) {
|
|
1088
|
+
context.strokeStyle = getColor(gradient, object, context, imagePool);
|
|
1089
|
+
if (!skipStroke) {
|
|
1090
|
+
context.stroke();
|
|
36
1091
|
}
|
|
37
1092
|
});
|
|
1093
|
+
} else {
|
|
1094
|
+
if (gLite.isPattern(stroke)) {
|
|
1095
|
+
context.strokeStyle = getPattern(stroke, object, context, canvasContext, plugin, runtime, imagePool);
|
|
1096
|
+
}
|
|
1097
|
+
if (!skipStroke) {
|
|
1098
|
+
context.stroke();
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
function calculateOverlapRect(rect1, rect2) {
|
|
1104
|
+
var _rect = _slicedToArray(rect1, 4),
|
|
1105
|
+
x1 = _rect[0],
|
|
1106
|
+
y1 = _rect[1],
|
|
1107
|
+
w1 = _rect[2],
|
|
1108
|
+
h1 = _rect[3];
|
|
1109
|
+
var _rect2 = _slicedToArray(rect2, 4),
|
|
1110
|
+
x2 = _rect2[0],
|
|
1111
|
+
y2 = _rect2[1],
|
|
1112
|
+
w2 = _rect2[2],
|
|
1113
|
+
h2 = _rect2[3];
|
|
1114
|
+
|
|
1115
|
+
// 计算重叠区域的左上角和右下角
|
|
1116
|
+
var overlapLeft = Math.max(x1, x2);
|
|
1117
|
+
var overlapTop = Math.max(y1, y2);
|
|
1118
|
+
var overlapRight = Math.min(x1 + w1, x2 + w2);
|
|
1119
|
+
var overlapBottom = Math.min(y1 + h1, y2 + h2);
|
|
1120
|
+
if (overlapRight <= overlapLeft || overlapBottom <= overlapTop) {
|
|
1121
|
+
return null;
|
|
38
1122
|
}
|
|
39
|
-
|
|
40
|
-
|
|
1123
|
+
return [overlapLeft, overlapTop, overlapRight - overlapLeft, overlapBottom - overlapTop];
|
|
1124
|
+
}
|
|
1125
|
+
function transformRect(rect, matrix) {
|
|
1126
|
+
var tl = glMatrix.vec3.transformMat4(glMatrix.vec3.create(), [rect[0], rect[1], 0], matrix);
|
|
1127
|
+
var tr = glMatrix.vec3.transformMat4(glMatrix.vec3.create(), [rect[0] + rect[2], rect[1], 0], matrix);
|
|
1128
|
+
var bl = glMatrix.vec3.transformMat4(glMatrix.vec3.create(), [rect[0], rect[1] + rect[3], 0], matrix);
|
|
1129
|
+
var br = glMatrix.vec3.transformMat4(glMatrix.vec3.create(), [rect[0] + rect[2], rect[1] + rect[3], 0], matrix);
|
|
1130
|
+
return [Math.min(tl[0], tr[0], bl[0], br[0]), Math.min(tl[1], tr[1], bl[1], br[1]), Math.max(tl[0], tr[0], bl[0], br[0]) - Math.min(tl[0], tr[0], bl[0], br[0]), Math.max(tl[1], tr[1], bl[1], br[1]) - Math.min(tl[1], tr[1], bl[1], br[1])];
|
|
41
1131
|
}
|
|
42
1132
|
|
|
43
|
-
var
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1133
|
+
var ImageRenderer = /*#__PURE__*/function (_DefaultRenderer) {
|
|
1134
|
+
function ImageRenderer() {
|
|
1135
|
+
_classCallCheck(this, ImageRenderer);
|
|
1136
|
+
return _callSuper(this, ImageRenderer, arguments);
|
|
1137
|
+
}
|
|
1138
|
+
_inherits(ImageRenderer, _DefaultRenderer);
|
|
1139
|
+
return _createClass(ImageRenderer, [{
|
|
1140
|
+
key: "renderDownSampled",
|
|
1141
|
+
value: function renderDownSampled(context, parsedStyle, object, data) {
|
|
1142
|
+
var src = data.src,
|
|
1143
|
+
imageCache = data.imageCache;
|
|
1144
|
+
if (!imageCache.downSampled) {
|
|
1145
|
+
this.imagePool.createDownSampledImage(src, object).then(function () {
|
|
1146
|
+
// be removed from dom tree
|
|
1147
|
+
if (!object.ownerDocument) {
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// rerender
|
|
1152
|
+
object.dirty();
|
|
1153
|
+
object.ownerDocument.defaultView.context.renderingService.dirty();
|
|
1154
|
+
})["catch"](function (reason) {
|
|
1155
|
+
console.error(reason);
|
|
1156
|
+
});
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
context.drawImage(imageCache.downSampled, Math.floor(data.drawRect[0]), Math.floor(data.drawRect[1]), Math.ceil(data.drawRect[2]), Math.ceil(data.drawRect[3]));
|
|
1160
|
+
}
|
|
1161
|
+
}, {
|
|
1162
|
+
key: "renderTile",
|
|
1163
|
+
value: function renderTile(context, parsedStyle, object, data) {
|
|
1164
|
+
var src = data.src,
|
|
1165
|
+
imageCache = data.imageCache,
|
|
1166
|
+
imageRect = data.imageRect,
|
|
1167
|
+
drawRect = data.drawRect;
|
|
1168
|
+
var originalSize = imageCache.size;
|
|
1169
|
+
var _context$getTransform = context.getTransform(),
|
|
1170
|
+
a = _context$getTransform.a,
|
|
1171
|
+
b = _context$getTransform.b,
|
|
1172
|
+
c = _context$getTransform.c,
|
|
1173
|
+
d = _context$getTransform.d,
|
|
1174
|
+
e = _context$getTransform.e,
|
|
1175
|
+
f = _context$getTransform.f;
|
|
1176
|
+
context.resetTransform();
|
|
1177
|
+
if (!(imageCache !== null && imageCache !== void 0 && imageCache.gridSize)) {
|
|
1178
|
+
this.imagePool.createImageTiles(src, [], function () {
|
|
1179
|
+
// be removed from dom tree
|
|
1180
|
+
if (!object.ownerDocument) {
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
// rerender
|
|
1185
|
+
object.dirty();
|
|
1186
|
+
object.ownerDocument.defaultView.context.renderingService.dirty();
|
|
1187
|
+
}, object)["catch"](function (reason) {
|
|
1188
|
+
console.error(reason);
|
|
1189
|
+
});
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
var scaleToOrigin = [originalSize[0] / imageRect[2], originalSize[1] / imageRect[3]];
|
|
1193
|
+
var scaledTileSize = [imageCache.tileSize[0] / scaleToOrigin[0], imageCache.tileSize[1] / scaleToOrigin[1]];
|
|
1194
|
+
var _ref = [Math.floor((drawRect[0] - imageRect[0]) / scaledTileSize[0]), Math.ceil((drawRect[0] + drawRect[2] - imageRect[0]) / scaledTileSize[0])],
|
|
1195
|
+
startTileX = _ref[0],
|
|
1196
|
+
endTileX = _ref[1];
|
|
1197
|
+
var _ref2 = [Math.floor((drawRect[1] - imageRect[1]) / scaledTileSize[1]), Math.ceil((drawRect[1] + drawRect[3] - imageRect[1]) / scaledTileSize[1])],
|
|
1198
|
+
startTileY = _ref2[0],
|
|
1199
|
+
endTileY = _ref2[1];
|
|
1200
|
+
for (var tileY = startTileY; tileY <= endTileY; tileY++) {
|
|
1201
|
+
for (var tileX = startTileX; tileX <= endTileX; tileX++) {
|
|
1202
|
+
var item = imageCache.tiles[tileY][tileX];
|
|
1203
|
+
if (item) {
|
|
1204
|
+
var tileRect = [Math.floor(imageRect[0] + item.tileX * scaledTileSize[0]), Math.floor(imageRect[1] + item.tileY * scaledTileSize[1]), Math.ceil(scaledTileSize[0]), Math.ceil(scaledTileSize[1])];
|
|
1205
|
+
context.drawImage(item.data, tileRect[0], tileRect[1], tileRect[2], tileRect[3]);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
context.setTransform(a, b, c, d, e, f);
|
|
1210
|
+
}
|
|
1211
|
+
}, {
|
|
1212
|
+
key: "render",
|
|
1213
|
+
value: function render(context, parsedStyle, object) {
|
|
1214
|
+
var _parsedStyle$x = parsedStyle.x,
|
|
1215
|
+
x = _parsedStyle$x === void 0 ? 0 : _parsedStyle$x,
|
|
1216
|
+
_parsedStyle$y = parsedStyle.y,
|
|
1217
|
+
y = _parsedStyle$y === void 0 ? 0 : _parsedStyle$y,
|
|
1218
|
+
width = parsedStyle.width,
|
|
1219
|
+
height = parsedStyle.height,
|
|
1220
|
+
src = parsedStyle.src,
|
|
1221
|
+
shadowColor = parsedStyle.shadowColor,
|
|
1222
|
+
shadowBlur = parsedStyle.shadowBlur;
|
|
1223
|
+
var imageCache = this.imagePool.getImageSync(src, object);
|
|
1224
|
+
var image = imageCache === null || imageCache === void 0 ? void 0 : imageCache.img;
|
|
1225
|
+
var iw = width;
|
|
1226
|
+
var ih = height;
|
|
1227
|
+
if (!image) {
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
iw || (iw = image.width);
|
|
1231
|
+
ih || (ih = image.height);
|
|
1232
|
+
var hasShadow = !util.isNil(shadowColor) && shadowBlur > 0;
|
|
1233
|
+
setShadowAndFilter(object, context, hasShadow);
|
|
1234
|
+
|
|
1235
|
+
// node-canvas will throw the following err:
|
|
1236
|
+
// Error: Image given has not completed loading
|
|
1237
|
+
try {
|
|
1238
|
+
var _object$ownerDocument = object.ownerDocument.defaultView.getContextService().getDomElement(),
|
|
1239
|
+
viewWidth = _object$ownerDocument.width,
|
|
1240
|
+
viewHeight = _object$ownerDocument.height;
|
|
1241
|
+
var currentTransform = context.getTransform();
|
|
1242
|
+
var a = currentTransform.a,
|
|
1243
|
+
b = currentTransform.b,
|
|
1244
|
+
c = currentTransform.c,
|
|
1245
|
+
d = currentTransform.d,
|
|
1246
|
+
e = currentTransform.e,
|
|
1247
|
+
f = currentTransform.f;
|
|
1248
|
+
// 构建 mat4 矩阵
|
|
1249
|
+
// prettier-ignore
|
|
1250
|
+
var transformMatrix = glMatrix.mat4.fromValues(a, c, 0, 0, b, d, 0, 0, 0, 0, 1, 0, e, f, 0, 1);
|
|
1251
|
+
var imageRect = transformRect([x, y, iw, ih], transformMatrix);
|
|
1252
|
+
var drawRect = calculateOverlapRect([0, 0, viewWidth, viewHeight], imageRect);
|
|
1253
|
+
if (!drawRect) {
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
if (!object.ownerDocument.defaultView.getConfig().enableLargeImageOptimization) {
|
|
1257
|
+
ImageRenderer.renderFull(context, parsedStyle, object, {
|
|
1258
|
+
image: image,
|
|
1259
|
+
drawRect: [x, y, iw, ih]
|
|
1260
|
+
});
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
var sizeOfOrigin = imageRect[2] / imageCache.size[0];
|
|
1264
|
+
if (sizeOfOrigin < (imageCache.downSamplingRate || 0.5)) {
|
|
1265
|
+
this.renderDownSampled(context, parsedStyle, object, {
|
|
1266
|
+
src: src,
|
|
1267
|
+
imageCache: imageCache,
|
|
1268
|
+
drawRect: [x, y, iw, ih]
|
|
1269
|
+
});
|
|
1270
|
+
return;
|
|
1271
|
+
}
|
|
1272
|
+
if (!ImagePool.isSupportTile) {
|
|
1273
|
+
ImageRenderer.renderFull(context, parsedStyle, object, {
|
|
1274
|
+
image: image,
|
|
1275
|
+
drawRect: [x, y, iw, ih]
|
|
1276
|
+
});
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
this.renderTile(context, parsedStyle, object, {
|
|
1280
|
+
src: src,
|
|
1281
|
+
imageCache: imageCache,
|
|
1282
|
+
imageRect: imageRect,
|
|
1283
|
+
drawRect: drawRect
|
|
1284
|
+
});
|
|
1285
|
+
} catch (_unused) {
|
|
1286
|
+
// expected error
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// ---
|
|
1291
|
+
}, {
|
|
1292
|
+
key: "drawToContext",
|
|
1293
|
+
value: function drawToContext(context, object, renderState, plugin, runtime) {
|
|
1294
|
+
this.render(context, object.parsedStyle, object);
|
|
1295
|
+
}
|
|
1296
|
+
}], [{
|
|
1297
|
+
key: "renderFull",
|
|
1298
|
+
value: function renderFull(context, parsedStyle, object, data) {
|
|
1299
|
+
context.drawImage(data.image, Math.floor(data.drawRect[0]), Math.floor(data.drawRect[1]), Math.ceil(data.drawRect[2]), Math.ceil(data.drawRect[3]));
|
|
1300
|
+
}
|
|
1301
|
+
}]);
|
|
1302
|
+
}(DefaultRenderer);
|
|
1303
|
+
|
|
1304
|
+
var TextRenderer = /*#__PURE__*/function (_DefaultRenderer) {
|
|
1305
|
+
function TextRenderer() {
|
|
1306
|
+
_classCallCheck(this, TextRenderer);
|
|
1307
|
+
return _callSuper(this, TextRenderer, arguments);
|
|
1308
|
+
}
|
|
1309
|
+
_inherits(TextRenderer, _DefaultRenderer);
|
|
1310
|
+
return _createClass(TextRenderer, [{
|
|
1311
|
+
key: "render",
|
|
1312
|
+
value: function render(context, parsedStyle, object, canvasContext, plugin, runtime) {
|
|
1313
|
+
// Trigger text geometry calculation.
|
|
1314
|
+
object.getBounds();
|
|
1315
|
+
var _parsedStyle$lineWidt = parsedStyle.lineWidth,
|
|
1316
|
+
lineWidth = _parsedStyle$lineWidt === void 0 ? 1 : _parsedStyle$lineWidt,
|
|
1317
|
+
_parsedStyle$textAlig = parsedStyle.textAlign,
|
|
1318
|
+
textAlign = _parsedStyle$textAlig === void 0 ? 'start' : _parsedStyle$textAlig,
|
|
1319
|
+
_parsedStyle$textBase = parsedStyle.textBaseline,
|
|
1320
|
+
textBaseline = _parsedStyle$textBase === void 0 ? 'alphabetic' : _parsedStyle$textBase,
|
|
1321
|
+
_parsedStyle$lineJoin = parsedStyle.lineJoin,
|
|
1322
|
+
lineJoin = _parsedStyle$lineJoin === void 0 ? 'miter' : _parsedStyle$lineJoin,
|
|
1323
|
+
_parsedStyle$miterLim = parsedStyle.miterLimit,
|
|
1324
|
+
miterLimit = _parsedStyle$miterLim === void 0 ? 10 : _parsedStyle$miterLim,
|
|
1325
|
+
_parsedStyle$letterSp = parsedStyle.letterSpacing,
|
|
1326
|
+
letterSpacing = _parsedStyle$letterSp === void 0 ? 0 : _parsedStyle$letterSp,
|
|
1327
|
+
stroke = parsedStyle.stroke,
|
|
1328
|
+
fill = parsedStyle.fill,
|
|
1329
|
+
fillRule = parsedStyle.fillRule,
|
|
1330
|
+
_parsedStyle$fillOpac = parsedStyle.fillOpacity,
|
|
1331
|
+
fillOpacity = _parsedStyle$fillOpac === void 0 ? 1 : _parsedStyle$fillOpac,
|
|
1332
|
+
_parsedStyle$strokeOp = parsedStyle.strokeOpacity,
|
|
1333
|
+
strokeOpacity = _parsedStyle$strokeOp === void 0 ? 1 : _parsedStyle$strokeOp,
|
|
1334
|
+
_parsedStyle$opacity = parsedStyle.opacity,
|
|
1335
|
+
opacity = _parsedStyle$opacity === void 0 ? 1 : _parsedStyle$opacity,
|
|
1336
|
+
metrics = parsedStyle.metrics,
|
|
1337
|
+
_parsedStyle$x = parsedStyle.x,
|
|
1338
|
+
x = _parsedStyle$x === void 0 ? 0 : _parsedStyle$x,
|
|
1339
|
+
_parsedStyle$y = parsedStyle.y,
|
|
1340
|
+
y = _parsedStyle$y === void 0 ? 0 : _parsedStyle$y,
|
|
1341
|
+
dx = parsedStyle.dx,
|
|
1342
|
+
dy = parsedStyle.dy,
|
|
1343
|
+
shadowColor = parsedStyle.shadowColor,
|
|
1344
|
+
shadowBlur = parsedStyle.shadowBlur,
|
|
1345
|
+
textDecorationLine = parsedStyle.textDecorationLine;
|
|
1346
|
+
var font = metrics.font,
|
|
1347
|
+
lines = metrics.lines,
|
|
1348
|
+
height = metrics.height,
|
|
1349
|
+
lineHeight = metrics.lineHeight,
|
|
1350
|
+
lineMetrics = metrics.lineMetrics;
|
|
1351
|
+
context.font = font;
|
|
1352
|
+
context.lineWidth = lineWidth;
|
|
1353
|
+
context.textAlign = textAlign === 'middle' ? 'center' : textAlign;
|
|
1354
|
+
var formattedTextBaseline = textBaseline;
|
|
1355
|
+
if (formattedTextBaseline === 'alphabetic') {
|
|
1356
|
+
formattedTextBaseline = 'bottom';
|
|
1357
|
+
}
|
|
1358
|
+
context.lineJoin = lineJoin;
|
|
1359
|
+
if (!util.isNil(miterLimit)) {
|
|
1360
|
+
context.miterLimit = miterLimit;
|
|
1361
|
+
}
|
|
1362
|
+
var linePositionY = y;
|
|
1363
|
+
// handle vertical text baseline
|
|
1364
|
+
if (textBaseline === 'middle') {
|
|
1365
|
+
linePositionY += -height / 2 - lineHeight / 2;
|
|
1366
|
+
} else if (textBaseline === 'bottom' || textBaseline === 'alphabetic' || textBaseline === 'ideographic') {
|
|
1367
|
+
linePositionY += -height;
|
|
1368
|
+
} else if (textBaseline === 'top' || textBaseline === 'hanging') {
|
|
1369
|
+
linePositionY += -lineHeight;
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
// account for dx & dy
|
|
1373
|
+
var offsetX = x + (dx || 0);
|
|
1374
|
+
linePositionY += dy || 0;
|
|
1375
|
+
if (lines.length === 1) {
|
|
1376
|
+
if (formattedTextBaseline === 'bottom') {
|
|
1377
|
+
formattedTextBaseline = 'middle';
|
|
1378
|
+
linePositionY -= 0.5 * height;
|
|
1379
|
+
} else if (formattedTextBaseline === 'top') {
|
|
1380
|
+
formattedTextBaseline = 'middle';
|
|
1381
|
+
linePositionY += 0.5 * height;
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
context.textBaseline = formattedTextBaseline;
|
|
1385
|
+
var hasShadow = !util.isNil(shadowColor) && shadowBlur > 0;
|
|
1386
|
+
setShadowAndFilter(object, context, hasShadow);
|
|
1387
|
+
|
|
1388
|
+
// draw lines line by line
|
|
1389
|
+
for (var i = 0; i < lines.length; i++) {
|
|
1390
|
+
var linePositionX = lineWidth / 2 + offsetX;
|
|
1391
|
+
linePositionY += lineHeight;
|
|
1392
|
+
|
|
1393
|
+
// no need to re-position X, cause we already set text align
|
|
1394
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
|
|
1395
|
+
if (!util.isNil(stroke) && !stroke.isNone && lineWidth) {
|
|
1396
|
+
this.drawLetterSpacing(context, object, lines[i], lineMetrics[i], textAlign, linePositionX, linePositionY, letterSpacing, fill, fillRule, fillOpacity, stroke, strokeOpacity, opacity, true, canvasContext, plugin, runtime);
|
|
1397
|
+
}
|
|
1398
|
+
if (!util.isNil(fill)) {
|
|
1399
|
+
this.drawLetterSpacing(context, object, lines[i], lineMetrics[i], textAlign, linePositionX, linePositionY, letterSpacing, fill, fillRule, fillOpacity, stroke, strokeOpacity, opacity, false, canvasContext, plugin, runtime);
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// Draw text decoration lines
|
|
1404
|
+
if (textDecorationLine && textDecorationLine !== 'none') {
|
|
1405
|
+
this.drawTextDecorations(context, parsedStyle, object, lines, lineHeight, offsetX, y + (dy || 0), canvasContext, plugin, runtime);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
}, {
|
|
1409
|
+
key: "drawLetterSpacing",
|
|
1410
|
+
value: function drawLetterSpacing(context, object, text, lineMetrics, textAlign, x, y, letterSpacing, fill, fillRule, fillOpacity, stroke, strokeOpacity, opacity, isStroke, canvasContext, plugin, runtime) {
|
|
1411
|
+
// letterSpacing of 0 means normal, render all texts directly
|
|
1412
|
+
if (letterSpacing === 0) {
|
|
1413
|
+
if (isStroke) {
|
|
1414
|
+
this.strokeText(context, object, text, x, y, stroke, strokeOpacity, canvasContext, plugin, runtime);
|
|
1415
|
+
} else {
|
|
1416
|
+
this.fillText(context, object, text, x, y, fill, fillRule, fillOpacity, opacity, canvasContext, plugin, runtime);
|
|
1417
|
+
}
|
|
1418
|
+
return;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// draw text using left align
|
|
1422
|
+
var currentTextAlign = context.textAlign;
|
|
1423
|
+
context.textAlign = 'left';
|
|
1424
|
+
var currentPosition = x;
|
|
1425
|
+
if (textAlign === 'center' || textAlign === 'middle') {
|
|
1426
|
+
currentPosition = x - lineMetrics.width / 2;
|
|
1427
|
+
} else if (textAlign === 'right' || textAlign === 'end') {
|
|
1428
|
+
currentPosition = x - lineMetrics.width;
|
|
1429
|
+
}
|
|
1430
|
+
var stringArray = Array.from(text);
|
|
1431
|
+
var previousWidth = context.measureText(text).width;
|
|
1432
|
+
var currentWidth = 0;
|
|
1433
|
+
for (var i = 0; i < stringArray.length; ++i) {
|
|
1434
|
+
var currentChar = stringArray[i];
|
|
1435
|
+
if (isStroke) {
|
|
1436
|
+
this.strokeText(context, object, currentChar, currentPosition, y, stroke, strokeOpacity, canvasContext, plugin, runtime);
|
|
1437
|
+
} else {
|
|
1438
|
+
this.fillText(context, object, currentChar, currentPosition, y, fill, fillRule, fillOpacity, opacity, canvasContext, plugin, runtime);
|
|
1439
|
+
}
|
|
1440
|
+
currentWidth = context.measureText(text.substring(i + 1)).width;
|
|
1441
|
+
currentPosition += previousWidth - currentWidth + letterSpacing;
|
|
1442
|
+
previousWidth = currentWidth;
|
|
1443
|
+
}
|
|
1444
|
+
context.textAlign = currentTextAlign;
|
|
1445
|
+
}
|
|
1446
|
+
}, {
|
|
1447
|
+
key: "fillText",
|
|
1448
|
+
value: function fillText(context, object, text, x, y, fill, fillRule, fillOpacity, opacity, canvasContext, plugin, runtime) {
|
|
1449
|
+
applyFill(context, object, fill, fillRule, canvasContext, plugin, runtime, this.imagePool, true);
|
|
1450
|
+
var currentGlobalAlpha;
|
|
1451
|
+
var applyOpacity = !util.isNil(fillOpacity) && fillOpacity !== 1;
|
|
1452
|
+
if (applyOpacity) {
|
|
1453
|
+
currentGlobalAlpha = context.globalAlpha;
|
|
1454
|
+
context.globalAlpha = fillOpacity * opacity;
|
|
1455
|
+
}
|
|
1456
|
+
context.fillText(text, x, y);
|
|
1457
|
+
if (applyOpacity) {
|
|
1458
|
+
context.globalAlpha = currentGlobalAlpha;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
}, {
|
|
1462
|
+
key: "strokeText",
|
|
1463
|
+
value: function strokeText(context, object, text, x, y, stroke, strokeOpacity, canvasContext, plugin, runtime) {
|
|
1464
|
+
applyStroke(context, object, stroke, canvasContext, plugin, runtime, this.imagePool, true);
|
|
1465
|
+
var currentGlobalAlpha;
|
|
1466
|
+
var applyOpacity = !util.isNil(strokeOpacity) && strokeOpacity !== 1;
|
|
1467
|
+
if (applyOpacity) {
|
|
1468
|
+
currentGlobalAlpha = context.globalAlpha;
|
|
1469
|
+
context.globalAlpha = strokeOpacity;
|
|
1470
|
+
}
|
|
1471
|
+
context.strokeText(text, x, y);
|
|
1472
|
+
if (applyOpacity) {
|
|
1473
|
+
context.globalAlpha = currentGlobalAlpha;
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/**
|
|
1478
|
+
* Draw text decoration lines (underline, overline, line-through)
|
|
1479
|
+
*/
|
|
1480
|
+
}, {
|
|
1481
|
+
key: "drawTextDecorations",
|
|
1482
|
+
value: function drawTextDecorations(context, parsedStyle, object, lines, lineHeight, offsetX, baseY, canvasContext, plugin, runtime) {
|
|
1483
|
+
var textDecorationLine = parsedStyle.textDecorationLine,
|
|
1484
|
+
textDecorationColor = parsedStyle.textDecorationColor,
|
|
1485
|
+
textDecorationStyle = parsedStyle.textDecorationStyle,
|
|
1486
|
+
_parsedStyle$textDeco = parsedStyle.textDecorationThickness,
|
|
1487
|
+
textDecorationThickness = _parsedStyle$textDeco === void 0 ? 1 : _parsedStyle$textDeco,
|
|
1488
|
+
_parsedStyle$textAlig2 = parsedStyle.textAlign,
|
|
1489
|
+
textAlign = _parsedStyle$textAlig2 === void 0 ? 'start' : _parsedStyle$textAlig2,
|
|
1490
|
+
_parsedStyle$lineWidt2 = parsedStyle.lineWidth,
|
|
1491
|
+
lineWidth = _parsedStyle$lineWidt2 === void 0 ? 1 : _parsedStyle$lineWidt2,
|
|
1492
|
+
metrics = parsedStyle.metrics;
|
|
1493
|
+
if (!textDecorationLine || textDecorationLine === 'none') {
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
var lineMetrics = metrics.lineMetrics;
|
|
1497
|
+
var decorations = textDecorationLine.split(' ');
|
|
1498
|
+
|
|
1499
|
+
// Set decoration style
|
|
1500
|
+
context.lineWidth = textDecorationThickness;
|
|
1501
|
+
if (textDecorationColor) {
|
|
1502
|
+
context.strokeStyle = "rgba(".concat(textDecorationColor.r, ", ").concat(textDecorationColor.g, ", ").concat(textDecorationColor.b, ", ").concat(textDecorationColor.alpha, ")");
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
// Set line style based on textDecorationStyle
|
|
1506
|
+
switch (textDecorationStyle) {
|
|
1507
|
+
case 'dashed':
|
|
1508
|
+
context.setLineDash([5, 5]);
|
|
1509
|
+
break;
|
|
1510
|
+
case 'dotted':
|
|
1511
|
+
context.setLineDash([2, 2]);
|
|
1512
|
+
break;
|
|
1513
|
+
case 'wavy':
|
|
1514
|
+
// For wavy, we'll use a simple approximation with bezier curves
|
|
1515
|
+
// A full implementation would be more complex
|
|
1516
|
+
context.setLineDash([]);
|
|
1517
|
+
break;
|
|
1518
|
+
default:
|
|
1519
|
+
context.setLineDash([]);
|
|
1520
|
+
break;
|
|
1521
|
+
}
|
|
1522
|
+
var linePositionY = baseY;
|
|
1523
|
+
|
|
1524
|
+
// Adjust for text baseline
|
|
1525
|
+
var _parsedStyle$textBase2 = parsedStyle.textBaseline,
|
|
1526
|
+
textBaseline = _parsedStyle$textBase2 === void 0 ? 'alphabetic' : _parsedStyle$textBase2;
|
|
1527
|
+
if (textBaseline === 'middle') {
|
|
1528
|
+
linePositionY += -metrics.height / 2 - lineHeight / 2;
|
|
1529
|
+
} else if (textBaseline === 'bottom' || textBaseline === 'alphabetic' || textBaseline === 'ideographic') {
|
|
1530
|
+
linePositionY += -metrics.height;
|
|
1531
|
+
} else if (textBaseline === 'top' || textBaseline === 'hanging') {
|
|
1532
|
+
linePositionY += -lineHeight;
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
// Draw each decoration for each line
|
|
1536
|
+
for (var i = 0; i < lines.length; i++) {
|
|
1537
|
+
linePositionY += lineHeight;
|
|
1538
|
+
var lineMetric = lineMetrics[i];
|
|
1539
|
+
if (!lineMetric) continue;
|
|
1540
|
+
var lineWidthOffset = lineWidth / 2;
|
|
1541
|
+
var startX = offsetX;
|
|
1542
|
+
var endX = offsetX + lineMetric.width;
|
|
1543
|
+
|
|
1544
|
+
// Adjust for text alignment
|
|
1545
|
+
if (textAlign === 'center' || textAlign === 'middle') {
|
|
1546
|
+
startX = offsetX - lineMetric.width / 2;
|
|
1547
|
+
endX = offsetX + lineMetric.width / 2;
|
|
1548
|
+
} else if (textAlign === 'right' || textAlign === 'end') {
|
|
1549
|
+
startX = offsetX - lineMetric.width;
|
|
1550
|
+
endX = offsetX;
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
// Add lineWidth offset
|
|
1554
|
+
startX += lineWidthOffset;
|
|
1555
|
+
endX += lineWidthOffset;
|
|
1556
|
+
|
|
1557
|
+
// Draw each type of decoration
|
|
1558
|
+
var _iterator = _createForOfIteratorHelper(decorations),
|
|
1559
|
+
_step;
|
|
1560
|
+
try {
|
|
1561
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1562
|
+
var decoration = _step.value;
|
|
1563
|
+
var decorationY = linePositionY;
|
|
1564
|
+
switch (decoration) {
|
|
1565
|
+
case 'underline':
|
|
1566
|
+
decorationY += 2; // Small offset below baseline
|
|
1567
|
+
break;
|
|
1568
|
+
case 'overline':
|
|
1569
|
+
decorationY -= lineHeight - 2; // Small offset above line
|
|
1570
|
+
break;
|
|
1571
|
+
case 'line-through':
|
|
1572
|
+
decorationY -= lineHeight / 2; // Middle of line
|
|
1573
|
+
break;
|
|
1574
|
+
default:
|
|
1575
|
+
continue;
|
|
1576
|
+
// Unknown decoration type
|
|
1577
|
+
}
|
|
1578
|
+
if (textDecorationStyle === 'wavy') {
|
|
1579
|
+
this.drawWavyLine(context, startX, endX, decorationY);
|
|
1580
|
+
} else {
|
|
1581
|
+
context.beginPath();
|
|
1582
|
+
context.moveTo(startX, decorationY);
|
|
1583
|
+
context.lineTo(endX, decorationY);
|
|
1584
|
+
context.stroke();
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
} catch (err) {
|
|
1588
|
+
_iterator.e(err);
|
|
1589
|
+
} finally {
|
|
1590
|
+
_iterator.f();
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
// Reset line dash
|
|
1595
|
+
context.setLineDash([]);
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
/**
|
|
1599
|
+
* Draw a wavy line as an approximation of the wavy text decoration style
|
|
1600
|
+
*/
|
|
1601
|
+
}, {
|
|
1602
|
+
key: "drawWavyLine",
|
|
1603
|
+
value: function drawWavyLine(context, startX, endX, y) {
|
|
1604
|
+
var amplitude = 2; // Height of the wave
|
|
1605
|
+
var wavelength = 10; // Length of one wave cycle
|
|
1606
|
+
var numWaves = Math.floor((endX - startX) / wavelength);
|
|
1607
|
+
context.beginPath();
|
|
1608
|
+
context.moveTo(startX, y);
|
|
1609
|
+
for (var i = 0; i < numWaves; i++) {
|
|
1610
|
+
var x1 = startX + i * wavelength + wavelength / 4;
|
|
1611
|
+
var y1 = y + amplitude;
|
|
1612
|
+
var x2 = startX + i * wavelength + wavelength / 2;
|
|
1613
|
+
var y2 = y;
|
|
1614
|
+
context.quadraticCurveTo(x1, y1, x2, y2);
|
|
1615
|
+
var x3 = startX + i * wavelength + wavelength * 3 / 4;
|
|
1616
|
+
var y3 = y - amplitude;
|
|
1617
|
+
var x4 = startX + (i + 1) * wavelength;
|
|
1618
|
+
var y4 = y;
|
|
1619
|
+
context.quadraticCurveTo(x3, y3, x4, y4);
|
|
1620
|
+
}
|
|
1621
|
+
context.stroke();
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
// ---
|
|
1625
|
+
}, {
|
|
1626
|
+
key: "drawToContext",
|
|
1627
|
+
value: function drawToContext(context, object, renderState, plugin, runtime) {
|
|
1628
|
+
this.render(context, object.parsedStyle, object, object.ownerDocument.defaultView.context, plugin, runtime);
|
|
1629
|
+
}
|
|
1630
|
+
}]);
|
|
1631
|
+
}(DefaultRenderer);
|
|
1632
|
+
|
|
1633
|
+
var Plugin$2 = /*#__PURE__*/function (_AbstractRendererPlug) {
|
|
1634
|
+
function Plugin() {
|
|
1635
|
+
var _this;
|
|
1636
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
1637
|
+
_classCallCheck(this, Plugin);
|
|
1638
|
+
_this = _callSuper(this, Plugin);
|
|
1639
|
+
_this.name = 'canvas-renderer';
|
|
1640
|
+
_this.options = options;
|
|
1641
|
+
return _this;
|
|
1642
|
+
}
|
|
1643
|
+
_inherits(Plugin, _AbstractRendererPlug);
|
|
1644
|
+
return _createClass(Plugin, [{
|
|
1645
|
+
key: "init",
|
|
1646
|
+
value: function init() {
|
|
1647
|
+
var _defaultStyleRenderer;
|
|
1648
|
+
var canvasRendererPluginOptions = _objectSpread({
|
|
1649
|
+
dirtyObjectNumThreshold: 500,
|
|
1650
|
+
dirtyObjectRatioThreshold: 0.8
|
|
1651
|
+
}, this.options);
|
|
1652
|
+
|
|
1653
|
+
// @ts-ignore
|
|
1654
|
+
var imagePool = this.context.imagePool;
|
|
1655
|
+
var defaultRenderer = new DefaultRenderer(imagePool);
|
|
1656
|
+
var defaultStyleRendererFactory = (_defaultStyleRenderer = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defaultStyleRenderer, gLite.Shape.CIRCLE, defaultRenderer), gLite.Shape.ELLIPSE, defaultRenderer), gLite.Shape.RECT, defaultRenderer), gLite.Shape.IMAGE, new ImageRenderer(imagePool)), gLite.Shape.TEXT, new TextRenderer(imagePool)), gLite.Shape.LINE, defaultRenderer), gLite.Shape.POLYLINE, defaultRenderer), gLite.Shape.POLYGON, defaultRenderer), gLite.Shape.PATH, defaultRenderer), gLite.Shape.GROUP, undefined), _defineProperty(_defineProperty(_defineProperty(_defaultStyleRenderer, gLite.Shape.HTML, undefined), gLite.Shape.MESH, undefined), gLite.Shape.FRAGMENT, undefined));
|
|
1657
|
+
this.context.defaultStyleRendererFactory = defaultStyleRendererFactory;
|
|
1658
|
+
this.context.styleRendererFactory = defaultStyleRendererFactory;
|
|
1659
|
+
this.addRenderingPlugin(new CanvasRendererPlugin(canvasRendererPluginOptions));
|
|
1660
|
+
}
|
|
1661
|
+
}, {
|
|
1662
|
+
key: "destroy",
|
|
1663
|
+
value: function destroy() {
|
|
1664
|
+
this.removeAllRenderingPlugins();
|
|
1665
|
+
delete this.context.defaultStyleRendererFactory;
|
|
1666
|
+
delete this.context.styleRendererFactory;
|
|
1667
|
+
}
|
|
1668
|
+
}]);
|
|
1669
|
+
}(gLite.AbstractRendererPlugin);
|
|
1670
|
+
|
|
1671
|
+
var index$2 = /*#__PURE__*/Object.freeze({
|
|
1672
|
+
__proto__: null,
|
|
1673
|
+
CircleRenderer: DefaultRenderer,
|
|
1674
|
+
DefaultRenderer: DefaultRenderer,
|
|
1675
|
+
EllipseRenderer: DefaultRenderer,
|
|
1676
|
+
ImageRenderer: ImageRenderer,
|
|
1677
|
+
LineRenderer: DefaultRenderer,
|
|
1678
|
+
PathRenderer: DefaultRenderer,
|
|
1679
|
+
Plugin: Plugin$2,
|
|
1680
|
+
PolygonRenderer: DefaultRenderer,
|
|
1681
|
+
PolylineRenderer: DefaultRenderer,
|
|
1682
|
+
RectRenderer: DefaultRenderer,
|
|
1683
|
+
TextRenderer: TextRenderer
|
|
1684
|
+
});
|
|
1685
|
+
|
|
1686
|
+
var tmpVec3a = glMatrix.vec3.create();
|
|
1687
|
+
var tmpVec3b = glMatrix.vec3.create();
|
|
1688
|
+
var tmpVec3c = glMatrix.vec3.create();
|
|
1689
|
+
var tmpMat4 = glMatrix.mat4.create();
|
|
1690
|
+
/**
|
|
1691
|
+
* pick shape(s) with Mouse/Touch event
|
|
1692
|
+
*
|
|
1693
|
+
* 1. find AABB with r-tree
|
|
1694
|
+
* 2. do math calculation with geometry in an accurate way
|
|
1695
|
+
*/
|
|
1696
|
+
var CanvasPickerPlugin = /*#__PURE__*/function () {
|
|
1697
|
+
function CanvasPickerPlugin() {
|
|
1698
|
+
var _this = this;
|
|
1699
|
+
_classCallCheck(this, CanvasPickerPlugin);
|
|
1700
|
+
this.isHit = function (displayObject, position, worldTransform, isClipPath) {
|
|
1701
|
+
// use picker for current shape's type
|
|
1702
|
+
var pick = _this.context.pointInPathPickerFactory[displayObject.nodeName];
|
|
1703
|
+
if (pick) {
|
|
1704
|
+
// invert with world matrix
|
|
1705
|
+
var invertWorldMat = glMatrix.mat4.invert(tmpMat4, worldTransform);
|
|
1706
|
+
|
|
1707
|
+
// transform client position to local space, do picking in local space
|
|
1708
|
+
var localPosition = glMatrix.vec3.transformMat4(tmpVec3b, glMatrix.vec3.set(tmpVec3c, position[0], position[1], 0), invertWorldMat);
|
|
1709
|
+
if (pick(displayObject, new gLite.Point(localPosition[0], localPosition[1]), isClipPath, _this.isPointInPath, _this.context, _this.runtime)) {
|
|
1710
|
+
return true;
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
return false;
|
|
1714
|
+
};
|
|
1715
|
+
/**
|
|
1716
|
+
* use native picking method
|
|
1717
|
+
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/isPointInPath
|
|
1718
|
+
*/
|
|
1719
|
+
this.isPointInPath = function (displayObject, position) {
|
|
1720
|
+
var context = _this.runtime.offscreenCanvasCreator.getOrCreateContext(_this.context.config.offscreenCanvas);
|
|
1721
|
+
var generatePath = _this.context.pathGeneratorFactory[displayObject.nodeName];
|
|
1722
|
+
if (generatePath) {
|
|
1723
|
+
context.beginPath();
|
|
1724
|
+
generatePath(context, displayObject.parsedStyle);
|
|
1725
|
+
context.closePath();
|
|
1726
|
+
}
|
|
1727
|
+
return context.isPointInPath(position.x, position.y);
|
|
1728
|
+
};
|
|
1729
|
+
}
|
|
1730
|
+
return _createClass(CanvasPickerPlugin, [{
|
|
1731
|
+
key: "apply",
|
|
1732
|
+
value: function apply(context, runtime) {
|
|
1733
|
+
var _renderingContext$roo,
|
|
1734
|
+
_this2 = this;
|
|
1735
|
+
var renderingService = context.renderingService,
|
|
1736
|
+
renderingContext = context.renderingContext;
|
|
1737
|
+
this.context = context;
|
|
1738
|
+
this.runtime = runtime;
|
|
1739
|
+
var document = (_renderingContext$roo = renderingContext.root) === null || _renderingContext$roo === void 0 ? void 0 : _renderingContext$roo.ownerDocument;
|
|
1740
|
+
renderingService.hooks.pick.tapPromise(CanvasPickerPlugin.tag, /*#__PURE__*/function () {
|
|
1741
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(result) {
|
|
1742
|
+
return _regeneratorRuntime().wrap(function (_context) {
|
|
1743
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1744
|
+
case 0:
|
|
1745
|
+
return _context.abrupt("return", _this2.pick(document, result));
|
|
1746
|
+
case 1:
|
|
1747
|
+
case "end":
|
|
1748
|
+
return _context.stop();
|
|
1749
|
+
}
|
|
1750
|
+
}, _callee);
|
|
1751
|
+
}));
|
|
1752
|
+
return function (_x) {
|
|
1753
|
+
return _ref.apply(this, arguments);
|
|
1754
|
+
};
|
|
1755
|
+
}());
|
|
1756
|
+
renderingService.hooks.pickSync.tap(CanvasPickerPlugin.tag, function (result) {
|
|
1757
|
+
return _this2.pick(document, result);
|
|
1758
|
+
});
|
|
1759
|
+
}
|
|
1760
|
+
}, {
|
|
1761
|
+
key: "pick",
|
|
1762
|
+
value: function pick(document, result) {
|
|
1763
|
+
var topmost = result.topmost,
|
|
1764
|
+
_result$position = result.position,
|
|
1765
|
+
x = _result$position.x,
|
|
1766
|
+
y = _result$position.y;
|
|
1767
|
+
|
|
1768
|
+
// position in world space
|
|
1769
|
+
var position = glMatrix.vec3.set(tmpVec3a, x, y, 0);
|
|
1770
|
+
|
|
1771
|
+
// query by AABB
|
|
1772
|
+
var hitTestList = document.elementsFromBBox(position[0], position[1], position[0], position[1]);
|
|
1773
|
+
|
|
1774
|
+
// test with clip path & origin shape
|
|
1775
|
+
// @see https://github.com/antvis/g/issues/1064
|
|
1776
|
+
var pickedDisplayObjects = [];
|
|
1777
|
+
var _iterator = _createForOfIteratorHelper(hitTestList),
|
|
1778
|
+
_step;
|
|
1779
|
+
try {
|
|
1780
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1781
|
+
var _displayObject = _step.value;
|
|
1782
|
+
var worldTransform = _displayObject.getWorldTransform();
|
|
1783
|
+
var isHitOriginShape = this.isHit(_displayObject, position, worldTransform, false);
|
|
1784
|
+
if (isHitOriginShape) {
|
|
1785
|
+
// should look up in the ancestor node
|
|
1786
|
+
var clipped = gLite.findClosestClipPathTarget(_displayObject);
|
|
1787
|
+
if (clipped) {
|
|
1788
|
+
var clipPath = clipped.parsedStyle.clipPath;
|
|
1789
|
+
var isHitClipPath = this.isHit(clipPath, position, clipPath.getWorldTransform(), true);
|
|
1790
|
+
if (isHitClipPath) {
|
|
1791
|
+
if (topmost) {
|
|
1792
|
+
result.picked = [_displayObject];
|
|
1793
|
+
return result;
|
|
1794
|
+
}
|
|
1795
|
+
pickedDisplayObjects.push(_displayObject);
|
|
1796
|
+
}
|
|
1797
|
+
} else {
|
|
1798
|
+
if (topmost) {
|
|
1799
|
+
result.picked = [_displayObject];
|
|
1800
|
+
return result;
|
|
1801
|
+
}
|
|
1802
|
+
pickedDisplayObjects.push(_displayObject);
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
} catch (err) {
|
|
1807
|
+
_iterator.e(err);
|
|
1808
|
+
} finally {
|
|
1809
|
+
_iterator.f();
|
|
1810
|
+
}
|
|
1811
|
+
result.picked = pickedDisplayObjects;
|
|
1812
|
+
return result;
|
|
1813
|
+
}
|
|
1814
|
+
}]);
|
|
1815
|
+
}();
|
|
1816
|
+
CanvasPickerPlugin.tag = 'CanvasPicker';
|
|
1817
|
+
|
|
1818
|
+
function isPointInPath$8(displayObject, position, isClipPath) {
|
|
1819
|
+
var _ref = displayObject.parsedStyle,
|
|
1820
|
+
_ref$cx = _ref.cx,
|
|
1821
|
+
cx = _ref$cx === void 0 ? 0 : _ref$cx,
|
|
1822
|
+
_ref$cy = _ref.cy,
|
|
1823
|
+
cy = _ref$cy === void 0 ? 0 : _ref$cy,
|
|
1824
|
+
r = _ref.r,
|
|
1825
|
+
fill = _ref.fill,
|
|
1826
|
+
stroke = _ref.stroke,
|
|
1827
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
1828
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
1829
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
1830
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
1831
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
1832
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents;
|
|
1833
|
+
var halfLineWidth = (lineWidth + increasedLineWidthForHitTesting) / 2;
|
|
1834
|
+
var absDistance = gMath.distance(cx, cy, position.x, position.y);
|
|
1835
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
|
|
1836
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
1837
|
+
hasFill = _isFillOrStrokeAffect2[0],
|
|
1838
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
1839
|
+
if (hasFill && hasStroke || isClipPath) {
|
|
1840
|
+
return absDistance <= r + halfLineWidth;
|
|
1841
|
+
}
|
|
1842
|
+
if (hasFill) {
|
|
1843
|
+
return absDistance <= r;
|
|
1844
|
+
}
|
|
1845
|
+
if (hasStroke) {
|
|
1846
|
+
return absDistance >= r - halfLineWidth && absDistance <= r + halfLineWidth;
|
|
1847
|
+
}
|
|
1848
|
+
return false;
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
function ellipseDistance(squareX, squareY, rx, ry) {
|
|
1852
|
+
return squareX / (rx * rx) + squareY / (ry * ry);
|
|
1853
|
+
}
|
|
1854
|
+
function isPointInPath$7(displayObject, position, isClipPath) {
|
|
1855
|
+
var _ref = displayObject.parsedStyle,
|
|
1856
|
+
_ref$cx = _ref.cx,
|
|
1857
|
+
cx = _ref$cx === void 0 ? 0 : _ref$cx,
|
|
1858
|
+
_ref$cy = _ref.cy,
|
|
1859
|
+
cy = _ref$cy === void 0 ? 0 : _ref$cy,
|
|
1860
|
+
rx = _ref.rx,
|
|
1861
|
+
ry = _ref.ry,
|
|
1862
|
+
fill = _ref.fill,
|
|
1863
|
+
stroke = _ref.stroke,
|
|
1864
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
1865
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
1866
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
1867
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
1868
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
1869
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents;
|
|
1870
|
+
var x = position.x,
|
|
1871
|
+
y = position.y;
|
|
1872
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
|
|
1873
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
1874
|
+
hasFill = _isFillOrStrokeAffect2[0],
|
|
1875
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
1876
|
+
var halfLineWith = (lineWidth + increasedLineWidthForHitTesting) / 2;
|
|
1877
|
+
var squareX = (x - cx) * (x - cx);
|
|
1878
|
+
var squareY = (y - cy) * (y - cy);
|
|
1879
|
+
// 使用椭圆的公式: x*x/rx*rx + y*y/ry*ry = 1;
|
|
1880
|
+
if (hasFill && hasStroke || isClipPath) {
|
|
1881
|
+
return ellipseDistance(squareX, squareY, rx + halfLineWith, ry + halfLineWith) <= 1;
|
|
1882
|
+
}
|
|
1883
|
+
if (hasFill) {
|
|
1884
|
+
return ellipseDistance(squareX, squareY, rx, ry) <= 1;
|
|
1885
|
+
}
|
|
1886
|
+
if (hasStroke) {
|
|
1887
|
+
return ellipseDistance(squareX, squareY, rx - halfLineWith, ry - halfLineWith) >= 1 && ellipseDistance(squareX, squareY, rx + halfLineWith, ry + halfLineWith) <= 1;
|
|
1888
|
+
}
|
|
1889
|
+
return false;
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
function inBox(minX, minY, width, height, x, y) {
|
|
1893
|
+
return x >= minX && x <= minX + width && y >= minY && y <= minY + height;
|
|
1894
|
+
}
|
|
1895
|
+
function inRect(minX, minY, width, height, lineWidth, x, y) {
|
|
1896
|
+
var halfWidth = lineWidth / 2;
|
|
1897
|
+
// 将四个边看做矩形来检测,比边的检测算法要快
|
|
1898
|
+
return inBox(minX - halfWidth, minY - halfWidth, width, lineWidth, x, y) ||
|
|
1899
|
+
// 上边
|
|
1900
|
+
inBox(minX + width - halfWidth, minY - halfWidth, lineWidth, height, x, y) ||
|
|
1901
|
+
// 右边
|
|
1902
|
+
inBox(minX + halfWidth, minY + height - halfWidth, width, lineWidth, x, y) ||
|
|
1903
|
+
// 下边
|
|
1904
|
+
inBox(minX - halfWidth, minY + halfWidth, lineWidth, height, x, y); // 左边
|
|
1905
|
+
}
|
|
1906
|
+
function inArc(cx, cy, r, startAngle, endAngle, lineWidth, x, y) {
|
|
1907
|
+
var angle = (Math.atan2(y - cy, x - cx) + Math.PI * 2) % (Math.PI * 2); // 转换到 0 - 2 * Math.PI 之间
|
|
1908
|
+
// if (angle < startAngle || angle > endAngle) {
|
|
1909
|
+
// return false;
|
|
1910
|
+
// }
|
|
1911
|
+
var point = {
|
|
1912
|
+
x: cx + r * Math.cos(angle),
|
|
1913
|
+
y: cy + r * Math.sin(angle)
|
|
1914
|
+
};
|
|
1915
|
+
return gMath.distance(point.x, point.y, x, y) <= lineWidth / 2;
|
|
1916
|
+
}
|
|
1917
|
+
function inLine(x1, y1, x2, y2, lineWidth, x, y) {
|
|
1918
|
+
var minX = Math.min(x1, x2);
|
|
1919
|
+
var maxX = Math.max(x1, x2);
|
|
1920
|
+
var minY = Math.min(y1, y2);
|
|
1921
|
+
var maxY = Math.max(y1, y2);
|
|
1922
|
+
var halfWidth = lineWidth / 2;
|
|
1923
|
+
// 因为目前的方案是计算点到直线的距离,而有可能会在延长线上,所以要先判断是否在包围盒内
|
|
1924
|
+
// 这种方案会在水平或者竖直的情况下载线的延长线上有半 lineWidth 的误差
|
|
1925
|
+
if (!(x >= minX - halfWidth && x <= maxX + halfWidth && y >= minY - halfWidth && y <= maxY + halfWidth)) {
|
|
1926
|
+
return false;
|
|
1927
|
+
}
|
|
1928
|
+
// 因为已经计算了包围盒,所以仅需要计算到直线的距离即可,可以显著提升性能
|
|
1929
|
+
return gMath.linePointToLine(x1, y1, x2, y2, x, y) <= lineWidth / 2;
|
|
1930
|
+
}
|
|
1931
|
+
function inPolyline(points, lineWidth, x, y, isClose) {
|
|
1932
|
+
var count = points.length;
|
|
1933
|
+
if (count < 2) {
|
|
1934
|
+
return false;
|
|
1935
|
+
}
|
|
1936
|
+
for (var i = 0; i < count - 1; i++) {
|
|
1937
|
+
var x1 = points[i][0];
|
|
1938
|
+
var y1 = points[i][1];
|
|
1939
|
+
var x2 = points[i + 1][0];
|
|
1940
|
+
var y2 = points[i + 1][1];
|
|
1941
|
+
if (inLine(x1, y1, x2, y2, lineWidth, x, y)) {
|
|
1942
|
+
return true;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
// 如果封闭,则计算起始点和结束点的边
|
|
1947
|
+
if (isClose) {
|
|
1948
|
+
var first = points[0];
|
|
1949
|
+
var last = points[count - 1];
|
|
1950
|
+
if (inLine(first[0], first[1], last[0], last[1], lineWidth, x, y)) {
|
|
1951
|
+
return true;
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
return false;
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
// 多边形的射线检测,参考:https://blog.csdn.net/WilliamSun0122/article/details/77994526
|
|
1958
|
+
var tolerance = 1e-6;
|
|
1959
|
+
// 三态函数,判断两个double在eps精度下的大小关系
|
|
1960
|
+
function dcmp(x) {
|
|
1961
|
+
if (Math.abs(x) < tolerance) {
|
|
1962
|
+
return 0;
|
|
1963
|
+
}
|
|
1964
|
+
return x < 0 ? -1 : 1;
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
// 判断点Q是否在p1和p2的线段上
|
|
1968
|
+
function onSegment(p1, p2, q) {
|
|
1969
|
+
if ((q[0] - p1[0]) * (p2[1] - p1[1]) === (p2[0] - p1[0]) * (q[1] - p1[1]) && Math.min(p1[0], p2[0]) <= q[0] && q[0] <= Math.max(p1[0], p2[0]) && Math.min(p1[1], p2[1]) <= q[1] && q[1] <= Math.max(p1[1], p2[1])) {
|
|
1970
|
+
return true;
|
|
1971
|
+
}
|
|
1972
|
+
return false;
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
// 判断点P在多边形内-射线法
|
|
1976
|
+
function inPolygon(points, x, y) {
|
|
1977
|
+
var isHit = false;
|
|
1978
|
+
var n = points.length;
|
|
1979
|
+
if (n <= 2) {
|
|
1980
|
+
// svg 中点小于 3 个时,不显示,也无法被拾取
|
|
1981
|
+
return false;
|
|
1982
|
+
}
|
|
1983
|
+
for (var i = 0; i < n; i++) {
|
|
1984
|
+
var p1 = points[i];
|
|
1985
|
+
var p2 = points[(i + 1) % n];
|
|
1986
|
+
if (onSegment(p1, p2, [x, y])) {
|
|
1987
|
+
// 点在多边形一条边上
|
|
1988
|
+
return true;
|
|
1989
|
+
}
|
|
1990
|
+
// 前一个判断min(p1[1],p2[1])<P.y<=max(p1[1],p2[1])
|
|
1991
|
+
// 后一个判断被测点 在 射线与边交点 的左边
|
|
1992
|
+
if (dcmp(p1[1] - y) > 0 !== dcmp(p2[1] - y) > 0 && dcmp(x - (y - p1[1]) * (p1[0] - p2[0]) / (p1[1] - p2[1]) - p1[0]) < 0) {
|
|
1993
|
+
isHit = !isHit;
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
return isHit;
|
|
1997
|
+
}
|
|
1998
|
+
function inPolygons(polygons, x, y) {
|
|
1999
|
+
var isHit = false;
|
|
2000
|
+
for (var i = 0; i < polygons.length; i++) {
|
|
2001
|
+
var points = polygons[i];
|
|
2002
|
+
isHit = inPolygon(points, x, y);
|
|
2003
|
+
if (isHit) {
|
|
2004
|
+
break;
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
return isHit;
|
|
2008
|
+
}
|
|
2009
|
+
|
|
2010
|
+
function isPointInPath$6(displayObject, position, isClipPath) {
|
|
2011
|
+
var _ref = displayObject.parsedStyle,
|
|
2012
|
+
x1 = _ref.x1,
|
|
2013
|
+
y1 = _ref.y1,
|
|
2014
|
+
x2 = _ref.x2,
|
|
2015
|
+
y2 = _ref.y2,
|
|
2016
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
2017
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
2018
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
2019
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
2020
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
2021
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents,
|
|
2022
|
+
fill = _ref.fill,
|
|
2023
|
+
stroke = _ref.stroke;
|
|
2024
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
|
|
2025
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
2026
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
2027
|
+
if (!hasStroke && !isClipPath || !lineWidth) {
|
|
2028
|
+
return false;
|
|
2029
|
+
}
|
|
2030
|
+
return inLine(x1, y1, x2, y2, lineWidth + increasedLineWidthForHitTesting, position.x, position.y);
|
|
2031
|
+
}
|
|
2032
|
+
|
|
2033
|
+
// TODO: replace it with method in @antv/util
|
|
2034
|
+
function isPointInStroke(segments, lineWidth, px, py, length) {
|
|
2035
|
+
var isHit = false;
|
|
2036
|
+
var halfWidth = lineWidth / 2;
|
|
2037
|
+
for (var i = 0; i < segments.length; i++) {
|
|
2038
|
+
var segment = segments[i];
|
|
2039
|
+
var currentPoint = segment.currentPoint,
|
|
2040
|
+
params = segment.params,
|
|
2041
|
+
prePoint = segment.prePoint,
|
|
2042
|
+
box = segment.box;
|
|
2043
|
+
// 如果在前面已经生成过包围盒,直接按照包围盒计算
|
|
2044
|
+
if (box && !inBox(box.x - halfWidth, box.y - halfWidth, box.width + lineWidth, box.height + lineWidth, px, py)) {
|
|
2045
|
+
continue;
|
|
2046
|
+
}
|
|
2047
|
+
switch (segment.command) {
|
|
2048
|
+
// L 和 Z 都是直线, M 不进行拾取
|
|
2049
|
+
case 'L':
|
|
2050
|
+
case 'Z':
|
|
2051
|
+
isHit = inLine(prePoint[0], prePoint[1], currentPoint[0], currentPoint[1], lineWidth, px, py);
|
|
2052
|
+
if (isHit) {
|
|
2053
|
+
return true;
|
|
2054
|
+
}
|
|
2055
|
+
break;
|
|
2056
|
+
case 'Q':
|
|
2057
|
+
var qDistance = gMath.quadPointDistance(prePoint[0], prePoint[1], params[1], params[2], params[3], params[4], px, py);
|
|
2058
|
+
isHit = qDistance <= lineWidth / 2;
|
|
2059
|
+
if (isHit) {
|
|
2060
|
+
return true;
|
|
2061
|
+
}
|
|
2062
|
+
break;
|
|
2063
|
+
case 'C':
|
|
2064
|
+
var cDistance = gMath.cubicPointDistance(prePoint[0],
|
|
2065
|
+
// 上一段结束位置, 即 C 的起始点
|
|
2066
|
+
prePoint[1], params[1],
|
|
2067
|
+
// 'C' 的参数,1、2 为第一个控制点,3、4 为第二个控制点,5、6 为结束点
|
|
2068
|
+
params[2], params[3], params[4], params[5], params[6], px, py, length);
|
|
2069
|
+
isHit = cDistance <= lineWidth / 2;
|
|
2070
|
+
if (isHit) {
|
|
2071
|
+
return true;
|
|
2072
|
+
}
|
|
2073
|
+
break;
|
|
2074
|
+
case 'A':
|
|
2075
|
+
// cache conversion result
|
|
2076
|
+
if (!segment.cubicParams) {
|
|
2077
|
+
segment.cubicParams = util.arcToCubic(prePoint[0], prePoint[1], params[1], params[2], params[3], params[4], params[5], params[6], params[7], undefined);
|
|
2078
|
+
}
|
|
2079
|
+
var args = segment.cubicParams;
|
|
2080
|
+
|
|
2081
|
+
// fixArc
|
|
2082
|
+
var prePointInCubic = prePoint;
|
|
2083
|
+
for (var _i = 0; _i < args.length; _i += 6) {
|
|
2084
|
+
var _cDistance = gMath.cubicPointDistance(prePointInCubic[0],
|
|
2085
|
+
// 上一段结束位置, 即 C 的起始点
|
|
2086
|
+
prePointInCubic[1], args[_i], args[_i + 1], args[_i + 2], args[_i + 3], args[_i + 4], args[_i + 5], px, py, length);
|
|
2087
|
+
prePointInCubic = [args[_i + 4], args[_i + 5]];
|
|
2088
|
+
isHit = _cDistance <= lineWidth / 2;
|
|
2089
|
+
if (isHit) {
|
|
2090
|
+
return true;
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
break;
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
return isHit;
|
|
2097
|
+
}
|
|
2098
|
+
function isPointInPath$5(displayObject, position, isClipPath, isPointInPath, renderingPluginContext, runtime) {
|
|
2099
|
+
var _ref = displayObject.parsedStyle,
|
|
2100
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
2101
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
2102
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
2103
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
2104
|
+
stroke = _ref.stroke,
|
|
2105
|
+
fill = _ref.fill,
|
|
2106
|
+
d = _ref.d,
|
|
2107
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
2108
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents;
|
|
2109
|
+
var segments = d.segments,
|
|
2110
|
+
hasArc = d.hasArc,
|
|
2111
|
+
polylines = d.polylines,
|
|
2112
|
+
polygons = d.polygons;
|
|
2113
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents,
|
|
2114
|
+
// Only a closed path can be filled.
|
|
2115
|
+
(polygons === null || polygons === void 0 ? void 0 : polygons.length) && fill, stroke),
|
|
2116
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
2117
|
+
hasFill = _isFillOrStrokeAffect2[0],
|
|
2118
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
2119
|
+
var totalLength = gLite.getOrCalculatePathTotalLength(displayObject);
|
|
2120
|
+
var isHit = false;
|
|
2121
|
+
if (hasFill || isClipPath) {
|
|
2122
|
+
if (hasArc) {
|
|
2123
|
+
// 存在曲线时,暂时使用 canvas 的 api 计算,后续可以进行多边形切割
|
|
2124
|
+
isHit = isPointInPath(displayObject, position);
|
|
2125
|
+
} else {
|
|
2126
|
+
// 提取出来的多边形包含闭合的和非闭合的,在这里统一按照多边形处理
|
|
2127
|
+
isHit = inPolygons(polygons, position.x, position.y) || inPolygons(polylines, position.x, position.y);
|
|
2128
|
+
}
|
|
2129
|
+
return isHit;
|
|
2130
|
+
}
|
|
2131
|
+
if (hasStroke || isClipPath) {
|
|
2132
|
+
isHit = isPointInStroke(segments, lineWidth + increasedLineWidthForHitTesting, position.x, position.y, totalLength);
|
|
2133
|
+
}
|
|
2134
|
+
return isHit;
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2137
|
+
function isPointInPath$4(displayObject, position, isClipPath) {
|
|
2138
|
+
var _ref = displayObject.parsedStyle,
|
|
2139
|
+
stroke = _ref.stroke,
|
|
2140
|
+
fill = _ref.fill,
|
|
2141
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
2142
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
2143
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
2144
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
2145
|
+
points = _ref.points,
|
|
2146
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
2147
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents;
|
|
2148
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
|
|
2149
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
2150
|
+
hasFill = _isFillOrStrokeAffect2[0],
|
|
2151
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
2152
|
+
var isHit = false;
|
|
2153
|
+
if (hasStroke || isClipPath) {
|
|
2154
|
+
isHit = inPolyline(points.points, lineWidth + increasedLineWidthForHitTesting, position.x, position.y, true);
|
|
2155
|
+
}
|
|
2156
|
+
if (!isHit && (hasFill || isClipPath)) {
|
|
2157
|
+
isHit = inPolygon(points.points, position.x, position.y);
|
|
2158
|
+
}
|
|
2159
|
+
return isHit;
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
function isPointInPath$3(displayObject, position, isClipPath) {
|
|
2163
|
+
var _ref = displayObject.parsedStyle,
|
|
2164
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
2165
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
2166
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
2167
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
2168
|
+
points = _ref.points,
|
|
2169
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
2170
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents,
|
|
2171
|
+
fill = _ref.fill,
|
|
2172
|
+
stroke = _ref.stroke;
|
|
2173
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
|
|
2174
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
2175
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
2176
|
+
if (!hasStroke && !isClipPath || !lineWidth) {
|
|
2177
|
+
return false;
|
|
2178
|
+
}
|
|
2179
|
+
return inPolyline(points.points, lineWidth + increasedLineWidthForHitTesting, position.x, position.y, false);
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
function isPointInPath$2(displayObject, position, isClipPath, isPointInPath, runtime) {
|
|
2183
|
+
var _ref = displayObject.parsedStyle,
|
|
2184
|
+
radius = _ref.radius,
|
|
2185
|
+
fill = _ref.fill,
|
|
2186
|
+
stroke = _ref.stroke,
|
|
2187
|
+
_ref$lineWidth = _ref.lineWidth,
|
|
2188
|
+
lineWidth = _ref$lineWidth === void 0 ? 1 : _ref$lineWidth,
|
|
2189
|
+
_ref$increasedLineWid = _ref.increasedLineWidthForHitTesting,
|
|
2190
|
+
increasedLineWidthForHitTesting = _ref$increasedLineWid === void 0 ? 0 : _ref$increasedLineWid,
|
|
2191
|
+
_ref$x = _ref.x,
|
|
2192
|
+
x = _ref$x === void 0 ? 0 : _ref$x,
|
|
2193
|
+
_ref$y = _ref.y,
|
|
2194
|
+
y = _ref$y === void 0 ? 0 : _ref$y,
|
|
2195
|
+
width = _ref.width,
|
|
2196
|
+
height = _ref.height,
|
|
2197
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
2198
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents;
|
|
2199
|
+
var _isFillOrStrokeAffect = gLite.isFillOrStrokeAffected(pointerEvents, fill, stroke),
|
|
2200
|
+
_isFillOrStrokeAffect2 = _slicedToArray(_isFillOrStrokeAffect, 2),
|
|
2201
|
+
hasFill = _isFillOrStrokeAffect2[0],
|
|
2202
|
+
hasStroke = _isFillOrStrokeAffect2[1];
|
|
2203
|
+
var hasRadius = radius && radius.some(function (r) {
|
|
2204
|
+
return r !== 0;
|
|
2205
|
+
});
|
|
2206
|
+
var lineWidthForHitTesting = lineWidth + increasedLineWidthForHitTesting;
|
|
2207
|
+
|
|
2208
|
+
// 无圆角时的策略
|
|
2209
|
+
if (!hasRadius) {
|
|
2210
|
+
var halfWidth = lineWidthForHitTesting / 2;
|
|
2211
|
+
// 同时填充和带有边框
|
|
2212
|
+
if (hasFill && hasStroke || isClipPath) {
|
|
2213
|
+
return inBox(x - halfWidth, y - halfWidth, width + halfWidth, height + halfWidth, position.x, position.y);
|
|
2214
|
+
}
|
|
2215
|
+
// 仅填充
|
|
2216
|
+
if (hasFill) {
|
|
2217
|
+
return inBox(x, y, width, height, position.x, position.y);
|
|
2218
|
+
}
|
|
2219
|
+
if (hasStroke) {
|
|
2220
|
+
return inRect(x, y, width, height, lineWidthForHitTesting, position.x, position.y);
|
|
2221
|
+
}
|
|
2222
|
+
} else {
|
|
2223
|
+
var isHit = false;
|
|
2224
|
+
if (hasStroke || isClipPath) {
|
|
2225
|
+
isHit = inRectWithRadius(x, y, width, height, radius.map(function (r) {
|
|
2226
|
+
return util.clamp(r, 0, Math.min(Math.abs(width) / 2, Math.abs(height) / 2));
|
|
2227
|
+
}), lineWidthForHitTesting, position.x, position.y);
|
|
2228
|
+
}
|
|
2229
|
+
// 仅填充时带有圆角的矩形直接通过图形拾取
|
|
2230
|
+
// 以后可以改成纯数学的近似拾取,将圆弧切割成多边形
|
|
2231
|
+
if (!isHit && (hasFill || isClipPath)) {
|
|
2232
|
+
isHit = isPointInPath(displayObject, position);
|
|
2233
|
+
}
|
|
2234
|
+
return isHit;
|
|
2235
|
+
}
|
|
2236
|
+
return false;
|
|
2237
|
+
}
|
|
2238
|
+
function inRectWithRadius(minX, minY, width, height, radiusArray, lineWidth, x, y) {
|
|
2239
|
+
var _radiusArray = _slicedToArray(radiusArray, 4),
|
|
2240
|
+
tlr = _radiusArray[0],
|
|
2241
|
+
trr = _radiusArray[1],
|
|
2242
|
+
brr = _radiusArray[2],
|
|
2243
|
+
blr = _radiusArray[3];
|
|
2244
|
+
return inLine(minX + tlr, minY, minX + width - trr, minY, lineWidth, x, y) || inLine(minX + width, minY + trr, minX + width, minY + height - brr, lineWidth, x, y) || inLine(minX + width - brr, minY + height, minX + blr, minY + height, lineWidth, x, y) || inLine(minX, minY + height - blr, minX, minY + tlr, lineWidth, x, y) || inArc(minX + width - trr, minY + trr, trr, 1.5 * Math.PI, 2 * Math.PI, lineWidth, x, y) || inArc(minX + width - brr, minY + height - brr, brr, 0, 0.5 * Math.PI, lineWidth, x, y) || inArc(minX + blr, minY + height - blr, blr, 0.5 * Math.PI, Math.PI, lineWidth, x, y) || inArc(minX + tlr, minY + tlr, tlr, Math.PI, 1.5 * Math.PI, lineWidth, x, y);
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
function isPointInPath$1(displayObject, position, isClipPath, isPointInPath, renderingPluginContext, runtime) {
|
|
2248
|
+
var _ref = displayObject.parsedStyle,
|
|
2249
|
+
_ref$pointerEvents = _ref.pointerEvents,
|
|
2250
|
+
pointerEvents = _ref$pointerEvents === void 0 ? 'auto' : _ref$pointerEvents,
|
|
2251
|
+
_ref$x = _ref.x,
|
|
2252
|
+
x = _ref$x === void 0 ? 0 : _ref$x,
|
|
2253
|
+
_ref$y = _ref.y,
|
|
2254
|
+
y = _ref$y === void 0 ? 0 : _ref$y,
|
|
2255
|
+
width = _ref.width,
|
|
2256
|
+
height = _ref.height;
|
|
2257
|
+
if (pointerEvents === 'non-transparent-pixel') {
|
|
2258
|
+
var offscreenCanvas = renderingPluginContext.config.offscreenCanvas;
|
|
2259
|
+
var canvas = runtime.offscreenCanvasCreator.getOrCreateCanvas(offscreenCanvas);
|
|
2260
|
+
var context = runtime.offscreenCanvasCreator.getOrCreateContext(offscreenCanvas, {
|
|
2261
|
+
willReadFrequently: true
|
|
2262
|
+
});
|
|
2263
|
+
canvas.width = width;
|
|
2264
|
+
canvas.height = height;
|
|
2265
|
+
renderingPluginContext.defaultStyleRendererFactory[gLite.Shape.IMAGE].render(context, _objectSpread(_objectSpread({}, displayObject.parsedStyle), {}, {
|
|
2266
|
+
x: 0,
|
|
2267
|
+
y: 0
|
|
2268
|
+
}), displayObject, undefined, undefined, undefined);
|
|
2269
|
+
var imagedata = context.getImageData(position.x - x, position.y - y, 1, 1).data;
|
|
2270
|
+
return imagedata.every(function (component) {
|
|
2271
|
+
return component !== 0;
|
|
2272
|
+
});
|
|
2273
|
+
}
|
|
2274
|
+
return true;
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
function isPointInPath(displayObject, position, isClipPath, isPointInPath) {
|
|
2278
|
+
var bounds = displayObject.getGeometryBounds();
|
|
2279
|
+
|
|
2280
|
+
// @see https://stackoverflow.com/questions/28706989/how-do-i-check-if-a-mouse-click-is-inside-a-rotated-text-on-the-html5-canvas-in
|
|
2281
|
+
return position.x >= bounds.min[0] && position.y >= bounds.min[1] && position.x <= bounds.max[0] && position.y <= bounds.max[1];
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
var Plugin$1 = /*#__PURE__*/function (_AbstractRendererPlug) {
|
|
2285
|
+
function Plugin() {
|
|
2286
|
+
var _this;
|
|
2287
|
+
_classCallCheck(this, Plugin);
|
|
2288
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2289
|
+
args[_key] = arguments[_key];
|
|
2290
|
+
}
|
|
2291
|
+
_this = _callSuper(this, Plugin, [].concat(args));
|
|
2292
|
+
_this.name = 'canvas-picker';
|
|
2293
|
+
return _this;
|
|
2294
|
+
}
|
|
2295
|
+
_inherits(Plugin, _AbstractRendererPlug);
|
|
2296
|
+
return _createClass(Plugin, [{
|
|
2297
|
+
key: "init",
|
|
2298
|
+
value: function init() {
|
|
2299
|
+
var _pointInPathPickerFac;
|
|
2300
|
+
var pointInPathPickerFactory = (_pointInPathPickerFac = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_pointInPathPickerFac, gLite.Shape.CIRCLE, isPointInPath$8), gLite.Shape.ELLIPSE, isPointInPath$7), gLite.Shape.RECT, isPointInPath$2), gLite.Shape.LINE, isPointInPath$6), gLite.Shape.POLYLINE, isPointInPath$3), gLite.Shape.POLYGON, isPointInPath$4), gLite.Shape.PATH, isPointInPath$5), gLite.Shape.TEXT, isPointInPath), gLite.Shape.GROUP, null), gLite.Shape.IMAGE, isPointInPath$1), _defineProperty(_defineProperty(_pointInPathPickerFac, gLite.Shape.HTML, null), gLite.Shape.MESH, null));
|
|
2301
|
+
|
|
2302
|
+
// @ts-ignore
|
|
2303
|
+
this.context.pointInPathPickerFactory = pointInPathPickerFactory;
|
|
2304
|
+
this.addRenderingPlugin(new CanvasPickerPlugin());
|
|
2305
|
+
}
|
|
2306
|
+
}, {
|
|
2307
|
+
key: "destroy",
|
|
2308
|
+
value: function destroy() {
|
|
2309
|
+
// @ts-ignore
|
|
2310
|
+
delete this.context.pointInPathPickerFactory;
|
|
2311
|
+
this.removeAllRenderingPlugins();
|
|
2312
|
+
}
|
|
2313
|
+
}]);
|
|
2314
|
+
}(gLite.AbstractRendererPlugin);
|
|
2315
|
+
|
|
2316
|
+
var index$1 = /*#__PURE__*/Object.freeze({
|
|
2317
|
+
__proto__: null,
|
|
2318
|
+
Plugin: Plugin$1
|
|
2319
|
+
});
|
|
2320
|
+
|
|
2321
|
+
function generatePath$6(context, parsedStyle) {
|
|
2322
|
+
var _parsedStyle$cx = parsedStyle.cx,
|
|
2323
|
+
cx = _parsedStyle$cx === void 0 ? 0 : _parsedStyle$cx,
|
|
2324
|
+
_parsedStyle$cy = parsedStyle.cy,
|
|
2325
|
+
cy = _parsedStyle$cy === void 0 ? 0 : _parsedStyle$cy,
|
|
2326
|
+
r = parsedStyle.r;
|
|
2327
|
+
context.arc(cx, cy, r, 0, Math.PI * 2, false);
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
function generatePath$5(context, parsedStyle) {
|
|
2331
|
+
var _parsedStyle$cx = parsedStyle.cx,
|
|
2332
|
+
cx = _parsedStyle$cx === void 0 ? 0 : _parsedStyle$cx,
|
|
2333
|
+
_parsedStyle$cy = parsedStyle.cy,
|
|
2334
|
+
cy = _parsedStyle$cy === void 0 ? 0 : _parsedStyle$cy,
|
|
2335
|
+
rx = parsedStyle.rx,
|
|
2336
|
+
ry = parsedStyle.ry;
|
|
2337
|
+
|
|
2338
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/ellipse
|
|
2339
|
+
if (context.ellipse) {
|
|
2340
|
+
context.ellipse(cx, cy, rx, ry, 0, 0, Math.PI * 2, false);
|
|
2341
|
+
} else {
|
|
2342
|
+
// 如果不支持,则使用圆来绘制,进行变形
|
|
2343
|
+
var r = rx > ry ? rx : ry;
|
|
2344
|
+
var scaleX = rx > ry ? 1 : rx / ry;
|
|
2345
|
+
var scaleY = rx > ry ? ry / rx : 1;
|
|
2346
|
+
context.save();
|
|
2347
|
+
context.scale(scaleX, scaleY);
|
|
2348
|
+
context.arc(cx, cy, r, 0, Math.PI * 2);
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
function generatePath$4(context, parsedStyle) {
|
|
2353
|
+
var x1 = parsedStyle.x1,
|
|
2354
|
+
y1 = parsedStyle.y1,
|
|
2355
|
+
x2 = parsedStyle.x2,
|
|
2356
|
+
y2 = parsedStyle.y2,
|
|
2357
|
+
markerStart = parsedStyle.markerStart,
|
|
2358
|
+
markerEnd = parsedStyle.markerEnd,
|
|
2359
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
2360
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
2361
|
+
var startOffsetX = 0;
|
|
2362
|
+
var startOffsetY = 0;
|
|
2363
|
+
var endOffsetX = 0;
|
|
2364
|
+
var endOffsetY = 0;
|
|
2365
|
+
var rad = 0;
|
|
2366
|
+
var x;
|
|
2367
|
+
var y;
|
|
2368
|
+
if (markerStart && gLite.isDisplayObject(markerStart) && markerStartOffset) {
|
|
2369
|
+
x = x2 - x1;
|
|
2370
|
+
y = y2 - y1;
|
|
2371
|
+
rad = Math.atan2(y, x);
|
|
2372
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
2373
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
2374
|
+
}
|
|
2375
|
+
if (markerEnd && gLite.isDisplayObject(markerEnd) && markerEndOffset) {
|
|
2376
|
+
x = x1 - x2;
|
|
2377
|
+
y = y1 - y2;
|
|
2378
|
+
rad = Math.atan2(y, x);
|
|
2379
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
2380
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
2381
|
+
}
|
|
2382
|
+
context.moveTo(x1 + startOffsetX, y1 + startOffsetY);
|
|
2383
|
+
context.lineTo(x2 + endOffsetX, y2 + endOffsetY);
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2386
|
+
function generatePath$3(context, parsedStyle) {
|
|
2387
|
+
var markerStart = parsedStyle.markerStart,
|
|
2388
|
+
markerEnd = parsedStyle.markerEnd,
|
|
2389
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
2390
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
2391
|
+
var _parsedStyle$d = parsedStyle.d,
|
|
2392
|
+
absolutePath = _parsedStyle$d.absolutePath,
|
|
2393
|
+
segments = _parsedStyle$d.segments;
|
|
2394
|
+
var startOffsetX = 0;
|
|
2395
|
+
var startOffsetY = 0;
|
|
2396
|
+
var endOffsetX = 0;
|
|
2397
|
+
var endOffsetY = 0;
|
|
2398
|
+
var rad = 0;
|
|
2399
|
+
var x;
|
|
2400
|
+
var y;
|
|
2401
|
+
if (markerStart && gLite.isDisplayObject(markerStart) && markerStartOffset) {
|
|
2402
|
+
var _getStartTangent = markerStart.parentNode.getStartTangent(),
|
|
2403
|
+
_getStartTangent2 = _slicedToArray(_getStartTangent, 2),
|
|
2404
|
+
p1 = _getStartTangent2[0],
|
|
2405
|
+
p2 = _getStartTangent2[1];
|
|
2406
|
+
x = p1[0] - p2[0];
|
|
2407
|
+
y = p1[1] - p2[1];
|
|
2408
|
+
rad = Math.atan2(y, x);
|
|
2409
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
2410
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
2411
|
+
}
|
|
2412
|
+
if (markerEnd && gLite.isDisplayObject(markerEnd) && markerEndOffset) {
|
|
2413
|
+
var _getEndTangent = markerEnd.parentNode.getEndTangent(),
|
|
2414
|
+
_getEndTangent2 = _slicedToArray(_getEndTangent, 2),
|
|
2415
|
+
_p = _getEndTangent2[0],
|
|
2416
|
+
_p2 = _getEndTangent2[1];
|
|
2417
|
+
x = _p[0] - _p2[0];
|
|
2418
|
+
y = _p[1] - _p2[1];
|
|
2419
|
+
rad = Math.atan2(y, x);
|
|
2420
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
2421
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
2422
|
+
}
|
|
2423
|
+
for (var i = 0; i < absolutePath.length; i++) {
|
|
2424
|
+
var params = absolutePath[i];
|
|
2425
|
+
var command = params[0];
|
|
2426
|
+
var nextSegment = absolutePath[i + 1];
|
|
2427
|
+
var useStartOffset = i === 0 && (startOffsetX !== 0 || startOffsetY !== 0);
|
|
2428
|
+
var useEndOffset = (i === absolutePath.length - 1 || nextSegment && (nextSegment[0] === 'M' || nextSegment[0] === 'Z')) && endOffsetX !== 0 && endOffsetY !== 0;
|
|
2429
|
+
var _ref = useStartOffset ? [startOffsetX, startOffsetY] : [0, 0],
|
|
2430
|
+
_ref2 = _slicedToArray(_ref, 2),
|
|
2431
|
+
startOffsetXTemp = _ref2[0],
|
|
2432
|
+
startOffsetYTemp = _ref2[1];
|
|
2433
|
+
var _ref3 = useEndOffset ? [endOffsetX, endOffsetY] : [0, 0],
|
|
2434
|
+
_ref4 = _slicedToArray(_ref3, 2),
|
|
2435
|
+
endOffsetXTemp = _ref4[0],
|
|
2436
|
+
endOffsetYTemp = _ref4[1];
|
|
2437
|
+
switch (command) {
|
|
2438
|
+
case 'M':
|
|
2439
|
+
// Use start marker offset
|
|
2440
|
+
context.moveTo(params[1] + startOffsetXTemp, params[2] + startOffsetYTemp);
|
|
2441
|
+
break;
|
|
2442
|
+
case 'L':
|
|
2443
|
+
context.lineTo(params[1] + endOffsetXTemp, params[2] + endOffsetYTemp);
|
|
2444
|
+
break;
|
|
2445
|
+
case 'Q':
|
|
2446
|
+
context.quadraticCurveTo(params[1], params[2], params[3] + endOffsetXTemp, params[4] + endOffsetYTemp);
|
|
2447
|
+
break;
|
|
2448
|
+
case 'C':
|
|
2449
|
+
context.bezierCurveTo(params[1], params[2], params[3], params[4], params[5] + endOffsetXTemp, params[6] + endOffsetYTemp);
|
|
2450
|
+
break;
|
|
2451
|
+
case 'A':
|
|
2452
|
+
{
|
|
2453
|
+
// FIXME startOffset / endOffset
|
|
2454
|
+
var arcParams = segments[i].arcParams;
|
|
2455
|
+
var cx = arcParams.cx,
|
|
2456
|
+
cy = arcParams.cy,
|
|
2457
|
+
rx = arcParams.rx,
|
|
2458
|
+
ry = arcParams.ry,
|
|
2459
|
+
startAngle = arcParams.startAngle,
|
|
2460
|
+
endAngle = arcParams.endAngle,
|
|
2461
|
+
xRotation = arcParams.xRotation,
|
|
2462
|
+
sweepFlag = arcParams.sweepFlag;
|
|
2463
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/ellipse
|
|
2464
|
+
if (context.ellipse) {
|
|
2465
|
+
context.ellipse(cx, cy, rx, ry, xRotation, startAngle, endAngle, !!(1 - sweepFlag));
|
|
2466
|
+
} else {
|
|
2467
|
+
// @see https://stackoverflow.com/a/47494351
|
|
2468
|
+
var r = rx > ry ? rx : ry;
|
|
2469
|
+
var scaleX = rx > ry ? 1 : rx / ry;
|
|
2470
|
+
var scaleY = rx > ry ? ry / rx : 1;
|
|
2471
|
+
context.translate(cx, cy);
|
|
2472
|
+
context.rotate(xRotation);
|
|
2473
|
+
context.scale(scaleX, scaleY);
|
|
2474
|
+
context.arc(0, 0, r, startAngle, endAngle, !!(1 - sweepFlag));
|
|
2475
|
+
context.scale(1 / scaleX, 1 / scaleY);
|
|
2476
|
+
context.rotate(-xRotation);
|
|
2477
|
+
context.translate(-cx, -cy);
|
|
2478
|
+
}
|
|
2479
|
+
if (useEndOffset) {
|
|
2480
|
+
context.lineTo(params[6] + endOffsetX, params[7] + endOffsetY);
|
|
2481
|
+
}
|
|
2482
|
+
break;
|
|
2483
|
+
}
|
|
2484
|
+
case 'Z':
|
|
2485
|
+
context.closePath();
|
|
2486
|
+
break;
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
function generatePath$2(context, parsedStyle) {
|
|
2492
|
+
var markerStart = parsedStyle.markerStart,
|
|
2493
|
+
markerEnd = parsedStyle.markerEnd,
|
|
2494
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
2495
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
2496
|
+
var points = parsedStyle.points.points;
|
|
2497
|
+
var length = points.length;
|
|
2498
|
+
var x1 = points[0][0];
|
|
2499
|
+
var y1 = points[0][1];
|
|
2500
|
+
var x2 = points[length - 1][0];
|
|
2501
|
+
var y2 = points[length - 1][1];
|
|
2502
|
+
var startOffsetX = 0;
|
|
2503
|
+
var startOffsetY = 0;
|
|
2504
|
+
var endOffsetX = 0;
|
|
2505
|
+
var endOffsetY = 0;
|
|
2506
|
+
var rad = 0;
|
|
2507
|
+
var x;
|
|
2508
|
+
var y;
|
|
2509
|
+
if (markerStart && gLite.isDisplayObject(markerStart) && markerStartOffset) {
|
|
2510
|
+
x = points[1][0] - points[0][0];
|
|
2511
|
+
y = points[1][1] - points[0][1];
|
|
2512
|
+
rad = Math.atan2(y, x);
|
|
2513
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
2514
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
2515
|
+
}
|
|
2516
|
+
if (markerEnd && gLite.isDisplayObject(markerEnd) && markerEndOffset) {
|
|
2517
|
+
x = points[length - 1][0] - points[0][0];
|
|
2518
|
+
y = points[length - 1][1] - points[0][1];
|
|
2519
|
+
rad = Math.atan2(y, x);
|
|
2520
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
2521
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
2522
|
+
}
|
|
2523
|
+
context.moveTo(x1 + (startOffsetX || endOffsetX), y1 + (startOffsetY || endOffsetY));
|
|
2524
|
+
for (var i = 1; i < length - 1; i++) {
|
|
2525
|
+
var point = points[i];
|
|
2526
|
+
context.lineTo(point[0], point[1]);
|
|
2527
|
+
}
|
|
2528
|
+
context.lineTo(x2, y2);
|
|
2529
|
+
}
|
|
2530
|
+
|
|
2531
|
+
function generatePath$1(context, parsedStyle) {
|
|
2532
|
+
var markerStart = parsedStyle.markerStart,
|
|
2533
|
+
markerEnd = parsedStyle.markerEnd,
|
|
2534
|
+
markerStartOffset = parsedStyle.markerStartOffset,
|
|
2535
|
+
markerEndOffset = parsedStyle.markerEndOffset;
|
|
2536
|
+
var points = parsedStyle.points.points;
|
|
2537
|
+
var length = points.length;
|
|
2538
|
+
var x1 = points[0][0];
|
|
2539
|
+
var y1 = points[0][1];
|
|
2540
|
+
var x2 = points[length - 1][0];
|
|
2541
|
+
var y2 = points[length - 1][1];
|
|
2542
|
+
var startOffsetX = 0;
|
|
2543
|
+
var startOffsetY = 0;
|
|
2544
|
+
var endOffsetX = 0;
|
|
2545
|
+
var endOffsetY = 0;
|
|
2546
|
+
var rad = 0;
|
|
2547
|
+
var x;
|
|
2548
|
+
var y;
|
|
2549
|
+
if (markerStart && gLite.isDisplayObject(markerStart) && markerStartOffset) {
|
|
2550
|
+
x = points[1][0] - points[0][0];
|
|
2551
|
+
y = points[1][1] - points[0][1];
|
|
2552
|
+
rad = Math.atan2(y, x);
|
|
2553
|
+
startOffsetX = Math.cos(rad) * (markerStartOffset || 0);
|
|
2554
|
+
startOffsetY = Math.sin(rad) * (markerStartOffset || 0);
|
|
2555
|
+
}
|
|
2556
|
+
if (markerEnd && gLite.isDisplayObject(markerEnd) && markerEndOffset) {
|
|
2557
|
+
x = points[length - 2][0] - points[length - 1][0];
|
|
2558
|
+
y = points[length - 2][1] - points[length - 1][1];
|
|
2559
|
+
rad = Math.atan2(y, x);
|
|
2560
|
+
endOffsetX = Math.cos(rad) * (markerEndOffset || 0);
|
|
2561
|
+
endOffsetY = Math.sin(rad) * (markerEndOffset || 0);
|
|
2562
|
+
}
|
|
2563
|
+
context.moveTo(x1 + startOffsetX, y1 + startOffsetY);
|
|
2564
|
+
for (var i = 1; i < length - 1; i++) {
|
|
2565
|
+
var point = points[i];
|
|
2566
|
+
context.lineTo(point[0], point[1]);
|
|
2567
|
+
}
|
|
2568
|
+
context.lineTo(x2 + endOffsetX, y2 + endOffsetY);
|
|
2569
|
+
}
|
|
2570
|
+
|
|
2571
|
+
function generatePath(context, parsedStyle) {
|
|
2572
|
+
var _parsedStyle$x = parsedStyle.x,
|
|
2573
|
+
x = _parsedStyle$x === void 0 ? 0 : _parsedStyle$x,
|
|
2574
|
+
_parsedStyle$y = parsedStyle.y,
|
|
2575
|
+
y = _parsedStyle$y === void 0 ? 0 : _parsedStyle$y,
|
|
2576
|
+
radius = parsedStyle.radius,
|
|
2577
|
+
width = parsedStyle.width,
|
|
2578
|
+
height = parsedStyle.height;
|
|
2579
|
+
var w = width;
|
|
2580
|
+
var h = height;
|
|
2581
|
+
var hasRadius = radius && radius.some(function (r) {
|
|
2582
|
+
return r !== 0;
|
|
2583
|
+
});
|
|
2584
|
+
if (!hasRadius) {
|
|
2585
|
+
// Canvas support negative width/height of rect
|
|
2586
|
+
context.rect(x, y, w, h);
|
|
2587
|
+
} else {
|
|
2588
|
+
var signX = width > 0 ? 1 : -1;
|
|
2589
|
+
var signY = height > 0 ? 1 : -1;
|
|
2590
|
+
var sweepFlag = signX + signY === 0;
|
|
2591
|
+
var _radius$map = radius.map(function (r) {
|
|
2592
|
+
return util.clamp(r, 0, Math.min(Math.abs(w) / 2, Math.abs(h) / 2));
|
|
2593
|
+
}),
|
|
2594
|
+
_radius$map2 = _slicedToArray(_radius$map, 4),
|
|
2595
|
+
tlr = _radius$map2[0],
|
|
2596
|
+
trr = _radius$map2[1],
|
|
2597
|
+
brr = _radius$map2[2],
|
|
2598
|
+
blr = _radius$map2[3];
|
|
2599
|
+
context.moveTo(signX * tlr + x, y);
|
|
2600
|
+
context.lineTo(w - signX * trr + x, y);
|
|
2601
|
+
if (trr !== 0) {
|
|
2602
|
+
context.arc(w - signX * trr + x, signY * trr + y, trr, -signY * Math.PI / 2, signX > 0 ? 0 : Math.PI, sweepFlag);
|
|
2603
|
+
}
|
|
2604
|
+
context.lineTo(w + x, h - signY * brr + y);
|
|
2605
|
+
if (brr !== 0) {
|
|
2606
|
+
context.arc(w - signX * brr + x, h - signY * brr + y, brr, signX > 0 ? 0 : Math.PI, signY > 0 ? Math.PI / 2 : 1.5 * Math.PI, sweepFlag);
|
|
2607
|
+
}
|
|
2608
|
+
context.lineTo(signX * blr + x, h + y);
|
|
2609
|
+
if (blr !== 0) {
|
|
2610
|
+
context.arc(signX * blr + x, h - signY * blr + y, blr, signY > 0 ? Math.PI / 2 : -Math.PI / 2, signX > 0 ? Math.PI : 0, sweepFlag);
|
|
2611
|
+
}
|
|
2612
|
+
context.lineTo(x, signY * tlr + y);
|
|
2613
|
+
if (tlr !== 0) {
|
|
2614
|
+
context.arc(signX * tlr + x, signY * tlr + y, tlr, signX > 0 ? Math.PI : 0, signY > 0 ? Math.PI * 1.5 : Math.PI / 2, sweepFlag);
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2619
|
+
var Plugin = /*#__PURE__*/function (_AbstractRendererPlug) {
|
|
2620
|
+
function Plugin() {
|
|
2621
|
+
var _this;
|
|
2622
|
+
_classCallCheck(this, Plugin);
|
|
2623
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2624
|
+
args[_key] = arguments[_key];
|
|
2625
|
+
}
|
|
2626
|
+
_this = _callSuper(this, Plugin, [].concat(args));
|
|
2627
|
+
_this.name = 'canvas-path-generator';
|
|
2628
|
+
return _this;
|
|
2629
|
+
}
|
|
2630
|
+
_inherits(Plugin, _AbstractRendererPlug);
|
|
2631
|
+
return _createClass(Plugin, [{
|
|
2632
|
+
key: "init",
|
|
2633
|
+
value: function init() {
|
|
2634
|
+
var _pathGeneratorFactory;
|
|
2635
|
+
var pathGeneratorFactory = (_pathGeneratorFactory = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_pathGeneratorFactory, gLite.Shape.CIRCLE, generatePath$6), gLite.Shape.ELLIPSE, generatePath$5), gLite.Shape.RECT, generatePath), gLite.Shape.LINE, generatePath$4), gLite.Shape.POLYLINE, generatePath$1), gLite.Shape.POLYGON, generatePath$2), gLite.Shape.PATH, generatePath$3), gLite.Shape.TEXT, undefined), gLite.Shape.GROUP, undefined), gLite.Shape.IMAGE, undefined), _defineProperty(_defineProperty(_defineProperty(_pathGeneratorFactory, gLite.Shape.HTML, undefined), gLite.Shape.MESH, undefined), gLite.Shape.FRAGMENT, undefined));
|
|
2636
|
+
|
|
2637
|
+
// @ts-ignore
|
|
2638
|
+
this.context.pathGeneratorFactory = pathGeneratorFactory;
|
|
2639
|
+
}
|
|
2640
|
+
}, {
|
|
2641
|
+
key: "destroy",
|
|
2642
|
+
value: function destroy() {
|
|
2643
|
+
// @ts-ignore
|
|
2644
|
+
delete this.context.pathGeneratorFactory;
|
|
2645
|
+
}
|
|
2646
|
+
}]);
|
|
2647
|
+
}(gLite.AbstractRendererPlugin);
|
|
2648
|
+
|
|
2649
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
2650
|
+
__proto__: null,
|
|
2651
|
+
Plugin: Plugin
|
|
2652
|
+
});
|
|
49
2653
|
|
|
50
2654
|
var Canvas2DContextService = /*#__PURE__*/function () {
|
|
51
2655
|
function Canvas2DContextService(context) {
|
|
@@ -202,27 +2806,36 @@ var Renderer = /*#__PURE__*/function (_AbstractRenderer) {
|
|
|
202
2806
|
|
|
203
2807
|
// register Canvas2DContext
|
|
204
2808
|
_this.registerPlugin(new ContextRegisterPlugin());
|
|
205
|
-
_this.registerPlugin(new
|
|
206
|
-
_this.registerPlugin(new
|
|
2809
|
+
_this.registerPlugin(new gLite.ImageLoader.Plugin());
|
|
2810
|
+
_this.registerPlugin(new Plugin());
|
|
207
2811
|
// enable rendering with Canvas2D API
|
|
208
|
-
_this.registerPlugin(new
|
|
209
|
-
_this.registerPlugin(new
|
|
2812
|
+
_this.registerPlugin(new Plugin$2());
|
|
2813
|
+
_this.registerPlugin(new gLite.DomInteraction.Plugin());
|
|
210
2814
|
// enable picking with Canvas2D API
|
|
211
|
-
_this.registerPlugin(new
|
|
2815
|
+
_this.registerPlugin(new Plugin$1());
|
|
212
2816
|
|
|
213
2817
|
// render HTML component
|
|
214
|
-
_this.registerPlugin(new
|
|
2818
|
+
_this.registerPlugin(new gLite.HTMLRenderer.Plugin());
|
|
215
2819
|
return _this;
|
|
216
2820
|
}
|
|
217
2821
|
_inherits(Renderer, _AbstractRenderer);
|
|
218
2822
|
return _createClass(Renderer);
|
|
219
2823
|
}(gLite.AbstractRenderer);
|
|
220
2824
|
|
|
221
|
-
exports
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
exports
|
|
226
|
-
|
|
2825
|
+
Object.defineProperty(exports, 'DomInteraction', {
|
|
2826
|
+
enumerable: true,
|
|
2827
|
+
get: function () { return gLite.DomInteraction; }
|
|
2828
|
+
});
|
|
2829
|
+
Object.defineProperty(exports, 'HTMLRenderer', {
|
|
2830
|
+
enumerable: true,
|
|
2831
|
+
get: function () { return gLite.HTMLRenderer; }
|
|
2832
|
+
});
|
|
2833
|
+
Object.defineProperty(exports, 'ImageLoader', {
|
|
2834
|
+
enumerable: true,
|
|
2835
|
+
get: function () { return gLite.ImageLoader; }
|
|
2836
|
+
});
|
|
2837
|
+
exports.CanvasPathGenerator = index;
|
|
2838
|
+
exports.CanvasPicker = index$1;
|
|
2839
|
+
exports.CanvasRenderer = index$2;
|
|
227
2840
|
exports.Renderer = Renderer;
|
|
228
2841
|
//# sourceMappingURL=index.js.map
|