@antv/g 5.0.9 → 5.0.12

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.
Files changed (44) hide show
  1. package/dist/index.umd.min.js +2 -2
  2. package/es/Canvas.d.ts +4 -0
  3. package/es/Canvas.js +76 -15
  4. package/es/display-objects/DisplayObject.d.ts +2 -0
  5. package/es/display-objects/DisplayObject.js +13 -1
  6. package/es/display-objects/Polygon.d.ts +2 -5
  7. package/es/display-objects/Polyline.d.ts +2 -6
  8. package/es/plugins/EventPlugin.js +2 -1
  9. package/es/property-handlers/aabb.js +28 -24
  10. package/es/services/ContextService.d.ts +2 -1
  11. package/es/services/EventService.js +4 -2
  12. package/es/services/RenderingService.d.ts +2 -2
  13. package/es/services/RenderingService.js +2 -2
  14. package/es/services/SceneGraphService.d.ts +4 -0
  15. package/es/services/SceneGraphService.js +35 -1
  16. package/es/services/aabb/interfaces.d.ts +1 -0
  17. package/es/types.d.ts +17 -12
  18. package/es/utils/canvas.d.ts +1 -0
  19. package/es/utils/canvas.js +9 -6
  20. package/es/utils/path.js +13 -4
  21. package/es/utils/raf.d.ts +6 -3
  22. package/es/utils/raf.js +13 -14
  23. package/lib/Canvas.d.ts +4 -0
  24. package/lib/Canvas.js +76 -13
  25. package/lib/display-objects/DisplayObject.d.ts +2 -0
  26. package/lib/display-objects/DisplayObject.js +13 -1
  27. package/lib/display-objects/Polygon.d.ts +2 -5
  28. package/lib/display-objects/Polyline.d.ts +2 -6
  29. package/lib/plugins/EventPlugin.js +2 -1
  30. package/lib/property-handlers/aabb.js +28 -24
  31. package/lib/services/ContextService.d.ts +2 -1
  32. package/lib/services/EventService.js +4 -2
  33. package/lib/services/RenderingService.d.ts +2 -2
  34. package/lib/services/RenderingService.js +1 -1
  35. package/lib/services/SceneGraphService.d.ts +4 -0
  36. package/lib/services/SceneGraphService.js +41 -1
  37. package/lib/services/aabb/interfaces.d.ts +1 -0
  38. package/lib/types.d.ts +17 -12
  39. package/lib/utils/canvas.d.ts +1 -0
  40. package/lib/utils/canvas.js +11 -5
  41. package/lib/utils/path.js +15 -4
  42. package/lib/utils/raf.d.ts +6 -3
  43. package/lib/utils/raf.js +19 -13
  44. package/package.json +5 -3
