@antv/g-lite 1.0.68 → 1.0.70

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/dist/index.js CHANGED
@@ -3259,6 +3259,9 @@ var AsyncParallelHook = /*#__PURE__*/function () {
3259
3259
  this.callbacks = [];
3260
3260
  }
3261
3261
  var _proto = AsyncParallelHook.prototype;
3262
+ _proto.getCallbacksNum = function getCallbacksNum() {
3263
+ return this.callbacks.length;
3264
+ };
3262
3265
  _proto.tapPromise = function tapPromise(options, fn) {
3263
3266
  this.callbacks.push(fn);
3264
3267
  };
@@ -12332,7 +12335,8 @@ var RenderingService = /*#__PURE__*/function () {
12332
12335
  /**
12333
12336
  * called before any frame rendered
12334
12337
  */
12335
- init: new AsyncParallelHook(),
12338
+ init: new SyncHook(),
12339
+ initAsync: new AsyncParallelHook(),
12336
12340
  /**
12337
12341
  * only dirty object which has sth changed will be rendered
12338
12342
  */
@@ -12383,32 +12387,24 @@ var RenderingService = /*#__PURE__*/function () {
12383
12387
  this.context = context;
12384
12388
  }
12385
12389
  var _proto = RenderingService.prototype;
12386
- _proto.init = /*#__PURE__*/function () {
12387
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
12388
- var context;
12389
- return _regeneratorRuntime().wrap(function _callee$(_context) {
12390
- while (1) switch (_context.prev = _context.next) {
12391
- case 0:
12392
- context = _extends({}, this.globalRuntime, this.context); // register rendering plugins
12393
- this.context.renderingPlugins.forEach(function (plugin) {
12394
- plugin.apply(context, runtime);
12395
- });
12396
- // await this.hooks.init.callPromise();
12397
- _context.next = 4;
12398
- return this.hooks.init.promise();
12399
- case 4:
12400
- this.inited = true;
12401
- case 5:
12402
- case "end":
12403
- return _context.stop();
12404
- }
12405
- }, _callee, this);
12406
- }));
12407
- function init() {
12408
- return _init.apply(this, arguments);
12390
+ _proto.init = function init(callback) {
12391
+ var _this = this;
12392
+ var context = _extends({}, this.globalRuntime, this.context);
12393
+ // register rendering plugins
12394
+ this.context.renderingPlugins.forEach(function (plugin) {
12395
+ plugin.apply(context, runtime);
12396
+ });
12397
+ this.hooks.init.call();
12398
+ if (this.hooks.initAsync.getCallbacksNum() === 0) {
12399
+ this.inited = true;
12400
+ callback();
12401
+ } else {
12402
+ this.hooks.initAsync.promise().then(function () {
12403
+ _this.inited = true;
12404
+ callback();
12405
+ });
12409
12406
  }
12410
- return init;
12411
- }();
12407
+ };
12412
12408
  _proto.getStats = function getStats() {
12413
12409
  return this.stats;
12414
12410
  }
@@ -12424,7 +12420,7 @@ var RenderingService = /*#__PURE__*/function () {
12424
12420
  return !enableDirtyRectangleRendering || this.context.renderingContext.renderReasons.has(exports.RenderReason.CAMERA_CHANGED);
12425
12421
  };
