group.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  2. var Util = require('../util/common');
  3. var Element = require('./element');
  4. var Container = require('./container');
  5. var Vector2 = require('./util/vector2');
  6. var Group =
  7. /*#__PURE__*/
  8. function (_Element) {
  9. _inheritsLoose(Group, _Element);
  10. function Group() {
  11. return _Element.apply(this, arguments) || this;
  12. }
  13. var _proto = Group.prototype;
  14. _proto._initProperties = function _initProperties() {
  15. this._attrs = {
  16. zIndex: 0,
  17. visible: true,
  18. destroyed: false,
  19. isGroup: true,
  20. children: []
  21. };
  22. };
  23. _proto.drawInner = function drawInner(context) {
  24. var children = this.get('children');
  25. for (var i = 0, len = children.length; i < len; i++) {
  26. var child = children[i];
  27. child.draw(context);
  28. }
  29. return this;
  30. };
  31. _proto.getBBox = function getBBox() {
  32. var self = this;
  33. var minX = Infinity;
  34. var maxX = -Infinity;
  35. var minY = Infinity;
  36. var maxY = -Infinity;
  37. var children = self.get('children');
  38. for (var i = 0, length = children.length; i < length; i++) {
  39. var child = children[i];
  40. if (child.get('visible')) {
  41. var box = child.getBBox();
  42. if (!box) {
  43. continue;
  44. }
  45. var leftTop = [box.minX, box.minY];
  46. var leftBottom = [box.minX, box.maxY];
  47. var rightTop = [box.maxX, box.minY];
  48. var rightBottom = [box.maxX, box.maxY];
  49. var matrix = child.attr('matrix');
  50. Vector2.transformMat2d(leftTop, leftTop, matrix);
  51. Vector2.transformMat2d(leftBottom, leftBottom, matrix);
  52. Vector2.transformMat2d(rightTop, rightTop, matrix);
  53. Vector2.transformMat2d(rightBottom, rightBottom, matrix);
  54. minX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], minX);
  55. maxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], maxX);
  56. minY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], minY);
  57. maxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], maxY);
  58. }
  59. }
  60. return {
  61. minX: minX,
  62. minY: minY,
  63. maxX: maxX,
  64. maxY: maxY,
  65. x: minX,
  66. y: minY,
  67. width: maxX - minX,
  68. height: maxY - minY
  69. };
  70. };
  71. _proto.destroy = function destroy() {
  72. if (this.get('destroyed')) {
  73. return;
  74. }
  75. this.clear();
  76. _Element.prototype.destroy.call(this);
  77. };
  78. return Group;
  79. }(Element);
  80. Util.mix(Group.prototype, Container, {
  81. getGroupClass: function getGroupClass() {
  82. return Group;
  83. }
  84. });
  85. module.exports = Group;