index.d.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /// <reference path="./global.d.ts" />
  2. import { BLEND_MODES } from '@pixi/constants';
  3. import { Container } from '@pixi/display';
  4. import type { IBaseTextureOptions } from '@pixi/core';
  5. import type { IDestroyOptions } from '@pixi/display';
  6. import type { IPointData } from '@pixi/math';
  7. import { ObservablePoint } from '@pixi/math';
  8. import { Rectangle } from '@pixi/math';
  9. import type { Renderer } from '@pixi/core';
  10. import { Texture } from '@pixi/core';
  11. import type { TextureSource } from '@pixi/core';
  12. export declare interface Sprite extends GlobalMixins.Sprite, Container {
  13. }
  14. /**
  15. * The Sprite object is the base for all textured objects that are rendered to the screen
  16. *
  17. * A sprite can be created directly from an image like this:
  18. *
  19. * ```js
  20. * let sprite = PIXI.Sprite.from('assets/image.png');
  21. * ```
  22. *
  23. * The more efficient way to create sprites is using a {@link PIXI.Spritesheet},
  24. * as swapping base textures when rendering to the screen is inefficient.
  25. *
  26. * ```js
  27. * PIXI.Loader.shared.add("assets/spritesheet.json").load(setup);
  28. *
  29. * function setup() {
  30. * let sheet = PIXI.Loader.shared.resources["assets/spritesheet.json"].spritesheet;
  31. * let sprite = new PIXI.Sprite(sheet.textures["image.png"]);
  32. * ...
  33. * }
  34. * ```
  35. * @memberof PIXI
  36. */
  37. export declare class Sprite extends Container {
  38. /**
  39. * The blend mode to be applied to the sprite. Apply a value of `PIXI.BLEND_MODES.NORMAL` to reset the blend mode.
  40. * @default PIXI.BLEND_MODES.NORMAL
  41. */
  42. blendMode: BLEND_MODES;
  43. indices: Uint16Array;
  44. /**
  45. * Plugin that is responsible for rendering this element.
  46. * Allows to customize the rendering process without overriding '_render' & '_renderCanvas' methods.
  47. * @default 'batch'
  48. */
  49. pluginName: string;
  50. /**
  51. * The width of the sprite (this is initially set by the texture).
  52. * @protected
  53. */
  54. _width: number;
  55. /**
  56. * The height of the sprite (this is initially set by the texture)
  57. * @protected
  58. */
  59. _height: number;
  60. /**
  61. * The texture that the sprite is using.
  62. * @private
  63. */
  64. _texture: Texture;
  65. _textureID: number;
  66. /**
  67. * Cached tint value so we can tell when the tint is changed.
  68. * Value is used for 2d CanvasRenderer.
  69. * @protected
  70. * @default 0xFFFFFF
  71. */
  72. _cachedTint: number;
  73. protected _textureTrimmedID: number;
  74. /**
  75. * This is used to store the uvs data of the sprite, assigned at the same time
  76. * as the vertexData in calculateVertices().
  77. * @member {Float32Array}
  78. */
  79. protected uvs: Float32Array;
  80. /**
  81. * The anchor point defines the normalized coordinates
  82. * in the texture that map to the position of this
  83. * sprite.
  84. *
  85. * By default, this is `(0,0)` (or `texture.defaultAnchor`
  86. * if you have modified that), which means the position
  87. * `(x,y)` of this `Sprite` will be the top-left corner.
  88. *
  89. * Note: Updating `texture.defaultAnchor` after
  90. * constructing a `Sprite` does _not_ update its anchor.
  91. *
  92. * {@link https://docs.cocos2d-x.org/cocos2d-x/en/sprites/manipulation.html}
  93. * @default `this.texture.defaultAnchor`
  94. */
  95. protected _anchor: ObservablePoint;
  96. /**
  97. * This is used to store the vertex data of the sprite (basically a quad).
  98. * @member {Float32Array}
  99. */
  100. protected vertexData: Float32Array;
  101. /**
  102. * This is used to calculate the bounds of the object IF it is a trimmed sprite.
  103. * @member {Float32Array}
  104. */
  105. private vertexTrimmedData;
  106. /**
  107. * Internal roundPixels field
  108. * @private
  109. */
  110. private _roundPixels;
  111. private _transformID;
  112. private _transformTrimmedID;
  113. /**
  114. * The tint applied to the sprite. This is a hex value. A value of 0xFFFFFF will remove any tint effect.
  115. * @default 0xFFFFFF
  116. */
  117. private _tint;
  118. /**
  119. * The tint applied to the sprite. This is a RGB value. A value of 0xFFFFFF will remove any tint effect.
  120. * @private
  121. * @default 16777215
  122. */
  123. _tintRGB: number;
  124. /** @param texture - The texture for this sprite. */
  125. constructor(texture?: Texture);
  126. /** When the texture is updated, this event will fire to update the scale and frame. */
  127. protected _onTextureUpdate(): void;
  128. /** Called when the anchor position updates. */
  129. private _onAnchorUpdate;
  130. /** Calculates worldTransform * vertices, store it in vertexData. */
  131. calculateVertices(): void;
  132. /**
  133. * Calculates worldTransform * vertices for a non texture with a trim. store it in vertexTrimmedData.
  134. *
  135. * This is used to ensure that the true width and height of a trimmed texture is respected.
  136. */
  137. calculateTrimmedVertices(): void;
  138. /**
  139. *
  140. * Renders the object using the WebGL renderer
  141. * @param renderer - The webgl renderer to use.
  142. */
  143. protected _render(renderer: Renderer): void;
  144. /** Updates the bounds of the sprite. */
  145. protected _calculateBounds(): void;
  146. /**
  147. * Gets the local bounds of the sprite object.
  148. * @param rect - Optional output rectangle.
  149. * @returns The bounds.
  150. */
  151. getLocalBounds(rect?: Rectangle): Rectangle;
  152. /**
  153. * Tests if a point is inside this sprite
  154. * @param point - the point to test
  155. * @returns The result of the test
  156. */
  157. containsPoint(point: IPointData): boolean;
  158. /**
  159. * Destroys this sprite and optionally its texture and children.
  160. * @param options - Options parameter. A boolean will act as if all options
  161. * have been set to that value
  162. * @param [options.children=false] - if set to true, all the children will have their destroy
  163. * method called as well. 'options' will be passed on to those calls.
  164. * @param [options.texture=false] - Should it destroy the current texture of the sprite as well
  165. * @param [options.baseTexture=false] - Should it destroy the base texture of the sprite as well
  166. */
  167. destroy(options?: IDestroyOptions | boolean): void;
  168. /**
  169. * Helper function that creates a new sprite based on the source you provide.
  170. * The source can be - frame id, image url, video url, canvas element, video element, base texture
  171. * @param {string|PIXI.Texture|HTMLCanvasElement|HTMLVideoElement} source - Source to create texture from
  172. * @param {object} [options] - See {@link PIXI.BaseTexture}'s constructor for options.
  173. * @returns The newly created sprite
  174. */
  175. static from(source: SpriteSource, options?: IBaseTextureOptions): Sprite;
  176. /**
  177. * If true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation.
  178. *
  179. * Advantages can include sharper image quality (like text) and faster rendering on canvas.
  180. * The main disadvantage is movement of objects may appear less smooth.
  181. *
  182. * To set the global default, change {@link PIXI.settings.ROUND_PIXELS}.
  183. * @default false
  184. */
  185. set roundPixels(value: boolean);
  186. get roundPixels(): boolean;
  187. /** The width of the sprite, setting this will actually modify the scale to achieve the value set. */
  188. get width(): number;
  189. set width(value: number);
  190. /** The height of the sprite, setting this will actually modify the scale to achieve the value set. */
  191. get height(): number;
  192. set height(value: number);
  193. /**
  194. * The anchor sets the origin point of the sprite. The default value is taken from the {@link PIXI.Texture|Texture}
  195. * and passed to the constructor.
  196. *
  197. * The default is `(0,0)`, this means the sprite's origin is the top left.
  198. *
  199. * Setting the anchor to `(0.5,0.5)` means the sprite's origin is centered.
  200. *
  201. * Setting the anchor to `(1,1)` would mean the sprite's origin point will be the bottom right corner.
  202. *
  203. * If you pass only single parameter, it will set both x and y to the same value as shown in the example below.
  204. * @example
  205. * const sprite = new PIXI.Sprite(texture);
  206. * sprite.anchor.set(0.5); // This will set the origin to center. (0.5) is same as (0.5, 0.5).
  207. */
  208. get anchor(): ObservablePoint;
  209. set anchor(value: ObservablePoint);
  210. /**
  211. * The tint applied to the sprite. This is a hex value.
  212. *
  213. * A value of 0xFFFFFF will remove any tint effect.
  214. * @default 0xFFFFFF
  215. */
  216. get tint(): number;
  217. set tint(value: number);
  218. /** The texture that the sprite is using. */
  219. get texture(): Texture;
  220. set texture(value: Texture);
  221. }
  222. export declare type SpriteSource = TextureSource | Texture;
  223. export { }