12426
12422
  _proto.render = function render(canvasConfig, rerenderCallback) {
12427
- var _this = this;
12423
+ var _this2 = this;
12428
12424
  this.stats.total = 0;
12429
12425
  this.stats.rendered = 0;
12430
12426
  this.zIndexCounter = 0;
@@ -12435,9 +12431,9 @@ var RenderingService = /*#__PURE__*/function () {
12435
12431
  this.renderDisplayObject(renderingContext.root, canvasConfig, renderingContext);
12436
12432
  this.hooks.beginFrame.call();
12437
12433
  renderingContext.renderListCurrentFrame.forEach(function (object) {
12438
- _this.hooks.beforeRender.call(object);
12439
- _this.hooks.render.call(object);
12440
- _this.hooks.afterRender.call(object);
12434
+ _this2.hooks.beforeRender.call(object);
12435
+ _this2.hooks.render.call(object);
12436
+ _this2.hooks.afterRender.call(object);
12441
12437
  });
12442
12438
  this.hooks.endFrame.call();
12443
12439
  renderingContext.renderListCurrentFrame = [];
@@ -12447,7 +12443,7 @@ var RenderingService = /*#__PURE__*/function () {
12447
12443
  // console.log('stats', this.stats);
12448
12444
  };
12449
12445
  _proto.renderDisplayObject = function renderDisplayObject(displayObject, canvasConfig, renderingContext) {
12450
- var _this2 = this;
12446
+ var _this3 = this;
12451
12447
  var _canvasConfig$rendere = canvasConfig.renderer.getConfig(),
12452
12448
  enableDirtyCheck = _canvasConfig$rendere.enableDirtyCheck,
12453
12449
  enableCulling = _canvasConfig$rendere.enableCulling;
@@ -12478,7 +12474,7 @@ var RenderingService = /*#__PURE__*/function () {
12478
12474
  }
12479
12475
  // recursive rendering its children
12480
12476
  (sortable.sorted || displayObject.childNodes).forEach(function (child) {
12481
- _this2.renderDisplayObject(child, canvasConfig, renderingContext);
12477
+ _this3.renderDisplayObject(child, canvasConfig, renderingContext);
12482
12478
  });
12483
12479
  };
12484
12480
  _proto.sort = function sort(displayObject, sortable) {
@@ -13487,7 +13483,9 @@ var TextService = /*#__PURE__*/function () {
13487
13483
  fontSize: 0
13488
13484
  };
13489
13485
  var canvas = this.runtime.offscreenCanvas.getOrCreateCanvas(offscreenCanvas);
13490
- var context = this.runtime.offscreenCanvas.getOrCreateContext(offscreenCanvas);
13486
+ var context = this.runtime.offscreenCanvas.getOrCreateContext(offscreenCanvas, {
13487
+ willReadFrequently: true
13488
+ });
13491
13489
  context.font = font;
13492
13490
  var metricsString = TEXT_METRICS.MetricsString + TEXT_METRICS.BaselineSymbol;
13493
13491
  var width = Math.ceil(context.measureText(metricsString).width);
@@ -14409,20 +14407,12 @@ var PrepareRendererPlugin = /*#__PURE__*/function () {
14409
14407
  runtime.sceneGraphService.dirtifyToRoot(object);
14410
14408
  renderingService.dirtify();
14411
14409
  };
14412
- renderingService.hooks.init.tapPromise(PrepareRendererPlugin.tag, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
14413
- return _regeneratorRuntime().wrap(function _callee$(_context) {
14414
- while (1) switch (_context.prev = _context.next) {
14415
- case 0:
14416
- canvas.addEventListener(exports.ElementEvent.MOUNTED, handleMounted);
14417
- canvas.addEventListener(exports.ElementEvent.UNMOUNTED, handleUnmounted);
14418
- canvas.addEventListener(exports.ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
14419
- canvas.addEventListener(exports.ElementEvent.BOUNDS_CHANGED, handleBoundsChanged);
14420
- case 4:
14421
- case "end":
14422
- return _context.stop();
14423
- }
14424
- }, _callee);
14425
- })));
14410
+ renderingService.hooks.init.tap(PrepareRendererPlugin.tag, function () {
14411
+ canvas.addEventListener(exports.ElementEvent.MOUNTED, handleMounted);
14412
+ canvas.addEventListener(exports.ElementEvent.UNMOUNTED, handleUnmounted);
14413
+ canvas.addEventListener(exports.ElementEvent.ATTR_MODIFIED, handleAttributeChanged);
14414
+ canvas.addEventListener(exports.ElementEvent.BOUNDS_CHANGED, handleBoundsChanged);
14415
+ });
14426
14416
  renderingService.hooks.destroy.tap(PrepareRendererPlugin.tag, function () {
14427
14417
  canvas.removeEventListener(exports.ElementEvent.MOUNTED, handleMounted);
14428
14418
  canvas.removeEventListener(exports.ElementEvent.UNMOUNTED, handleUnmounted);
@@ -14509,6 +14499,7 @@ function isCanvas(value) {
14509
14499
  CanvasEvent["AFTER_DESTROY"] = "afterdestroy";
14510
14500
  CanvasEvent["RESIZE"] = "resize";
14511
14501
  CanvasEvent["DIRTY_RECTANGLE"] = "dirtyrectangle";
14502
+ CanvasEvent["RENDERER_CHANGED"] = "rendererchanged";
14512
14503
  })(exports.CanvasEvent || (exports.CanvasEvent = {}));
14513
14504
  var DEFAULT_CAMERA_Z = 500;
14514
14505
  var DEFAULT_CAMERA_NEAR = 0.1;
@@ -14657,23 +14648,7 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
14657
14648
  alwaysTriggerPointerEventOnCanvas: alwaysTriggerPointerEventOnCanvas
14658
14649
  });