package/lib/Canvas.d.ts CHANGED
@@ -48,6 +48,9 @@ export declare class Canvas extends EventTarget implements ICanvas {
48
48
  */
49
49
  private eventService;
50
50
  private renderingService;
51
+ private inited;
52
+ private readyPromise;
53
+ private resolveReadyPromise;
51
54
  constructor(config: CanvasConfig);
52
55
  private initRenderingContext;
53
56
  private initDefaultCamera;
@@ -71,6 +74,7 @@ export declare class Canvas extends EventTarget implements ICanvas {
71
74
  rendered: number;
72
75
  };
73
76
  getComputedStyle(node: DisplayObject): any;
77
+ get ready(): Promise<any>;
74
78
  destroy(destroyScenegraph?: boolean): void;
75
79
  /**
76
80
  * compatible with G 3.0
package/lib/Canvas.js CHANGED
@@ -64,24 +64,54 @@ function (_super) {
64
64
 
65
65
 
66
66
  _this.container = _globalModule.globalContainer.createChild();
67
- _this.Element = _DisplayObject.DisplayObject; // create document
67
+ _this.Element = _DisplayObject.DisplayObject;
68
+ _this.inited = false; // create document
68
69
 
69
70
  _this.document = new _dom.Document();
70
71
  _this.document.defaultView = _this; // create registry of custom elements
71
72
 
72
73
  _this.customElements = new _CustomElementRegistry.CustomElementRegistry();
73
- (0, _canvas.cleanExistedCanvas)(config.container, _this);
74
- var mergedConfig = (0, _tslib.__assign)({
75
- width: 300,
76
- height: 150,
77
- cursor: 'default'
78
- }, config);
74
+ var container = config.container,
75
+ canvas = config.canvas,
76
+ width = config.width,
77
+ height = config.height,
78
+ devicePixelRatio = config.devicePixelRatio,
79
+ renderer = config.renderer,
80
+ background = config.background,
81
+ requestAnimationFrame = config.requestAnimationFrame,
82
+ cancelAnimationFrame = config.cancelAnimationFrame;
83
+ (0, _canvas.cleanExistedCanvas)(container, _this);
84
+ var canvasWidth = width;
85
+ var canvasHeight = height;
86
+ var dpr = devicePixelRatio; // use user-defined <canvas> or OffscreenCanvas
87
+
88
+ if (canvas) {
89
+ // infer width & height with dpr
90
+ dpr = devicePixelRatio || _canvas.isBrowser && window.devicePixelRatio || 1;
91
+ dpr = dpr >= 1 ? Math.ceil(dpr) : 1;
92
+ canvasWidth = width || canvas.width / dpr;
93
+ canvasHeight = height || canvas.height / dpr;
94
+ }
95
+
96
+ if (requestAnimationFrame && cancelAnimationFrame) {
97
+ (0, _raf.patch)(requestAnimationFrame, cancelAnimationFrame);
98
+ }
99
+
100
+ _this.initRenderingContext({
101
+ container: container,
102
+ canvas: canvas,
103
+ width: canvasWidth,
104
+ height: canvasHeight,
105
+ renderer: renderer,
106
+ devicePixelRatio: dpr,
107
+ cursor: 'default',
108
+ background: background // background: 'white',
79
109
 
80
- _this.initRenderingContext(mergedConfig);
110
+ });
81
111
 
82
- _this.initDefaultCamera(mergedConfig.width, mergedConfig.height);
112
+ _this.initDefaultCamera(canvasWidth, canvasHeight);
83
113
 
84
- _this.initRenderer(mergedConfig.renderer);
114
+ _this.initRenderer(renderer);
85
115
 
86
116
  return _this;
87
117
  }
@@ -183,6 +213,28 @@ function (_super) {
183
213
  return node.parsedStyle;
184
214
  };
185
215
 
216
+ Object.defineProperty(Canvas.prototype, "ready", {
217
+ get: function get() {
218
+ var _this = this;
219
+
220
+ if (!this.readyPromise) {
221
+ this.readyPromise = new Promise(function (resolve) {
222
+ _this.resolveReadyPromise = function () {
223
+ resolve(_this);
224
+ };
225
+ });
226
+
227
+ if (this.inited) {
228
+ this.resolveReadyPromise();
229
+ }
230
+ }
231
+
232
+ return this.readyPromise;
233
+ },
234
+ enumerable: false,
235
+ configurable: true
236
+ });
237
+
186
238
  Canvas.prototype.destroy = function (destroyScenegraph) {
187
239
  if (destroyScenegraph === void 0) {
188
240
  destroyScenegraph = true;
@@ -191,7 +243,9 @@ function (_super) {
191
243
  this.emit(CanvasEvent.BEFORE_DESTROY, function () {});
192
244
 
193
245
  if (this.frameId) {
194
- (0, _raf.cancelAnimationFrame)(this.frameId);
246
+ var cancelRAF = this.getConfig().cancelAnimationFrame || _raf.cancelAnimationFrame;
247
+
248
+ cancelRAF(this.frameId);
195
249
  } // unmount all children
196
250
 
197
251
 
@@ -285,7 +339,7 @@ function (_super) {
285
339
  var tick = function tick() {
286
340
  _this.render();
287
341
 
288
- _this.frameId = requestAnimationFrame(tick);
342
+ _this.frameId = (0, _raf.requestAnimationFrame)(tick);
289
343
  };
290
344
 
291
345
  tick();
@@ -296,8 +350,11 @@ function (_super) {
296
350
 
297
351
  if (!renderer) {
298
352
  throw new Error('Renderer is required.');
299
- }
353
+ } // reset
300
354
 
355
+
356
+ this.inited = false;
357
+ this.readyPromise = undefined;
301
358
  this.loadCommonContainerModule();
302
359
  this.loadRendererContainerModule(renderer); // init context
303
360
 
@@ -307,6 +364,12 @@ function (_super) {
307
364
  contextService.init();
308
365
  this.renderingService.init().then(function () {
309
366
  _this.emit(CanvasEvent.READY, {});
367
+
368
+ if (_this.readyPromise) {
369
+ _this.resolveReadyPromise();
370
+ }
371
+
372
+ _this.inited = true;
310
373
  });
311
374
 
312
375
  if (renderer.getConfig().enableAutoRendering) {
@@ -107,6 +107,8 @@ export declare class DisplayObject<StyleProps extends BaseStyleProps = any, Pars
107
107
  setLocalEulerAngles(z: number): this;
108
108
  rotateLocal(x: number, y?: number, z?: number): this;
109
109
  rotate(x: number, y?: number, z?: number): this;
110
+ setRotation(rotation: quat | number, y?: number, z?: number, w?: number): this;
111
+ setLocalRotation(rotation: quat | number, y?: number, z?: number, w?: number): this;
110
112
  getRotation(): quat;
111
113
  getLocalRotation(): quat;
112
114
  getLocalTransform(): mat4;
@@ -34,7 +34,9 @@ var DEFAULT_STYLE_PROPS = {
34
34
  origin: [0, 0, 0],
35
35
  opacity: 1,
36
36
  fillOpacity: 1,
37
- strokeOpacity: 1
37
+ strokeOpacity: 1,
38
+ fill: 'transparent',
39
+ stroke: 'transparent'
38
40
  };
39
41
  /**
40
42
  * prototype chains: DisplayObject -> Element -> Node -> EventTarget
@@ -487,6 +489,16 @@ function (_super) {
487
489
  return this;
488
490
  };
489
491
 
492
+ DisplayObject.prototype.setRotation = function (rotation, y, z, w) {
493
+ this.sceneGraphService.setRotation(this, rotation, y, z, w);
494
+ return this;
495
+ };
496
+
497
+ DisplayObject.prototype.setLocalRotation = function (rotation, y, z, w) {
498
+ this.sceneGraphService.setLocalRotation(this, rotation, y, z, w);
499
+ return this;
500
+ };
501
+
490
502
  DisplayObject.prototype.getRotation = function () {
491
503
  return this.sceneGraphService.getRotation(this);
492
504
  };
@@ -1,12 +1,9 @@
1
- import type { BaseStyleProps } from '../types';
1
+ import type { BaseStyleProps, ParsedBaseStyleProps } from '../types';
2
2
  import { DisplayObject } from './DisplayObject';
3
3
  import type { DisplayObjectConfig } from '../dom';
4
- import type { ParsedPolylineStyleProps } from './Polyline';
5
4
  export interface PolygonStyleProps extends BaseStyleProps {
6
5
  points: [number, number][];
7
6
  }
8
- export declare class Polygon extends DisplayObject<PolygonStyleProps, {
9
- points: ParsedPolylineStyleProps;
10
- }> {
7
+ export declare class Polygon extends DisplayObject<PolygonStyleProps, ParsedBaseStyleProps> {
11
8
  constructor({ style, ...rest }: DisplayObjectConfig<PolygonStyleProps>);
12
9
  }
@@ -1,4 +1,4 @@
1
- import type { BaseStyleProps } from '../types';
1
+ import type { BaseStyleProps, ParsedBaseStyleProps } from '../types';
2
2
  import { DisplayObject } from './DisplayObject';
3
3
  import type { DisplayObjectConfig } from '../dom';
4
4
  import { Point } from '../shapes';
@@ -7,14 +7,10 @@ export interface PolylineStyleProps extends BaseStyleProps {
7
7
  }
8
8
  export interface ParsedPolylineStyleProps {
9
9
  points: [number, number][];
10
- defX: number;
11
- defY: number;
12
10
  segments: [number, number][];
13
11
  totalLength: number;
14
12
  }
15
- export declare class Polyline extends DisplayObject<PolylineStyleProps, {
16
- points: ParsedPolylineStyleProps;
17
- }> {
13
+ export declare class Polyline extends DisplayObject<PolylineStyleProps, ParsedBaseStyleProps> {
18
14
  constructor({ style, ...rest }: DisplayObjectConfig<PolylineStyleProps>);
19
15
  getTotalLength(): number;
20
16
  getPoint(ratio: number): Point;
@@ -145,7 +145,8 @@ function () {
145
145
 
146
146
  var $element = _this.contextService.getDomElement();
147
147
 
148
- var outside = $element && nativeEvent.target && nativeEvent.target !== $element && $element.contains && // @ts-ignore
148
+ var outside = $element && nativeEvent.target && nativeEvent.target !== $element && // @ts-ignore
149
+ $element.contains && // @ts-ignore
149
150
  !$element.contains(nativeEvent.target) ? 'outside' : '';
150
151
  var normalizedEvents = (0, _event.normalizeToPointerEvent)(nativeEvent);
151
152
 
@@ -42,12 +42,14 @@ function updateGeometry(oldValue, newValue, object) {
42
42
  x = _c === void 0 ? 0 : _c,
43
43
  _d = _a.y,
44
44
  y = _d === void 0 ? 0 : _d,
45
- _e = _a.offsetX,
46
- offsetX = _e === void 0 ? 0 : _e,
47
- _f = _a.offsetY,
48
- offsetY = _f === void 0 ? 0 : _f,
49
- _g = _a.offsetZ,
50
- offsetZ = _g === void 0 ? 0 : _g;
45
+ _e = _a.z,
46
+ z = _e === void 0 ? 0 : _e,
47
+ _f = _a.offsetX,
48
+ offsetX = _f === void 0 ? 0 : _f,
49
+ _g = _a.offsetY,
50
+ offsetY = _g === void 0 ? 0 : _g,
51
+ _h = _a.offsetZ,
52
+ offsetZ = _h === void 0 ? 0 : _h;
51
53
 
52
54
  object.parsedStyle.width = width;
53
55
  object.parsedStyle.height = height;
@@ -61,23 +63,25 @@ function updateGeometry(oldValue, newValue, object) {
61
63
  object.translateLocal(object.parsedStyle.offsetX, object.parsedStyle.offsetY);
62
64
  object.parsedStyle.x = object.style.x;
63
65
  object.parsedStyle.y = object.style.y;
66
+ object.parsedStyle.z = z;
64
67
  } else {
65
68
  object.parsedStyle.x = x;
66
69
  object.parsedStyle.y = y;
70
+ object.parsedStyle.z = z;
67
71
  } // init with content box
68
72
 
69
73
 
70
74
  var halfExtents = _glMatrix.vec3.fromValues(width / 2, height / 2, depth / 2); // anchor is center by default, don't account for lineWidth here
71
75
 
72
76
 
73
- var _h = object.parsedStyle,
74
- _j = _h.lineWidth,
75
- lineWidth = _j === void 0 ? 0 : _j,
76
- _k = _h.anchor,
77
- anchor = _k === void 0 ? [0, 0] : _k,
78
- shadowColor = _h.shadowColor,
79
- _l = _h.filter,
80
- filter = _l === void 0 ? [] : _l; // <Text> use textAlign & textBaseline instead of anchor
77
+ var _j = object.parsedStyle,
78
+ _k = _j.lineWidth,
79
+ lineWidth = _k === void 0 ? 0 : _k,
80
+ _l = _j.anchor,
81
+ anchor = _l === void 0 ? [0, 0] : _l,
82
+ shadowColor = _j.shadowColor,
83
+ _m = _j.filter,
84
+ filter = _m === void 0 ? [] : _m; // <Text> use textAlign & textBaseline instead of anchor
81
85
 
82
86
  if (object.nodeName === _types.SHAPE.Text) {
83
87
  delete object.parsedStyle.anchor;
@@ -96,16 +100,16 @@ function updateGeometry(oldValue, newValue, object) {
96
100
  geometry_1.renderBounds.update(center, halfExtents); // account for shadow, only support constant value now
97
101
 
98
102
  if (shadowColor && shadowColor.type === _color.PARSED_COLOR_TYPE.Constant) {
99
- var _m = geometry_1.renderBounds,
100
- min = _m.min,
101
- max = _m.max;
102
- var _o = object.parsedStyle,
103
- _p = _o.shadowBlur,
104
- shadowBlur = _p === void 0 ? 0 : _p,
105
- _q = _o.shadowOffsetX,
106
- shadowOffsetX = _q === void 0 ? 0 : _q,
107
- _r = _o.shadowOffsetY,
108
- shadowOffsetY = _r === void 0 ? 0 : _r;
103
+ var _o = geometry_1.renderBounds,
104
+ min = _o.min,
105
+ max = _o.max;
106
+ var _p = object.parsedStyle,
107
+ _q = _p.shadowBlur,
108
+ shadowBlur = _q === void 0 ? 0 : _q,
109
+ _r = _p.shadowOffsetX,
110
+ shadowOffsetX = _r === void 0 ? 0 : _r,
111
+ _s = _p.shadowOffsetY,
112
+ shadowOffsetY = _s === void 0 ? 0 : _s;
109
113
  var shadowLeft = min[0] - shadowBlur + shadowOffsetX;
110
114
  var shadowRight = max[0] + shadowBlur + shadowOffsetX;
111
115
  var shadowTop = min[1] - shadowBlur + shadowOffsetY;
@@ -1,10 +1,11 @@
1
+ /// <reference types="offscreencanvas" />
1
2
  import { Syringe } from 'mana-syringe';
2
3
  export declare const ContextService: Syringe.DefinedToken;
3
4
  export interface ContextService<Context> {
4
5
  init: () => void;
5
6
  destroy: () => void;
6
7
  getContext: () => Context | null;
7
- getDomElement: () => HTMLElement | null;
8
+ getDomElement: () => HTMLElement | OffscreenCanvas | null;
8
9
  getDPR: () => number;
9
10
  getBoundingClientRect: () => DOMRect | undefined;
10
11
  resize: (width: number, height: number) => void;
@@ -623,11 +623,13 @@ function (_super) {
623
623
  e.eventPhase = e.AT_TARGET;
624
624
  e.currentTarget = e.target;
625
625
  this.notifyTarget(e, type);
626
- if (e.propagationStopped || e.propagationImmediatelyStopped) return; // bubbling phase
626
+ if (e.propagationStopped || e.propagationImmediatelyStopped) return; // find current target in composed path
627
+
628
+ var index = composedPath.indexOf(e.currentTarget); // bubbling phase
627
629
 
628
630
  e.eventPhase = e.BUBBLING_PHASE;
629
631
 
630
- for (var i = 1; i < composedPath.length; i++) {
632
+ for (var i = index + 1; i < composedPath.length; i++) {
631
633
  e.currentTarget = composedPath[i];
632
634
  this.notifyTarget(e, type);
633
635
  if (e.propagationStopped || e.propagationImmediatelyStopped) return;
@@ -1,5 +1,5 @@
1
1
  import { Syringe } from 'mana-syringe';
2
- import { SyncHook, SyncWaterfallHook, AsyncSeriesHook, AsyncSeriesWaterfallHook } from 'tapable';
2
+ import { SyncHook, SyncWaterfallHook, AsyncParallelHook, AsyncSeriesWaterfallHook } from 'tapable';
3
3
  import type { DisplayObject } from '..';
4
4
  import type { EventPosition, InteractivePointerEvent } from '../types';
5
5
  export interface RenderingPlugin {
@@ -27,7 +27,7 @@ export declare class RenderingService {
27
27
  private stats;
28
28
  private zIndexCounter;
29
29
  hooks: {
30
- init: AsyncSeriesHook<[], import("tapable").UnsetAdditionalOptions>;
30
+ init: AsyncParallelHook<[], import("tapable").UnsetAdditionalOptions>;
31
31
  prepare: SyncWaterfallHook<[DisplayObject<any, any>], import("tapable").UnsetAdditionalOptions>;
32
32
  /**
33
33
  * called at beginning of each frame, won't get called if nothing to re-render
@@ -42,7 +42,7 @@ function () {
42
42
  };
43
43
  this.zIndexCounter = 0;
44
44
  this.hooks = {
45
- init: new _tapable.AsyncSeriesHook(),
45
+ init: new _tapable.AsyncParallelHook(),
46
46
  prepare: new _tapable.SyncWaterfallHook(['object']),
47
47
 
48
48
  /**
@@ -30,6 +30,8 @@ export interface SceneGraphService {
30
30
  rotateLocal: (element: INode, degrees: vec3 | number, y?: number, z?: number) => void;
31
31
  rotate: (element: INode, degrees: vec3 | number, y?: number, z?: number) => void;
32
32
  getRotation: (element: INode) => quat;
33
+ setRotation: (element: INode, rotation: quat | number, y?: number, z?: number, w?: number) => void;
34
+ setLocalRotation: (element: INode, rotation: quat | number, y?: number, z?: number, w?: number) => void;
33
35
  getLocalRotation: (element: INode) => quat;
34
36
  getWorldTransform: (element: INode, transform?: Transform) => mat4;
35
37
  getLocalTransform: (element: INode, transform?: Transform) => mat4;
@@ -117,6 +119,8 @@ export declare class DefaultSceneGraphService implements SceneGraphService {
117
119
  * @see https://github.com/antvis/g/blob/master/packages/g-base/src/abstract/element.ts#L665-L676
118
120
  */
119
121
  translate: (element: INode, translation: vec3 | number, y?: number, z?: number) => void;
122
+ setRotation: () => (element: INode, rotation: quat | number, y?: number, z?: number, w?: number) => void;
123
+ setLocalRotation(element: INode, rotation: quat | number, y?: number, z?: number, w?: number): void;
120
124
  dirtifyLocal(element: INode, transform: Transform): void;
121
125
  dirtifyWorld(element: INode, transform: Transform): void;
122
126
  getPosition(element: INode): vec3;
@@ -180,7 +180,7 @@ function () {
180
180
 
181
181
  _glMatrix.quat.invert(parentInvertRotation, parentInvertRotation);
182
182
 
183
- _glMatrix.quat.multiply(parentInvertRotation, parentInvertRotation, rotation);
183
+ _glMatrix.quat.multiply(rotation, parentInvertRotation, rotation);
184
184
 
185
185
  _glMatrix.quat.multiply(transform.localRotation, rotation, rot);
186
186
 
@@ -372,6 +372,34 @@ function () {
372
372
  _this.setPosition(element, tr);
373
373
  };
374
374
  }();
375
+
376
+ this.setRotation = function () {
377
+ var parentInvertRotation = _glMatrix.quat.create();
378
+
379
+ return function (element, rotation, y, z, w) {
380
+ var transform = element.transformable;
381
+
382
+ if (typeof rotation === 'number') {
383
+ rotation = _glMatrix.quat.fromValues(rotation, y, z, w);
384
+ }
385
+
386
+ if (element.parentNode === null || !element.parentNode.transformable) {
387
+ _this.setLocalRotation(element, rotation);
388
+ } else {
389
+ var parentRot = _this.getRotation(element.parentNode);
390
+
391
+ _glMatrix.quat.copy(parentInvertRotation, parentRot);
392
+
393
+ _glMatrix.quat.invert(parentInvertRotation, parentInvertRotation);
394
+
395
+ _glMatrix.quat.multiply(transform.localRotation, parentInvertRotation, rotation);
396
+
397
+ _glMatrix.quat.normalize(transform.localRotation, transform.localRotation);
398
+
399
+ _this.dirtifyLocal(element, transform);
400
+ }
401
+ };
402
+ };
375
403
  }
