Extensions

Source: display/BaseDrawable.js

import { Item } from "./Item";
import { Matrix3Utilities } from "../math/Matrix3Utilities";
import "../math/PointType";
import "../math/RectangleType";

/**
 * Base drawable class
 * @extends {Item}
 */
export class BaseDrawable extends Item {
  /**
   * Creates an instance of BaseDrawable.
   * @constructor
   */
  constructor() {
    super();

    this.$corners = [
      { x: 0, y: 0 },
      { x: 0, y: 0 },
      { x: 0, y: 0 },
      { x: 0, y: 0 },
    ];
  }

  /**
   * Returns with bounds
   * @returns {Rectangle}
   */
  getBounds() {
    this.$calcCorners();
    const corners = this.$corners,
      bounds = this.$bounds,
      a = corners[0],
      b = corners[1],
      c = corners[2],
      d = corners[3];

    bounds.x = Math.min(a.x, b.x, c.x, d.x);
    bounds.y = Math.min(a.y, b.y, c.y, d.y);
    bounds.width = Math.max(a.x, b.x, c.x, d.x);
    bounds.height = Math.max(a.y, b.y, c.y, d.y);

    return bounds;
  }

  /**
   * @ignore
   */
  $calcCorners() {
    this.stage &&
      Matrix3Utilities.calcCorners(
        this.$corners,
        this.matrixCache,
        this.stage.renderer
      );
  }
}