index.esm.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. import { setDOMSize, RenderReason, isBrowser, AbstractRendererPlugin, AbstractRenderer } from '@antv/g-lite';
  2. import { Plugin as Plugin$1 } from '@antv/g-plugin-canvas-path-generator';
  3. import * as CanvasPathGenerator from '@antv/g-plugin-canvas-path-generator';
  4. export { CanvasPathGenerator };
  5. import { Plugin as Plugin$4 } from '@antv/g-plugin-canvas-picker';
  6. import * as CanvasPicker from '@antv/g-plugin-canvas-picker';
  7. export { CanvasPicker };
  8. import { Plugin as Plugin$2 } from '@antv/g-plugin-canvas-renderer';
  9. import * as CanvasRenderer from '@antv/g-plugin-canvas-renderer';
  10. export { CanvasRenderer };
  11. import { Plugin as Plugin$3 } from '@antv/g-plugin-dom-interaction';
  12. import * as DomInteraction from '@antv/g-plugin-dom-interaction';
  13. export { DomInteraction };
  14. import { Plugin as Plugin$5 } from '@antv/g-plugin-html-renderer';
  15. import * as HTMLRenderer from '@antv/g-plugin-html-renderer';
  16. export { HTMLRenderer };
  17. import { Plugin } from '@antv/g-plugin-image-loader';
  18. import * as ImageLoader from '@antv/g-plugin-image-loader';
  19. export { ImageLoader };
  20. import { isString } from '@antv/util';
  21. function _regeneratorRuntime() {
  22. _regeneratorRuntime = function () {
  23. return exports;
  24. };
  25. var exports = {},
  26. Op = Object.prototype,
  27. hasOwn = Op.hasOwnProperty,
  28. defineProperty = Object.defineProperty || function (obj, key, desc) {
  29. obj[key] = desc.value;
  30. },
  31. $Symbol = "function" == typeof Symbol ? Symbol : {},
  32. iteratorSymbol = $Symbol.iterator || "@@iterator",
  33. asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
  34. toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  35. function define(obj, key, value) {
  36. return Object.defineProperty(obj, key, {
  37. value: value,
  38. enumerable: !0,
  39. configurable: !0,
  40. writable: !0
  41. }), obj[key];
  42. }
  43. try {
  44. define({}, "");
  45. } catch (err) {
  46. define = function (obj, key, value) {
  47. return obj[key] = value;
  48. };
  49. }
  50. function wrap(innerFn, outerFn, self, tryLocsList) {
  51. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
  52. generator = Object.create(protoGenerator.prototype),
  53. context = new Context(tryLocsList || []);
  54. return defineProperty(generator, "_invoke", {
  55. value: makeInvokeMethod(innerFn, self, context)
  56. }), generator;
  57. }
  58. function tryCatch(fn, obj, arg) {
  59. try {
  60. return {
  61. type: "normal",
  62. arg: fn.call(obj, arg)
  63. };
  64. } catch (err) {
  65. return {
  66. type: "throw",
  67. arg: err
  68. };
  69. }
  70. }
  71. exports.wrap = wrap;
  72. var ContinueSentinel = {};
  73. function Generator() {}
  74. function GeneratorFunction() {}
  75. function GeneratorFunctionPrototype() {}
  76. var IteratorPrototype = {};
  77. define(IteratorPrototype, iteratorSymbol, function () {
  78. return this;
  79. });
  80. var getProto = Object.getPrototypeOf,
  81. NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  82. NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
  83. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  84. function defineIteratorMethods(prototype) {
  85. ["next", "throw", "return"].forEach(function (method) {
  86. define(prototype, method, function (arg) {
  87. return this._invoke(method, arg);
  88. });
  89. });
  90. }
  91. function AsyncIterator(generator, PromiseImpl) {
  92. function invoke(method, arg, resolve, reject) {
  93. var record = tryCatch(generator[method], generator, arg);
  94. if ("throw" !== record.type) {
  95. var result = record.arg,
  96. value = result.value;
  97. return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
  98. invoke("next", value, resolve, reject);
  99. }, function (err) {
  100. invoke("throw", err, resolve, reject);
  101. }) : PromiseImpl.resolve(value).then(function (unwrapped) {
  102. result.value = unwrapped, resolve(result);
  103. }, function (error) {
  104. return invoke("throw", error, resolve, reject);
  105. });
  106. }
  107. reject(record.arg);
  108. }
  109. var previousPromise;
  110. defineProperty(this, "_invoke", {
  111. value: function (method, arg) {
  112. function callInvokeWithMethodAndArg() {
  113. return new PromiseImpl(function (resolve, reject) {
  114. invoke(method, arg, resolve, reject);
  115. });
  116. }
  117. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  118. }
  119. });
  120. }
  121. function makeInvokeMethod(innerFn, self, context) {
  122. var state = "suspendedStart";
  123. return function (method, arg) {
  124. if ("executing" === state) throw new Error("Generator is already running");
  125. if ("completed" === state) {
  126. if ("throw" === method) throw arg;
  127. return doneResult();
  128. }
  129. for (context.method = method, context.arg = arg;;) {
  130. var delegate = context.delegate;
  131. if (delegate) {
  132. var delegateResult = maybeInvokeDelegate(delegate, context);
  133. if (delegateResult) {
  134. if (delegateResult === ContinueSentinel) continue;
  135. return delegateResult;
  136. }
  137. }
  138. if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
  139. if ("suspendedStart" === state) throw state = "completed", context.arg;
  140. context.dispatchException(context.arg);
  141. } else "return" === context.method && context.abrupt("return", context.arg);
  142. state = "executing";
  143. var record = tryCatch(innerFn, self, context);
  144. if ("normal" === record.type) {
  145. if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
  146. return {
  147. value: record.arg,
  148. done: context.done
  149. };
  150. }
  151. "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
  152. }
  153. };
  154. }
  155. function maybeInvokeDelegate(delegate, context) {
  156. var methodName = context.method,
  157. method = delegate.iterator[methodName];
  158. if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel;
  159. var record = tryCatch(method, delegate.iterator, context.arg);
  160. if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
  161. var info = record.arg;
  162. return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
  163. }
  164. function pushTryEntry(locs) {
  165. var entry = {
  166. tryLoc: locs[0]
  167. };
  168. 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
  169. }
  170. function resetTryEntry(entry) {
  171. var record = entry.completion || {};
  172. record.type = "normal", delete record.arg, entry.completion = record;
  173. }
  174. function Context(tryLocsList) {
  175. this.tryEntries = [{
  176. tryLoc: "root"
  177. }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
  178. }
  179. function values(iterable) {
  180. if (iterable) {
  181. var iteratorMethod = iterable[iteratorSymbol];
  182. if (iteratorMethod) return iteratorMethod.call(iterable);
  183. if ("function" == typeof iterable.next) return iterable;
  184. if (!isNaN(iterable.length)) {
  185. var i = -1,
  186. next = function next() {
  187. for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
  188. return next.value = undefined, next.done = !0, next;
  189. };
  190. return next.next = next;
  191. }
  192. }
  193. return {
  194. next: doneResult
  195. };
  196. }
  197. function doneResult() {
  198. return {
  199. value: undefined,
  200. done: !0
  201. };
  202. }
  203. return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
  204. value: GeneratorFunctionPrototype,
  205. configurable: !0
  206. }), defineProperty(GeneratorFunctionPrototype, "constructor", {
  207. value: GeneratorFunction,
  208. configurable: !0
  209. }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
  210. var ctor = "function" == typeof genFun && genFun.constructor;
  211. return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
  212. }, exports.mark = function (genFun) {
  213. return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
  214. }, exports.awrap = function (arg) {
  215. return {
  216. __await: arg
  217. };
  218. }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
  219. return this;
  220. }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
  221. void 0 === PromiseImpl && (PromiseImpl = Promise);
  222. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
  223. return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
  224. return result.done ? result.value : iter.next();
  225. });
  226. }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
  227. return this;
  228. }), define(Gp, "toString", function () {
  229. return "[object Generator]";
  230. }), exports.keys = function (val) {
  231. var object = Object(val),
  232. keys = [];
  233. for (var key in object) keys.push(key);
  234. return keys.reverse(), function next() {
  235. for (; keys.length;) {
  236. var key = keys.pop();
  237. if (key in object) return next.value = key, next.done = !1, next;
  238. }
  239. return next.done = !0, next;
  240. };
  241. }, exports.values = values, Context.prototype = {
  242. constructor: Context,
  243. reset: function (skipTempReset) {
  244. if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
  245. },
  246. stop: function () {
  247. this.done = !0;
  248. var rootRecord = this.tryEntries[0].completion;
  249. if ("throw" === rootRecord.type) throw rootRecord.arg;
  250. return this.rval;
  251. },
  252. dispatchException: function (exception) {
  253. if (this.done) throw exception;
  254. var context = this;
  255. function handle(loc, caught) {
  256. return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
  257. }
  258. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  259. var entry = this.tryEntries[i],
  260. record = entry.completion;
  261. if ("root" === entry.tryLoc) return handle("end");
  262. if (entry.tryLoc <= this.prev) {
  263. var hasCatch = hasOwn.call(entry, "catchLoc"),
  264. hasFinally = hasOwn.call(entry, "finallyLoc");
  265. if (hasCatch && hasFinally) {
  266. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  267. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  268. } else if (hasCatch) {
  269. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  270. } else {
  271. if (!hasFinally) throw new Error("try statement without catch or finally");
  272. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  273. }
  274. }
  275. }
  276. },
  277. abrupt: function (type, arg) {
  278. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  279. var entry = this.tryEntries[i];
  280. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  281. var finallyEntry = entry;
  282. break;
  283. }
  284. }
  285. finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
  286. var record = finallyEntry ? finallyEntry.completion : {};
  287. return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
  288. },
  289. complete: function (record, afterLoc) {
  290. if ("throw" === record.type) throw record.arg;
  291. return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
  292. },
  293. finish: function (finallyLoc) {
  294. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  295. var entry = this.tryEntries[i];
  296. if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
  297. }
  298. },
  299. catch: function (tryLoc) {
  300. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  301. var entry = this.tryEntries[i];
  302. if (entry.tryLoc === tryLoc) {
  303. var record = entry.completion;
  304. if ("throw" === record.type) {
  305. var thrown = record.arg;
  306. resetTryEntry(entry);
  307. }
  308. return thrown;
  309. }
  310. }
  311. throw new Error("illegal catch attempt");
  312. },
  313. delegateYield: function (iterable, resultName, nextLoc) {
  314. return this.delegate = {
  315. iterator: values(iterable),
  316. resultName: resultName,
  317. nextLoc: nextLoc
  318. }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
  319. }
  320. }, exports;
  321. }
  322. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  323. try {
  324. var info = gen[key](arg);
  325. var value = info.value;
  326. } catch (error) {
  327. reject(error);
  328. return;
  329. }
  330. if (info.done) {
  331. resolve(value);
  332. } else {
  333. Promise.resolve(value).then(_next, _throw);
  334. }
  335. }
  336. function _asyncToGenerator(fn) {
  337. return function () {
  338. var self = this,
  339. args = arguments;
  340. return new Promise(function (resolve, reject) {
  341. var gen = fn.apply(self, args);
  342. function _next(value) {
  343. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
  344. }
  345. function _throw(err) {
  346. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
  347. }
  348. _next(undefined);
  349. });
  350. };
  351. }
  352. function _inheritsLoose(subClass, superClass) {
  353. subClass.prototype = Object.create(superClass.prototype);
  354. subClass.prototype.constructor = subClass;
  355. _setPrototypeOf(subClass, superClass);
  356. }
  357. function _setPrototypeOf(o, p) {
  358. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  359. o.__proto__ = p;
  360. return o;
  361. };
  362. return _setPrototypeOf(o, p);
  363. }
  364. var Canvas2DContextService = /*#__PURE__*/function () {
  365. function Canvas2DContextService(context) {
  366. this.$container = void 0;
  367. this.$canvas = void 0;
  368. this.dpr = void 0;
  369. this.context = void 0;
  370. this.canvasConfig = void 0;
  371. this.renderingContext = void 0;
  372. this.renderingContext = context.renderingContext;
  373. this.canvasConfig = context.config;
  374. }
  375. var _proto = Canvas2DContextService.prototype;
  376. _proto.init = function init() {
  377. var _this$canvasConfig = this.canvasConfig,
  378. container = _this$canvasConfig.container,
  379. canvas = _this$canvasConfig.canvas;
  380. if (canvas) {
  381. this.$canvas = canvas;
  382. if (container && canvas.parentElement !== container) {
  383. container.appendChild(canvas);
  384. }
  385. this.$container = canvas.parentElement;
  386. this.canvasConfig.container = this.$container;
  387. } else if (container) {
  388. // create container
  389. this.$container = isString(container) ? document.getElementById(container) : container;
  390. if (this.$container) {
  391. // create canvas
  392. var $canvas = document.createElement('canvas');
  393. this.$container.appendChild($canvas);
  394. if (!this.$container.style.position) {
  395. this.$container.style.position = 'relative';
  396. }
  397. this.$canvas = $canvas;
  398. }
  399. }
  400. this.context = this.$canvas.getContext('2d');
  401. this.resize(this.canvasConfig.width, this.canvasConfig.height);
  402. };
  403. _proto.getContext = function getContext() {
  404. return this.context;
  405. };
  406. _proto.getDomElement = function getDomElement() {
  407. return this.$canvas;
  408. };
  409. _proto.getDPR = function getDPR() {
  410. return this.dpr;
  411. };
  412. _proto.getBoundingClientRect = function getBoundingClientRect() {
  413. if (this.$canvas.getBoundingClientRect) {
  414. return this.$canvas.getBoundingClientRect();
  415. }
  416. };
  417. _proto.destroy = function destroy() {
  418. // @ts-ignore
  419. if (this.$container && this.$canvas && this.$canvas.parentNode) {
  420. // destroy context
  421. // @ts-ignore
  422. this.$container.removeChild(this.$canvas);
  423. }
  424. };
  425. _proto.resize = function resize(width, height) {
  426. var devicePixelRatio = this.canvasConfig.devicePixelRatio;
  427. // use user-defined dpr first
  428. var dpr = devicePixelRatio || isBrowser && window.devicePixelRatio || 1;
  429. dpr = dpr >= 1 ? Math.ceil(dpr) : 1;
  430. this.dpr = dpr;
  431. if (this.$canvas) {
  432. // set canvas width & height
  433. this.$canvas.width = this.dpr * width;
  434. this.$canvas.height = this.dpr * height;
  435. // set CSS style width & height
  436. setDOMSize(this.$canvas, width, height);
  437. // const dpr = this.getDPR();
  438. // scale all drawing operations by the dpr
  439. // @see https://www.html5rocks.com/en/tutorials/canvas/hidpi/
  440. // this.context.scale(dpr, dpr);
  441. }
  442. this.renderingContext.renderReasons.add(RenderReason.CAMERA_CHANGED);
  443. };
  444. _proto.applyCursorStyle = function applyCursorStyle(cursor) {
  445. if (this.$container && this.$container.style) {
  446. this.$container.style.cursor = cursor;
  447. }
  448. };
  449. _proto.toDataURL = /*#__PURE__*/function () {
  450. var _toDataURL = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(options) {
  451. var _options, type, encoderOptions;
  452. return _regeneratorRuntime().wrap(function _callee$(_context) {
  453. while (1) switch (_context.prev = _context.next) {
  454. case 0:
  455. if (options === void 0) {
  456. options = {};
  457. }
  458. _options = options, type = _options.type, encoderOptions = _options.encoderOptions;
  459. return _context.abrupt("return", this.context.canvas.toDataURL(type, encoderOptions));
  460. case 3:
  461. case "end":
  462. return _context.stop();
  463. }
  464. }, _callee, this);
  465. }));
  466. function toDataURL(_x) {
  467. return _toDataURL.apply(this, arguments);
  468. }
  469. return toDataURL;
  470. }();
  471. return Canvas2DContextService;
  472. }();
  473. var ContextRegisterPlugin = /*#__PURE__*/function (_AbstractRendererPlug) {
  474. _inheritsLoose(ContextRegisterPlugin, _AbstractRendererPlug);
  475. function ContextRegisterPlugin() {
  476. var _this;
  477. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  478. args[_key] = arguments[_key];
  479. }
  480. _this = _AbstractRendererPlug.call.apply(_AbstractRendererPlug, [this].concat(args)) || this;
  481. _this.name = 'canvas-context-register';
  482. return _this;
  483. }
  484. var _proto = ContextRegisterPlugin.prototype;
  485. _proto.init = function init() {
  486. this.context.ContextService = Canvas2DContextService;
  487. };
  488. _proto.destroy = function destroy() {
  489. delete this.context.ContextService;
  490. };
  491. return ContextRegisterPlugin;
  492. }(AbstractRendererPlugin);
  493. var Renderer = /*#__PURE__*/function (_AbstractRenderer) {
  494. _inheritsLoose(Renderer, _AbstractRenderer);
  495. function Renderer(config) {
  496. var _this;
  497. _this = _AbstractRenderer.call(this, config) || this;
  498. // register Canvas2DContext
  499. _this.registerPlugin(new ContextRegisterPlugin());
  500. _this.registerPlugin(new Plugin());
  501. _this.registerPlugin(new Plugin$1());
  502. // enable rendering with Canvas2D API
  503. _this.registerPlugin(new Plugin$2());
  504. _this.registerPlugin(new Plugin$3());
  505. // enable picking with Canvas2D API
  506. _this.registerPlugin(new Plugin$4());
  507. // render HTML component
  508. _this.registerPlugin(new Plugin$5());
  509. return _this;
  510. }
  511. return Renderer;
  512. }(AbstractRenderer);
  513. export { Renderer };