14659
14650
  _this.initDefaultCamera(canvasWidth, canvasHeight);
14660
- _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
14661
- return _regeneratorRuntime().wrap(function _callee$(_context) {
14662
- while (1) switch (_context.prev = _context.next) {
14663
- case 0:
14664
- _context.next = 2;
14665
- return _this.initRenderer(renderer);
14666
- case 2:
14667
- _this.dispatchEvent(new CustomEvent(exports.CanvasEvent.READY));
14668
- if (_this.readyPromise) {
14669
- _this.resolveReadyPromise();
14670
- }
14671
- case 4:
14672
- case "end":
14673
- return _context.stop();
14674
- }
14675
- }, _callee);
14676
- }))();
14651
+ _this.initRenderer(renderer, true);
14677
14652
  return _this;
14678
14653
  }
14679
14654
  var _proto = Canvas.prototype;
@@ -14851,123 +14826,120 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
14851
14826
  var _this4 = this;
14852
14827
  var tick = function tick() {
14853
14828
  _this4.render();
14854
- _this4.frameId = requestAnimationFrame(tick);
14829
+ _this4.frameId = _this4.requestAnimationFrame(tick);
14855
14830
  };
14856
14831
  tick();
14857
14832
  };
14858
- _proto.initRenderer = /*#__PURE__*/function () {
14859
- var _initRenderer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(renderer) {
14860
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
14861
- while (1) switch (_context2.prev = _context2.next) {
14862
- case 0:
14863
- if (renderer) {
14864
- _context2.next = 2;
14865
- break;
14866
- }
14867
- throw new Error('Renderer is required.');
14868
- case 2:
14869
- // reset
14870
- this.inited = false;
14871
- this.readyPromise = undefined;
14872
- // FIXME: should re-create here?
14873
- this.context.rBushRoot = new RBush__default['default']();
14874
- // reset rendering plugins
14875
- this.context.renderingPlugins = [];
14876
- this.context.renderingPlugins.push(new EventPlugin(), new PrepareRendererPlugin(), new DirtyCheckPlugin(), new CullingPlugin([new FrustumCullingStrategy()]));
14877
- //
14878
- this.loadRendererContainerModule(renderer);
14879
- // init context service
14880
- this.context.contextService = new this.context.ContextService(_extends({}, runtime, this.context));
14881
- // init rendering service
14882
- this.context.renderingService = new RenderingService(runtime, this.context);
14883
- // init event service
14884
- this.context.eventService = new EventService(runtime, this.context);
14885
- this.context.eventService.init();
14886
- _context2.next = 14;
14887
- return this.context.contextService.init();
14888
- case 14:
14889
- _context2.next = 16;
14890
- return this.context.renderingService.init();
14891
- case 16:
14892
- this.inited = true;
14893
- this.getRoot().forEach(function (node) {
14894
- var renderable = node.renderable;
14895
- if (renderable) {
14896
- renderable.renderBoundsDirty = true;
14897
- renderable.boundsDirty = true;
14898
- renderable.dirty = true;
14899
- }
14900
- });
14901
- // keep current scenegraph unchanged, just trigger mounted event
14902
- this.mountChildren(this.getRoot());
14903
- if (renderer.getConfig().enableAutoRendering) {
14904
- this.run();
14905
- }
14906
- case 20:
14907
- case "end":
14908
- return _context2.stop();
14909
- }
14910
- }, _callee2, this);
14911
- }));
14912
- function initRenderer(_x) {
14913
- return _initRenderer.apply(this, arguments);
14833
+ _proto.initRenderer = function initRenderer(renderer, firstContentfullPaint) {
14834
+ var _this5 = this;
14835
+ if (firstContentfullPaint === void 0) {
14836
+ firstContentfullPaint = false;
14914
14837
  }
14915
- return initRenderer;
14916
- }();
14838
+ if (!renderer) {
14839
+ throw new Error('Renderer is required.');
14840
+ }
14841
+ // reset
14842
+ this.inited = false;
14843
+ this.readyPromise = undefined;
14844
+ // FIXME: should re-create here?
14845
+ this.context.rBushRoot = new RBush__default['default']();
14846
+ // reset rendering plugins
14847
+ this.context.renderingPlugins = [];
14848
+ this.context.renderingPlugins.push(new EventPlugin(), new PrepareRendererPlugin(), new DirtyCheckPlugin(), new CullingPlugin([new FrustumCullingStrategy()]));
14849
+ //
14850
+ this.loadRendererContainerModule(renderer);
14851
+ // init context service
14852
+ this.context.contextService = new this.context.ContextService(_extends({}, runtime, this.context));
14853
+ // init rendering service
14854
+ this.context.renderingService = new RenderingService(runtime, this.context);
14855
+ // init event service
14856
+ this.context.eventService = new EventService(runtime, this.context);
14857
+ this.context.eventService.init();
14858
+ if (this.context.contextService.init) {
14859
+ this.context.contextService.init();
14860
+ this.initRenderingService(renderer, firstContentfullPaint, true);
14861
+ } else {
14862
+ this.context.contextService.initAsync().then(function () {
14863
+ _this5.initRenderingService(renderer, firstContentfullPaint);
14864
+ });
14865
+ }
14866
+ };
14867
+ _proto.initRenderingService = function initRenderingService(renderer, firstContentfullPaint, async) {
14868
+ var _this6 = this;
14869
+ if (firstContentfullPaint === void 0) {
14870
+ firstContentfullPaint = false;
14871
+ }
14872
+ if (async === void 0) {
14873
+ async = false;
14874
+ }
14875
+ this.context.renderingService.init(function () {
14876
+ _this6.inited = true;
14877
+ if (firstContentfullPaint) {
14878
+ if (async) {
14879
+ _this6.requestAnimationFrame(function () {
14880
+ _this6.dispatchEvent(new CustomEvent(exports.CanvasEvent.READY));
14881
+ });
14882
+ } else {
14883
+ _this6.dispatchEvent(new CustomEvent(exports.CanvasEvent.READY));
14884
+ }
14885
+ if (_this6.readyPromise) {
14886
+ _this6.resolveReadyPromise();
14887
+ }
14888
+ } else {
14889
+ _this6.dispatchEvent(new CustomEvent(exports.CanvasEvent.RENDERER_CHANGED));
14890
+ }
14891
+ if (!firstContentfullPaint) {
14892
+ _this6.getRoot().forEach(function (node) {
14893
+ var renderable = node.renderable;
14894
+ if (renderable) {
14895
+ renderable.renderBoundsDirty = true;
14896
+ renderable.boundsDirty = true;
14897
+ renderable.dirty = true;
14898
+ }
14899
+ });
14900
+ }
14901
+ // keep current scenegraph unchanged, just trigger mounted event
14902
+ _this6.mountChildren(_this6.getRoot());
14903
+ if (renderer.getConfig().enableAutoRendering) {
14904
+ _this6.run();
14905
+ }
14906
+ });
14907
+ };
14917
14908
  _proto.loadRendererContainerModule = function loadRendererContainerModule(renderer) {
14918
- var _this5 = this;
14909
+ var _this7 = this;
14919
14910
  // load other container modules provided by g-canvas/g-svg/g-webgl
14920
14911
  var plugins = renderer.getPlugins();
14921
14912
  plugins.forEach(function (plugin) {
14922
- plugin.context = _this5.context;
14913
+ plugin.context = _this7.context;
14923
14914
  plugin.init(runtime);
14924
14915
  });
14925
14916
  };
