group.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { mix } from '../../util/common';
  2. import Rect from './shape/rect';
  3. import Container from './container';
  4. import Vector2 from '../util/vector2';
  5. class Group extends Rect {
  6. _initProperties() {
  7. this._attrs = {
  8. type: 'group',
  9. zIndex: 0,
  10. visible: true,
  11. destroyed: false,
  12. isGroup: true,
  13. canFill: true,
  14. canStroke: true,
  15. attrs: {},
  16. children: []
  17. };
  18. }
  19. getBBox() {
  20. var self = this;
  21. var minX = Infinity;
  22. var maxX = -Infinity;
  23. var minY = Infinity;
  24. var maxY = -Infinity;
  25. var children = self.get('children');
  26. for (var i = 0, length = children.length; i < length; i++) {
  27. var child = children[i];
  28. if (child.get('visible')) {
  29. var box = child.getBBox();
  30. if (!box) {
  31. continue;
  32. }
  33. var leftTop = [box.minX, box.minY];
  34. var leftBottom = [box.minX, box.maxY];
  35. var rightTop = [box.maxX, box.minY];
  36. var rightBottom = [box.maxX, box.maxY];
  37. var matrix = child.attr('matrix');
  38. Vector2.transformMat2d(leftTop, leftTop, matrix);
  39. Vector2.transformMat2d(leftBottom, leftBottom, matrix);
  40. Vector2.transformMat2d(rightTop, rightTop, matrix);
  41. Vector2.transformMat2d(rightBottom, rightBottom, matrix);
  42. minX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], minX);
  43. maxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], maxX);
  44. minY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], minY);
  45. maxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], maxY);
  46. }
  47. }
  48. return {
  49. minX,
  50. minY,
  51. maxX,
  52. maxY,
  53. x: minX,
  54. y: minY,
  55. width: maxX - minX,
  56. height: maxY - minY
  57. };
  58. }
  59. createPath(context) {
  60. var attrs = this.get('attrs'); // 只有在有fillStyle或strokeStyle 时才需要绘制
  61. if (!attrs.fillStyle && !attrs.strokeStyle) {
  62. return;
  63. }
  64. super.createPath(context);
  65. }
  66. drawInner(context) {
  67. super.drawInner(context);
  68. this.drawChildren(context);
  69. }
  70. destroy() {
  71. if (this.get('destroyed')) {
  72. return;
  73. }
  74. this.clear();
  75. super.destroy();
  76. }
  77. }
  78. mix(Group.prototype, Container, {
  79. getGroupClass() {
  80. return Group;
  81. }
  82. });
  83. export default Group;