@antv/g-canvas 1.2.1 → 1.3.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/dist/index.js CHANGED
@@ -419,54 +419,11 @@ function _asyncToGenerator(fn) {
419
419
  };
420
420
  }
421
421
 
422
- function _classCallCheck(instance, Constructor) {
423
- if (!(instance instanceof Constructor)) {
424
- throw new TypeError("Cannot call a class as a function");
425
- }
426
- }
427
-
428
- function _defineProperties(target, props) {
429
- for (var i = 0; i < props.length; i++) {
430
- var descriptor = props[i];
431
- descriptor.enumerable = descriptor.enumerable || false;
432
- descriptor.configurable = true;
433
- if ("value" in descriptor) descriptor.writable = true;
434
- Object.defineProperty(target, descriptor.key, descriptor);
435
- }
436
- }
437
-
438
- function _createClass(Constructor, protoProps, staticProps) {
439
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
440
- if (staticProps) _defineProperties(Constructor, staticProps);
441
- Object.defineProperty(Constructor, "prototype", {
442
- writable: false
443
- });
444
- return Constructor;
445
- }
446
-
447
- function _inherits(subClass, superClass) {
448
- if (typeof superClass !== "function" && superClass !== null) {
449
- throw new TypeError("Super expression must either be null or a function");
450
- }
451
-
452
- subClass.prototype = Object.create(superClass && superClass.prototype, {
453
- constructor: {
454
- value: subClass,
455
- writable: true,
456
- configurable: true
457
- }
458
- });
459
- Object.defineProperty(subClass, "prototype", {
460
- writable: false
461
- });
462
- if (superClass) _setPrototypeOf(subClass, superClass);
463
- }
422
+ function _inheritsLoose(subClass, superClass) {
423
+ subClass.prototype = Object.create(superClass.prototype);
424
+ subClass.prototype.constructor = subClass;
464
425
 
465
- function _getPrototypeOf(o) {
466
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
467
- return o.__proto__ || Object.getPrototypeOf(o);
468
- };
469
- return _getPrototypeOf(o);
426
+ _setPrototypeOf(subClass, superClass);
470
427
  }
471
428
 