14926
- _proto.setRenderer = /*#__PURE__*/function () {
14927
- var _setRenderer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(renderer) {
14928
- var canvasConfig, oldRenderer;
14929
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
14930
- while (1) switch (_context3.prev = _context3.next) {
14931
- case 0:
14932
- // update canvas' config
14933
- canvasConfig = this.getConfig();
14934
- if (!(canvasConfig.renderer === renderer)) {
14935
- _context3.next = 3;
14936
- break;
14937
- }
14938
- return _context3.abrupt("return");
14939
- case 3:
14940
- oldRenderer = canvasConfig.renderer;
14941
- canvasConfig.renderer = renderer;
14942
- // keep all children undestroyed
14943
- this.destroy(false, true);
14944
- // destroy all plugins, reverse will mutate origin array
14945
- [].concat(oldRenderer === null || oldRenderer === void 0 ? void 0 : oldRenderer.getPlugins()).reverse().forEach(function (plugin) {
14946
- plugin.destroy(runtime);
14947
- });
14948
- _context3.next = 9;
14949
- return this.initRenderer(renderer);
14950
- case 9:
14951
- case "end":
14952
- return _context3.stop();
14953
- }
14954
- }, _callee3, this);
14955
- }));
14956
- function setRenderer(_x2) {
14957
- return _setRenderer.apply(this, arguments);
14917
+ _proto.setRenderer = function setRenderer(renderer) {
14918
+ // update canvas' config
14919
+ var canvasConfig = this.getConfig();
14920
+ if (canvasConfig.renderer === renderer) {
14921
+ return;
14958
14922
  }
14959
- return setRenderer;
14960
- }();
14923
+ var oldRenderer = canvasConfig.renderer;
14924
+ canvasConfig.renderer = renderer;
14925
+ // keep all children undestroyed
14926
+ this.destroy(false, true);
14927
+ // destroy all plugins, reverse will mutate origin array
14928
+ [].concat(oldRenderer === null || oldRenderer === void 0 ? void 0 : oldRenderer.getPlugins()).reverse().forEach(function (plugin) {
14929
+ plugin.destroy(runtime);
14930
+ });
14931
+ this.initRenderer(renderer);
14932
+ };
14961
14933
  _proto.setCursor = function setCursor(cursor) {
14962
14934
  var canvasConfig = this.getConfig();
14963
14935
  canvasConfig.cursor = cursor;
14964
14936
  this.getContextService().applyCursorStyle(cursor);
14965
14937
  };
