PIXI.Sprite
class Sprite extends PIXI.Container
The Sprite object is the base for all textured objects that are rendered to the screen
A sprite can be created directly from an image like this:
let sprite = PIXI.Sprite.from('assets/image.png');
The more efficient way to create sprites is using a PIXI.Spritesheet, as swapping base textures when rendering to the screen is inefficient.
PIXI.Loader.shared.add("assets/spritesheet.json").load(setup);
function setup() {
let sheet = PIXI.Loader.shared.resources["assets/spritesheet.json"].spritesheet;
let sprite = new PIXI.Sprite(sheet.textures["image.png"]);
...
}
Constructor
new PIXI.Sprite(texture: PIXI.Texture) → {}
Name | Type | Attributes | Description |
---|---|---|---|
texture | PIXI.Texture |
<optional> |
The texture for this sprite. |
Summary
Properties from Sprite
PIXI.ObservablePoint |
|
PIXI.BLEND_MODES |
|
number |
The height of the sprite, setting this will actually modify the scale to achieve the value set. |
boolean |
|
string |
|
boolean |
If true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation. |
PIXI.Texture |
The texture that the sprite is using. |
number |
The tint applied to the sprite. This is a hex value. |
number |
The width of the sprite, setting this will actually modify the scale to achieve the value set. |
PIXI.ObservablePoint |
|
number |
|
number |
|
HTMLCanvasElement |
|
number |
|
Float32Array |
|
Float32Array |
|
Methods from Sprite
PIXI.Sprite |
|
void |
Calculates worldTransform * vertices for a non texture with a trim. store it in vertexTrimmedData. |
void |
Calculates worldTransform * vertices, store it in vertexData. |
boolean |
|
void |
|
PIXI.Rectangle |
|
void |
|
void |
Updates the bounds of the sprite. |
void |
When the texture is updated, this event will fire to update the scale and frame. |
void |
|
Properties inherited from Container
T[] |
|
boolean |
|
boolean |
|
boolean |
Should children be sorted by zIndex at the next updateTransform call. |
Properties inherited from DisplayObject
Methods inherited from Container
Methods inherited from DisplayObject
Inherited Events from Container
Inherited Events from DisplayObject
|
|
|
|
|
|
Capture phase equivalent of |
|
|
|
|
|
Capture phase equivalent of |
|
Fired when the mouse pointer is moved over a DisplayObject and its descendant's hit testing boundaries. |
|
Capture phase equivalent of |
|
Fired when the mouse pointer exits a DisplayObject and its descendants. |
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
Fired when the pointer is moved over a DisplayObject and its descendant's hit testing boundaries. |
|
Capture phase equivalent of |
|
Fired when the pointer leaves the hit testing boundaries of a DisplayObject and its descendants. |
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
|
|
Capture phase equivalent of |
|
PIXI.FederatedWheelEvent |
Fired when a the user scrolls with the mouse cursor over a DisplayObject. |
PIXI.FederatedWheelEvent |
Capture phase equivalent of |
Public Properties
anchor: PIXI.ObservablePoint
The anchor sets the origin point of the sprite. The default value is taken from the Texture and passed to the constructor.
The default is (0,0)
, this means the sprite's origin is the top left.
Setting the anchor to (0.5,0.5)
means the sprite's origin is centered.
Setting the anchor to (1,1)
would mean the sprite's origin point will be the bottom right corner.
If you pass only single parameter, it will set both x and y to the same value as shown in the example below.
const sprite = new PIXI.Sprite(texture);
sprite.anchor.set(0.5); // This will set the origin to center. (0.5) is same as (0.5, 0.5).
blendMode: PIXI.BLEND_MODES = PIXI.BLEND_MODES.NORMAL
The blend mode to be applied to the sprite. Apply a value of PIXI.BLEND_MODES.NORMAL
to reset the blend mode.
height: number
The height of the sprite, setting this will actually modify the scale to achieve the value set.
pluginName: string = 'batch'
Plugin that is responsible for rendering this element. Allows to customize the rendering process without overriding '_render' & '_renderCanvas' methods.
roundPixels: boolean = false
If true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation.
Advantages can include sharper image quality (like text) and faster rendering on canvas. The main disadvantage is movement of objects may appear less smooth.
To set the global default, change PIXI.settings.ROUND_PIXELS.
tint: number = 0xFFFFFF
The tint applied to the sprite. This is a hex value.
A value of 0xFFFFFF will remove any tint effect.
width: number
The width of the sprite, setting this will actually modify the scale to achieve the value set.
Protected Properties
protected _anchor: PIXI.ObservablePoint = `this.texture.defaultAnchor`
The anchor point defines the normalized coordinates in the texture that map to the position of this sprite.
By default, this is (0,0)
(or texture.defaultAnchor
if you have modified that), which means the position
(x,y)
of this Sprite
will be the top-left corner.
Note: Updating texture.defaultAnchor
after
constructing a Sprite
does not update its anchor.
https://docs.cocos2d-x.org/cocos2d-x/en/sprites/manipulation.html
protected _cachedTint: number = 0xFFFFFF
Cached tint value so we can tell when the tint is changed. Value is used for 2d CanvasRenderer.
protected _height: number
The height of the sprite (this is initially set by the texture)
protected _tintedCanvas: HTMLCanvasElement = undefined
Cached tinted texture.
protected _width: number
The width of the sprite (this is initially set by the texture).
protected uvs: Float32Array
This is used to store the uvs data of the sprite, assigned at the same time as the vertexData in calculateVertices().
protected vertexData: Float32Array
This is used to store the vertex data of the sprite (basically a quad).
Public Methods
static from(source: string | PIXI.Texture | HTMLCanvasElement | HTMLVideoElement, options: object) → {PIXI.Sprite}
Helper function that creates a new sprite based on the source you provide. The source can be - frame id, image url, video url, canvas element, video element, base texture
Name | Type | Attributes | Description |
---|---|---|---|
source | string | PIXI.Texture | HTMLCanvasElement | HTMLVideoElement |
Source to create texture from |
|
options | object |
<optional> |
See PIXI.BaseTexture's constructor for options. |
Type | Description |
---|---|
PIXI.Sprite |
The newly created sprite |
calculateTrimmedVertices() → {void}
Calculates worldTransform * vertices for a non texture with a trim. store it in vertexTrimmedData.
This is used to ensure that the true width and height of a trimmed texture is respected.
Type | Description |
---|---|
void |
calculateVertices() → {void}
Calculates worldTransform * vertices, store it in vertexData.
Type | Description |
---|---|
void |
containsPoint(point: IPointData) → {boolean}
Tests if a point is inside this sprite
Name | Type | Description |
---|---|---|
point | IPointData |
the point to test |
Type | Description |
---|---|
boolean |
The result of the test |
destroy(options: IDestroyOptions | boolean) → {void}
Destroys this sprite and optionally its texture and children.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
options | IDestroyOptions | boolean |
<optional> |
Options parameter. A boolean will act as if all options have been set to that value |
|
options.children |
<optional> |
false |
if set to true, all the children will have their destroy method called as well. 'options' will be passed on to those calls. |
|
options.texture |
<optional> |
false |
Should it destroy the current texture of the sprite as well |
|
options.baseTexture |
<optional> |
false |
Should it destroy the base texture of the sprite as well |
Type | Description |
---|---|
void |
getLocalBounds(rect: PIXI.Rectangle) → {PIXI.Rectangle}
Gets the local bounds of the sprite object.
Name | Type | Attributes | Description |
---|---|---|---|
rect | PIXI.Rectangle |
<optional> |
Optional output rectangle. |
Type | Description |
---|---|
PIXI.Rectangle |
The bounds. |
renderCanvas(renderer: PIXI.CanvasRenderer) → {void}
Renders the object using the Canvas renderer
Name | Type | Description |
---|---|---|
renderer | PIXI.CanvasRenderer |
The renderer |
Type | Description |
---|---|
void |
Protected Methods
protected _calculateBounds() → {void}
Updates the bounds of the sprite.
Type | Description |
---|---|
void |
protected _onTextureUpdate() → {void}
When the texture is updated, this event will fire to update the scale and frame.
Type | Description |
---|---|
void |
protected _render(renderer: PIXI.Renderer) → {void}
Renders the object using the WebGL renderer
Name | Type | Description |
---|---|---|
renderer | PIXI.Renderer |
The webgl renderer to use. |
Type | Description |
---|---|
void |