Extensions

Source: display/Image.js

import { Distortion } from "../attributes/Distortion";
import { TextureCrop } from "../attributes/TextureCrop";
import { TextureTransform } from "../attributes/TextureTransform";
import { Matrix3Utilities } from "../math/Matrix3Utilities";
import "../math/PointType";
import { BlendMode } from "../rendering/BlendMode";
import { TintType } from "../rendering/TintType";
import { TextureInfo } from "../textures/TextureInfo";
import { BaseDrawable } from "./BaseDrawable";

/**
 * <pre>
 *  Image class
 *    - getBounds() includes the distortion corners, regardless of the renderer's useDistortion config
 * </pre>
 * @extends {BaseDrawable}
 * @property {TintType} tintType
 * @property {BlendMode} blendMode
 * @property {TextureTransform} textureTransform
 * @property {TextureCrop} textureCrop
 * @property {Distortion} distortion
 * @property {TextureInfo} texture
 * @property {Array<number>} textureMatrixCache
 * @property {Array<number>} textureCropCache
 * @property {Array<number>} textureRepeatRandomCache
 * @property {Array<number>} distortionCache
 */
export class Image extends BaseDrawable {
  /**
   * Creates an instance of Image.
   * @constructor
   * @param {TextureInfo} texture
   */
  constructor(texture) {
    super();

    this.RENDERING_TYPE = Image.RENDERING_TYPE;

    this.texture = texture;
    this.tintType = TintType.MULTIPLY;
    this.blendMode = BlendMode.NORMAL;
    this.$inverseMatrixCache = new Float32Array(6);
    this.textureMatrixCache = Matrix3Utilities.identity();
    this.textureTransform = new TextureTransform();
    this.textureCrop = new TextureCrop();
    this.distortion = new Distortion();
    this.textureCropCache = this.textureCrop.cache;
    this.textureRepeatRandomCache = this.textureTransform.repeatRandomCache;
    this.distortionCache = this.distortion.cache;
  }

  /**
   * Update Image
   */
  update() {
    super.update();

    const { textureTransform } = this;

    textureTransform.update();
    textureTransform.updated && Matrix3Utilities.transformLocal(this.textureMatrixCache, textureTransform);
  }

  /**
   * <pre>
   *  Returns true if the Image contains the point
   *    - Uses the matrix cache of the last rendered frame; changes made since then are not included
   * </pre>
   * @param {Point} point
   * @returns {boolean}
   */
  isContainsPoint(point) {
    const { $transformVersion } = this;

    if (this.$inverseVersion !== $transformVersion) {
      this.$inverseVersion = $transformVersion;
      Matrix3Utilities.inverse(this.$inverseMatrixCache, this.matrixCache);
    }

    return Matrix3Utilities.isPointInMatrix(this.$inverseMatrixCache, point);
  }

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

/**
 * Type "drawable"
 * @string
 */
Image.RENDERING_TYPE = "drawable";