14966
14938
  _proto.unmountChildren = function unmountChildren(parent) {
14967
- var _this6 = this;
14939
+ var _this8 = this;
14968
14940
  // unmountChildren recursively
14969
14941
  parent.childNodes.forEach(function (child) {
14970
- _this6.unmountChildren(child);
14942
+ _this8.unmountChildren(child);
14971
14943
  });
14972
14944
  if (this.inited) {
14973
14945
  if (parent.isMutationObserved) {
@@ -14990,7 +14962,7 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
14990
14962
  }
14991
14963
  };
14992
14964
  _proto.mountChildren = function mountChildren(parent) {
14993
- var _this7 = this;
14965
+ var _this9 = this;
14994
14966
  if (this.inited) {
14995
14967
  if (!parent.isConnected) {
14996
14968
  parent.ownerDocument = this.document;
@@ -15007,7 +14979,7 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
15007
14979
  }
15008
14980
  // recursively mount children
15009
14981
  parent.childNodes.forEach(function (child) {
15010
- _this7.mountChildren(child);
14982
+ _this9.mountChildren(child);
15011
14983
  });
15012
14984
  // trigger after mounted
15013
14985
  if (parent.isCustomElement) {
@@ -15051,11 +15023,11 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
15051
15023
  _createClass(Canvas, [{
15052
15024
  key: "ready",
15053
15025
  get: function get() {
15054
- var _this8 = this;
15026
+ var _this10 = this;
15055
15027
  if (!this.readyPromise) {
15056
15028
  this.readyPromise = new Promise(function (resolve) {
15057
- _this8.resolveReadyPromise = function () {
15058
- resolve(_this8);
15029
+ _this10.resolveReadyPromise = function () {
15030
+ resolve(_this10);
15059
15031
  };
15060
15032
  });
15061
15033
  if (this.inited) {