376
404
 
377
405
  DefaultSceneGraphService.prototype.matches = function (query, root) {
@@ -552,6 +580,18 @@ function () {
552
580
  this.dirtifyLocal(element, transform);
553
581
  };
554
582
 
583
+ DefaultSceneGraphService.prototype.setLocalRotation = function (element, rotation, y, z, w) {
584
+ if (typeof rotation === 'number') {
585
+ rotation = _glMatrix.quat.fromValues(rotation, y, z, w);
586
+ }
587
+
588
+ var transform = element.transformable;
589
+
590
+ _glMatrix.quat.copy(transform.localRotation, rotation);
591
+
592
+ this.dirtifyLocal(element, transform);
593
+ };
594
+
555
595
  DefaultSceneGraphService.prototype.dirtifyLocal = function (element, transform) {
556
596
  if (!transform.localDirtyFlag) {
557
597
  transform.localDirtyFlag = true;
@@ -9,6 +9,7 @@ export interface GeometryAABBUpdater<T extends ParsedBaseStyleProps = any> {
9
9
  depth?: number;
10
10
  x: number;
11
11
  y: number;
12
+ z?: number;
12
13
  offsetX?: number;
13
14
  offsetY?: number;
14
15
  offsetZ?: number;
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="offscreencanvas" />
1
2
  import type { vec2, vec3 } from 'gl-matrix';
2
3
  import type { ParsedPathStyleProps, ParsedPolylineStyleProps } from './display-objects';
3
4
  import type { DisplayObject } from './display-objects';
@@ -171,31 +172,35 @@ export interface CanvasConfig {
171
172
  renderer: IRenderer;
172
173
  /**
173
174
  * 容器
174
- * @type {string|HTMLElement}
175
175
  */
176
- container: string | HTMLElement;
176
+ container?: string | HTMLElement;
177
+ /**
178
+ * support OffscreenCanvas
179
+ */
180
+ canvas?: HTMLCanvasElement | OffscreenCanvas;
181
+ /**
182
+ * window.devicePixelRatio
183
+ */
184
+ devicePixelRatio?: number;
185
+ /**
186
+ * rAF
187
+ */
188
+ requestAnimationFrame?: (callback: FrameRequestCallback) => number;
189
+ cancelAnimationFrame?: (id: number) => void;
177
190
  /**
178
191
  * 画布宽度
179
- * @type {number}
180
192
  */
181
- width: number;
193
+ width?: number;
182
194
  /**
183
195
  * 画布高度
184
- * @type {number}
185
196
  */
186
- height: number;
197
+ height?: number;
187
198
  /**
188
199
  * 画布背景色
189
200
  */
190
201
  background?: ColorType;
191
- /**
192
- * 是否可监听
193
- * @type {boolean}
194
- */
195
- capture?: boolean;
196
202
  /**
197
203
  * 画布的 cursor 样式
198
- * @type {Cursor}
199
204
  */
200
205
  cursor?: Cursor;
201
206
  [key: string]: any;
@@ -3,3 +3,4 @@ import type { Canvas } from '../Canvas';
3
3
  * destroy existed canvas with the same id
4
4
  */
5
5
  export declare function cleanExistedCanvas(container: string | HTMLElement, canvas: Canvas): void;
6
+ export declare const isBrowser: boolean;
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.cleanExistedCanvas = cleanExistedCanvas;
7
+ exports.isBrowser = void 0;
7
8
  var canvasMap = {};
8
9
  var defaultCanvasIdCounter = 0;
9
10
  /**
@@ -11,11 +12,16 @@ var defaultCanvasIdCounter = 0;
11
12
  */
12
13
 
13
14
  function cleanExistedCanvas(container, canvas) {
14
- var id = typeof container === 'string' ? container : container.id || defaultCanvasIdCounter++;
15
+ if (container) {
16
+ var id = typeof container === 'string' ? container : container.id || defaultCanvasIdCounter++;
15
17
 
16
- if (canvasMap[id]) {
17
- canvasMap[id].destroy();
18
+ if (canvasMap[id]) {
19
+ canvasMap[id].destroy();
20
+ }
21
+
22
+ canvasMap[id] = canvas;
18
23
  }
24
+ }
19
25
 
20
- canvasMap[id] = canvas;
21
- }
26
+ var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
27
+ exports.isBrowser = isBrowser;
package/lib/utils/path.js CHANGED
@@ -251,6 +251,12 @@ function commandsToPathString(commands, localTransform, anchor, parsedStyle) {
251
251
  _glMatrix.vec3.transformMat4(p3, p3, localTransform);
252
252
 
253
253
  path = "" + cur[0] + p1[0] + "," + p1[1] + "," + p2[0] + "," + p2[1] + "," + p3[0] + "," + p3[1];
254
+ } else if (cur[0] === 'A') {
255
+ var c = _glMatrix.vec3.fromValues(cur[6] - x, cur[7] - y, 0);
256
+
257
+ _glMatrix.vec3.transformMat4(c, c, localTransform);
258
+
259
+ path = "" + cur[0] + cur[1] + "," + cur[2] + "," + cur[3] + "," + cur[4] + "," + cur[5] + "," + c[0] + "," + c[1];
254
260
  }
255
261
 
256
262
  return prev += path;
@@ -278,8 +284,12 @@ function polygonToCommands(points) {
278
284
  });
279
285
  }
280
286
 
281
- function rectToCommands(width, height, x, y) {
282
- // FIXME: account for radius
287
+ function rectToCommands(width, height, x, y, radius) {
288
+ // @see https://gist.github.com/danielpquinn/dd966af424030d47e476
289
+ if (radius) {
290
+ return [['M', x, radius + y], ['A', radius, radius, 0, 0, 1, radius + x, y], ['L', width - radius + x, y], ['A', radius, radius, 0, 0, 1, width + x, radius + y], ['L', width + x, height - radius + y], ['A', radius, radius, 0, 0, 1, width - radius + x, height + y], ['L', radius + x, height + y], ['A', radius, radius, 0, 0, 1, x, height - radius + y], ['Z']];
291
+ }
292
+
283
293
  return [['M', x, y], ['L', x + width, y], ['L', x + width, y + height], ['L', x, y + height], ['Z']];
284
294
  }
285
295
  /**
@@ -341,8 +351,9 @@ function convertToPath(object) {
341
351
  width = _h.width,
342
352
  height = _h.height,
343
353
  x = _h.x,
344
- y = _h.y;
345
- commands = rectToCommands(width, height, x, y);
354
+ y = _h.y,
355
+ radius = _h.radius;
356
+ commands = rectToCommands(width, height, x, y, radius);
346
357
  break;
347
358
 
348
359
  case _types.SHAPE.Path:
@@ -1,3 +1,6 @@
1
- declare function requestAnimationFrame(fn: FrameRequestCallback): number;
2
- declare function cancelAnimationFrame(handler: number): void;
3
- export { requestAnimationFrame, cancelAnimationFrame };
1
+ /**
2
+ * use user-defined raf instead of requestAnimationFrame
3
+ */
4
+ export declare function patch(requestAFPatch: (callback: Function) => number, cancelAFPatch: (id: number) => void): void;
5
+ export declare const requestAnimationFrame: (callback: Function) => number;
6
+ export declare const cancelAnimationFrame: (id: number) => void;
package/lib/utils/raf.js CHANGED
@@ -3,20 +3,26 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.cancelAnimationFrame = cancelAnimationFrame;
7
- exports.requestAnimationFrame = requestAnimationFrame;
6
+ exports.cancelAnimationFrame = void 0;
7
+ exports.patch = patch;
8
+ exports.requestAnimationFrame = void 0;
9
+
10
+ var _requestAnimationFramePolyfill = require("request-animation-frame-polyfill");
8
11
 
9
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
12
+ // import { requestAnimationFrame, cancelAnimationFrame } from 'request-animation-frame-polyfill';
13
+ // export { requestAnimationFrame, cancelAnimationFrame };
14
+ var requestAF = _requestAnimationFramePolyfill.requestAnimationFrame;
15
+ var cancelAF = _requestAnimationFramePolyfill.cancelAnimationFrame;
16
+ /**
17
+ * use user-defined raf instead of requestAnimationFrame
18
+ */
10
19
 
11
- function requestAnimationFrame(fn) {
12
- var method = (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object' && window.requestAnimationFrame ? window.requestAnimationFrame : // eslint-disable-next-line @typescript-eslint/ban-types
13
- function (f) {
14
- return setTimeout(f, 16);
15
- };
16
- return method(fn);
20
+ function patch(requestAFPatch, cancelAFPatch) {
21
+ requestAF = requestAFPatch;
22
+ cancelAF = cancelAFPatch;
17
23
  }
18
24
 
19
- function cancelAnimationFrame(handler) {
20
- var method = (typeof window === "undefined" ? "undefined" : _typeof(window)) === 'object' && window.cancelAnimationFrame ? window.cancelAnimationFrame : clearTimeout;
21
- return method(handler);
22
- }
25
+ var requestAnimationFrame = requestAF;
26
+ exports.requestAnimationFrame = requestAnimationFrame;
27
+ var cancelAnimationFrame = cancelAF;
28
+ exports.cancelAnimationFrame = cancelAnimationFrame;