Source: core/textures/BaseRenderTexture.js

core/textures/BaseRenderTexture.js

  1. import BaseTexture from './BaseTexture';
  2. import settings from '../settings';
  3. /**
  4. * A BaseRenderTexture is a special texture that allows any PixiJS display object to be rendered to it.
  5. *
  6. * __Hint__: All DisplayObjects (i.e. Sprites) that render to a BaseRenderTexture should be preloaded
  7. * otherwise black rectangles will be drawn instead.
  8. *
  9. * A BaseRenderTexture takes a snapshot of any Display Object given to its render method. The position
  10. * and rotation of the given Display Objects is ignored. For example:
  11. *
  12. * ```js
  13. * let renderer = PIXI.autoDetectRenderer(1024, 1024);
  14. * let baseRenderTexture = new PIXI.BaseRenderTexture(800, 600);
  15. * let renderTexture = new PIXI.RenderTexture(baseRenderTexture);
  16. * let sprite = PIXI.Sprite.fromImage("spinObj_01.png");
  17. *
  18. * sprite.position.x = 800/2;
  19. * sprite.position.y = 600/2;
  20. * sprite.anchor.x = 0.5;
  21. * sprite.anchor.y = 0.5;
  22. *
  23. * renderer.render(sprite, renderTexture);
  24. * ```
  25. *
  26. * The Sprite in this case will be rendered using its local transform. To render this sprite at 0,0
  27. * you can clear the transform
  28. *
  29. * ```js
  30. *
  31. * sprite.setTransform()
  32. *
  33. * let baseRenderTexture = new PIXI.BaseRenderTexture(100, 100);
  34. * let renderTexture = new PIXI.RenderTexture(baseRenderTexture);
  35. *
  36. * renderer.render(sprite, renderTexture); // Renders to center of RenderTexture
  37. * ```
  38. *
  39. * @class
  40. * @extends PIXI.BaseTexture
  41. * @memberof PIXI
  42. */
  43. export default class BaseRenderTexture extends BaseTexture
  44. {
  45. /**
  46. * @param {number} [width=100] - The width of the base render texture
  47. * @param {number} [height=100] - The height of the base render texture
  48. * @param {number} [scaleMode=PIXI.settings.SCALE_MODE] - See {@link PIXI.SCALE_MODES} for possible values
  49. * @param {number} [resolution=1] - The resolution / device pixel ratio of the texture being generated
  50. */
  51. constructor(width = 100, height = 100, scaleMode, resolution)
  52. {
  53. super(null, scaleMode);
  54. this.resolution = resolution || settings.RESOLUTION;
  55. this.width = Math.ceil(width);
  56. this.height = Math.ceil(height);
  57. this.realWidth = this.width * this.resolution;
  58. this.realHeight = this.height * this.resolution;
  59. this.scaleMode = scaleMode !== undefined ? scaleMode : settings.SCALE_MODE;
  60. this.hasLoaded = true;
  61. /**
  62. * A map of renderer IDs to webgl renderTargets
  63. *
  64. * @private
  65. * @member {object<number, WebGLTexture>}
  66. */
  67. this._glRenderTargets = {};
  68. /**
  69. * A reference to the canvas render target (we only need one as this can be shared across renderers)
  70. *
  71. * @private
  72. * @member {object<number, WebGLTexture>}
  73. */
  74. this._canvasRenderTarget = null;
  75. /**
  76. * This will let the renderer know if the texture is valid. If it's not then it cannot be rendered.
  77. *
  78. * @member {boolean}
  79. */
  80. this.valid = false;
  81. }
  82. /**
  83. * Resizes the BaseRenderTexture.
  84. *
  85. * @param {number} width - The width to resize to.
  86. * @param {number} height - The height to resize to.
  87. */
  88. resize(width, height)
  89. {
  90. width = Math.ceil(width);
  91. height = Math.ceil(height);
  92. if (width === this.width && height === this.height)
  93. {
  94. return;
  95. }
  96. this.valid = (width > 0 && height > 0);
  97. this.width = width;
  98. this.height = height;
  99. this.realWidth = this.width * this.resolution;
  100. this.realHeight = this.height * this.resolution;
  101. if (!this.valid)
  102. {
  103. return;
  104. }
  105. this.emit('update', this);
  106. }
  107. /**
  108. * Destroys this texture
  109. *
  110. */
  111. destroy()
  112. {
  113. super.destroy(true);
  114. this.renderer = null;
  115. }
  116. }