472
429
  function _setPrototypeOf(o, p) {
@@ -477,60 +434,8 @@ function _setPrototypeOf(o, p) {
477
434
  return _setPrototypeOf(o, p);
478
435
  }
479
436
 
480
- function _isNativeReflectConstruct() {
481
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
482
- if (Reflect.construct.sham) return false;
483
- if (typeof Proxy === "function") return true;
484
-
485
- try {
486
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
487
- return true;
488
- } catch (e) {
489
- return false;
490
- }
491
- }
492
-
493
- function _assertThisInitialized(self) {
494
- if (self === void 0) {
495
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
496
- }
497
-
498
- return self;
499
- }
500
-
501
- function _possibleConstructorReturn(self, call) {
502
- if (call && (typeof call === "object" || typeof call === "function")) {
503
- return call;
504
- } else if (call !== void 0) {
505
- throw new TypeError("Derived constructors may only return object or undefined");
506
- }
507
-
508
- return _assertThisInitialized(self);
509
- }
510
-
511
- function _createSuper(Derived) {
512
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
513
-
514
- return function _createSuperInternal() {
515
- var Super = _getPrototypeOf(Derived),
516
- result;
517
-
518
- if (hasNativeReflectConstruct) {
519
- var NewTarget = _getPrototypeOf(this).constructor;
520
-
521
- result = Reflect.construct(Super, arguments, NewTarget);
522
- } else {
523
- result = Super.apply(this, arguments);
524
- }
525
-
526
- return _possibleConstructorReturn(this, result);
527
- };
528
- }
529
-
530
437
  var Canvas2DContextService = /*#__PURE__*/function () {
531
438
  function Canvas2DContextService() {
532
- _classCallCheck(this, Canvas2DContextService);
533
-
534
439
  this.$container = void 0;
535
440
  this.$canvas = void 0;
536
441
  this.dpr = void 0;
@@ -538,120 +443,142 @@ var Canvas2DContextService = /*#__PURE__*/function () {
538
443
  this.canvasConfig = void 0;
539
444
  }
540
445
 
541
- _createClass(Canvas2DContextService, [{
542
- key: "init",
543
- value: function () {
544
- var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
545
- var _this$canvasConfig, container, canvas, devicePixelRatio, $canvas, dpr;
446
+ var _proto = Canvas2DContextService.prototype;
546
447
 
547
- return _regeneratorRuntime().wrap(function _callee$(_context) {
548
- while (1) {
549
- switch (_context.prev = _context.next) {
550
- case 0:
551
- _this$canvasConfig = this.canvasConfig, container = _this$canvasConfig.container, canvas = _this$canvasConfig.canvas, devicePixelRatio = _this$canvasConfig.devicePixelRatio;
448
+ _proto.init = /*#__PURE__*/function () {
449
+ var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
450
+ var _this$canvasConfig, container, canvas, devicePixelRatio, $canvas, dpr;
552
451
 
553
- if (canvas) {
554
- this.$canvas = canvas;
452
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
453
+ while (1) {
454
+ switch (_context.prev = _context.next) {
455
+ case 0:
456
+ _this$canvasConfig = this.canvasConfig, container = _this$canvasConfig.container, canvas = _this$canvasConfig.canvas, devicePixelRatio = _this$canvasConfig.devicePixelRatio;
555
457
 
556
- if (container && canvas.parentElement !== container) {
557
- container.appendChild(canvas);
558
- }
458
+ if (canvas) {
459
+ this.$canvas = canvas;
559
460
 
560
- this.$container = canvas.parentElement;
561
- this.canvasConfig.container = this.$container;
562
- } else if (container) {
563
- // create container
564
- this.$container = g.isString(container) ? document.getElementById(container) : container;
461
+ if (container && canvas.parentElement !== container) {
462
+ container.appendChild(canvas);
463
+ }
565
464
 
566
- if (this.$container) {
567
- // create canvas
568
- $canvas = document.createElement('canvas');
569
- this.$container.appendChild($canvas);
465
+ this.$container = canvas.parentElement;
466
+ this.canvasConfig.container = this.$container;
467
+ } else if (container) {
468
+ // create container
469
+ this.$container = g.isString(container) ? document.getElementById(container) : container;
570
470
 
571
- if (!this.$container.style.position) {
572
- this.$container.style.position = 'relative';
573
- }
471
+ if (this.$container) {
472
+ // create canvas
473
+ $canvas = document.createElement('canvas');
474
+ this.$container.appendChild($canvas);
574
475
 
575
- this.$canvas = $canvas;
476
+ if (!this.$container.style.position) {
477
+ this.$container.style.position = 'relative';
576
478
  }
479
+
480
+ this.$canvas = $canvas;
577
481
  }
482
+ }
578
483
 
579
- this.context = this.$canvas.getContext('2d'); // use user-defined dpr first
484
+ this.context = this.$canvas.getContext('2d'); // use user-defined dpr first
580
485
 
581
- dpr = devicePixelRatio || g.isBrowser && window.devicePixelRatio || 1;
582
- dpr = dpr >= 1 ? Math.ceil(dpr) : 1;
583
- this.dpr = dpr;
584
- this.resize(this.canvasConfig.width, this.canvasConfig.height);
486
+ dpr = devicePixelRatio || g.isBrowser && window.devicePixelRatio || 1;
487
+ dpr = dpr >= 1 ? Math.ceil(dpr) : 1;
488
+ this.dpr = dpr;
489
+ this.resize(this.canvasConfig.width, this.canvasConfig.height);
585
490
 
586
- case 7:
587
- case "end":
588
- return _context.stop();
589
- }
491
+ case 7:
492
+ case "end":
493
+ return _context.stop();
590
494
  }
591
- }, _callee, this);
592
- }));
593
-
594
- function init() {
595
- return _init.apply(this, arguments);
596
- }
495
+ }
496
+ }, _callee, this);
497
+ }));
597
498
 
598
- return init;
599
- }()
600
- }, {
601
- key: "getContext",
602
- value: function getContext() {
603
- return this.context;
604
- }
605
- }, {
606
- key: "getDomElement",
607
- value: function getDomElement() {
608
- return this.$canvas;
499
+ function init() {
500
+ return _init.apply(this, arguments);
609
501
  }
610
- }, {
611
- key: "getDPR",
612
- value: function getDPR() {
613
- return this.dpr;
614
- }
615
- }, {
616
- key: "getBoundingClientRect",
617
- value: function getBoundingClientRect() {
618
- if (this.$canvas.getBoundingClientRect) {
619
- return this.$canvas.getBoundingClientRect();
620
- }
502
+
503
+ return init;
504
+ }();
505
+
506
+ _proto.getContext = function getContext() {
507
+ return this.context;
508
+ };
509
+
510
+ _proto.getDomElement = function getDomElement() {
511
+ return this.$canvas;
512
+ };
513
+
514
+ _proto.getDPR = function getDPR() {
515
+ return this.dpr;
516
+ };
517
+
518
+ _proto.getBoundingClientRect = function getBoundingClientRect() {
519
+ if (this.$canvas.getBoundingClientRect) {
520
+ return this.$canvas.getBoundingClientRect();
621
521
  }
622
- }, {
623
- key: "destroy",
624
- value: function destroy() {
522
+ };
523
+
524
+ _proto.destroy = function destroy() {
525
+ // @ts-ignore
526
+ if (this.$container && this.$canvas && this.$canvas.parentNode) {
527
+ // destroy context
625
528
  // @ts-ignore
626
- if (this.$container && this.$canvas && this.$canvas.parentNode) {
627
- // destroy context
628
- // @ts-ignore
629
- this.$container.removeChild(this.$canvas);
630
- }
529
+ this.$container.removeChild(this.$canvas);
631
530
  }
632
- }, {
633
- key: "resize",
634
- value: function resize(width, height) {
635
- if (this.$canvas) {
636
- // set canvas width & height
637
- this.$canvas.width = this.dpr * width;
638
- this.$canvas.height = this.dpr * height; // set CSS style width & height
639
-
640
- g.setDOMSize(this.$canvas, width, height);
641
- var dpr = this.getDPR(); // scale all drawing operations by the dpr
642
- // @see https://www.html5rocks.com/en/tutorials/canvas/hidpi/
643
-
644
- this.context.scale(dpr, dpr);
645
- }
531
+ };
532
+
533
+ _proto.resize = function resize(width, height) {
534
+ if (this.$canvas) {
535
+ // set canvas width & height
536
+ this.$canvas.width = this.dpr * width;
537
+ this.$canvas.height = this.dpr * height; // set CSS style width & height
538
+
539
+ g.setDOMSize(this.$canvas, width, height);
540
+ var dpr = this.getDPR(); // scale all drawing operations by the dpr
541
+ // @see https://www.html5rocks.com/en/tutorials/canvas/hidpi/
542
+
543
+ this.context.scale(dpr, dpr);
646
544
  }
647
- }, {
648
- key: "applyCursorStyle",
649
- value: function applyCursorStyle(cursor) {
650
- if (this.$container && this.$container.style) {
651
- this.$container.style.cursor = cursor;
652
- }
545
+ };
546
+
547
+ _proto.applyCursorStyle = function applyCursorStyle(cursor) {
548
+ if (this.$container && this.$container.style) {
549
+ this.$container.style.cursor = cursor;
550
+ }
551
+ };
552
+
553
+ _proto.toDataURL = /*#__PURE__*/function () {
554
+ var _toDataURL = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(options) {
555
+ var _options, type, encoderOptions;
556
+
557
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
558
+ while (1) {
559
+ switch (_context2.prev = _context2.next) {
560
+ case 0:
561
+ if (options === void 0) {
562
+ options = {};
563
+ }
564
+
565
+ _options = options, type = _options.type, encoderOptions = _options.encoderOptions;
566
+ return _context2.abrupt("return", this.context.canvas.toDataURL(type, encoderOptions));
567
+
568
+ case 3:
569
+ case "end":
570
+ return _context2.stop();
571
+ }
572
+ }
573
+ }, _callee2, this);
574
+ }));
575
+
576
+ function toDataURL(_x) {
577
+ return _toDataURL.apply(this, arguments);
653
578
  }
654
- }]);
579
+
580
+ return toDataURL;
581
+ }();
655
582
 
