group.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  4. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  5. var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
  6. function _createSuper(Derived) { return function () { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
  7. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  8. var Util = require('../util/common');
  9. var Element = require('./element');
  10. var Container = require('./container');
  11. var Vector2 = require('./util/vector2');
  12. var Group = /*#__PURE__*/function (_Element) {
  13. (0, _inheritsLoose2["default"])(Group, _Element);
  14. var _super = _createSuper(Group);
  15. function Group() {
  16. return _Element.apply(this, arguments) || this;
  17. }
  18. var _proto = Group.prototype;
  19. _proto._initProperties = function _initProperties() {
  20. this._attrs = {
  21. zIndex: 0,
  22. visible: true,
  23. destroyed: false,
  24. isGroup: true,
  25. children: []
  26. };
  27. };
  28. _proto.drawInner = function drawInner(context) {
  29. var children = this.get('children');
  30. for (var i = 0, len = children.length; i < len; i++) {
  31. var child = children[i];
  32. child.draw(context);
  33. }
  34. return this;
  35. };
  36. _proto.getBBox = function getBBox() {
  37. var self = this;
  38. var minX = Infinity;
  39. var maxX = -Infinity;
  40. var minY = Infinity;
  41. var maxY = -Infinity;
  42. var children = self.get('children');
  43. for (var i = 0, length = children.length; i < length; i++) {
  44. var child = children[i];
  45. if (child.get('visible')) {
  46. var box = child.getBBox();
  47. if (!box) {
  48. continue;
  49. }
  50. var leftTop = [box.minX, box.minY];
  51. var leftBottom = [box.minX, box.maxY];
  52. var rightTop = [box.maxX, box.minY];
  53. var rightBottom = [box.maxX, box.maxY];
  54. var matrix = child.attr('matrix');
  55. Vector2.transformMat2d(leftTop, leftTop, matrix);
  56. Vector2.transformMat2d(leftBottom, leftBottom, matrix);
  57. Vector2.transformMat2d(rightTop, rightTop, matrix);
  58. Vector2.transformMat2d(rightBottom, rightBottom, matrix);
  59. minX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], minX);
  60. maxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], maxX);
  61. minY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], minY);
  62. maxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], maxY);
  63. }
  64. }
  65. return {
  66. minX: minX,
  67. minY: minY,
  68. maxX: maxX,
  69. maxY: maxY,
  70. x: minX,
  71. y: minY,
  72. width: maxX - minX,
  73. height: maxY - minY
  74. };
  75. };
  76. _proto.destroy = function destroy() {
  77. if (this.get('destroyed')) {
  78. return;
  79. }
  80. this.clear();
  81. _Element.prototype.destroy.call(this);
  82. };
  83. return Group;
  84. }(Element);
  85. Util.mix(Group.prototype, Container, {
  86. getGroupClass: function getGroupClass() {
  87. return Group;
  88. }
  89. });
  90. module.exports = Group;