656
583
  return Canvas2DContextService;
657
584
  }();
@@ -670,37 +597,29 @@ var containerModule = g.Module(function (register) {
670
597
  });
671
598
  var ContextRegisterPlugin = /*#__PURE__*/function () {
672
599
  function ContextRegisterPlugin() {
673
- _classCallCheck(this, ContextRegisterPlugin);
674
-
675
600
  this.name = 'canvas-context-register';
676
601
  }
677
602
 
678
- _createClass(ContextRegisterPlugin, [{
679
- key: "init",
680
- value: function init(container) {
681
- container.load(containerModule, true);
682
- }
683
- }, {
684
- key: "destroy",
685
- value: function destroy(container) {
686
- container.unload(containerModule);
687
- }
688
- }]);
603
+ var _proto = ContextRegisterPlugin.prototype;
604
+
605
+ _proto.init = function init(container) {
606
+ container.load(containerModule, true);
607
+ };
608
+
609
+ _proto.destroy = function destroy(container) {
610
+ container.unload(containerModule);
611
+ };
689
612
 
690
613
  return ContextRegisterPlugin;
691
614
  }();
692
615
 
693
616
  var Renderer = /*#__PURE__*/function (_AbstractRenderer) {
694
- _inherits(Renderer, _AbstractRenderer);
695
-
696
- var _super = _createSuper(Renderer);
617
+ _inheritsLoose(Renderer, _AbstractRenderer);
697
618
 
698
619
  function Renderer(config) {
699
620
  var _this;
700
621
 
701
- _classCallCheck(this, Renderer);
702
-
703
- _this = _super.call(this, config); // register Canvas2DContext
622
+ _this = _AbstractRenderer.call(this, config) || this; // register Canvas2DContext
704
623
 
705
624
  _this.registerPlugin(new ContextRegisterPlugin());
706
625
 
@@ -722,7 +641,7 @@ var Renderer = /*#__PURE__*/function (_AbstractRenderer) {
722
641
  return _this;
723
642
  }
724
643
 
725
- return _createClass(Renderer);
644
+ return Renderer;
726
645
  }(g.AbstractRenderer);
727
646
 
728
647
  exports.CanvasPathGenerator = CanvasPathGenerator__namespace;