Class Texture<R>

A texture stores the information that represents an image or part of an image.

It cannot be added to the display list directly; instead use it as the texture for a Sprite. If no frame is provided for a texture, then the whole image is used.

You can directly create a texture from an image and then reuse it multiple times like this :

import { Sprite, Texture } from 'pixi.js';

const texture = Texture.from('assets/image.png');
const sprite1 = new Sprite(texture);
const sprite2 = new Sprite(texture);

If you didnt pass the texture frame to constructor, it enables noFrame mode: it subscribes on baseTexture events, it automatically resizes at the same time as baseTexture.

Textures made from SVGs, loaded or not, cannot be used before the file finishes processing. You can check for this by checking the sprite's _textureID property.

import { Sprite, Texture } from 'pixi.js';

const texture = Texture.from('assets/image.svg');
const sprite1 = new Sprite(texture);
// sprite1._textureID should not be undefined if the texture has finished processing the SVG file

You can use a ticker or rAF to ensure your sprites load the finished textures after processing. See issue [#3085]https://github.com/pixijs/pixijs/issues/3085.

Memberof

PIXI

Type Parameters

Hierarchy (view full)

Constructors

  • Type Parameters

    Parameters

    • baseTexture: BaseTexture<R, IAutoDetectOptions>

      The base texture source to create the texture from

    • Optional frame: Rectangle

      The rectangle frame of the texture to show

    • Optional orig: Rectangle

      The area of original texture

    • Optional trim: Rectangle

      Trimmed rectangle of original texture

    • Optional rotate: number

      indicates how the texture was rotated by texture packer. See PIXI.groupD8

    • Optional anchor: IPointData

      Default anchor point used for sprite placement / rotation

    • Optional borders: ITextureBorders

      Default borders used for 9-slice scaling. See PIXI.NineSlicePlane

    Returns Texture<R>

Properties

_frame: Rectangle

This is the area of the BaseTexture image to actually copy to the Canvas / WebGL when rendering, irrespective of the actual frame size or placement (which can be influenced by trimmed texture atlases)

The base texture that this texture uses.

defaultAnchor: Point

Anchor point that is used as default if sprite is created with this texture. Changing the defaultAnchor at a later point of time will not update Sprite's anchor point.

Default

{0,0}
defaultBorders?: ITextureBorders

Default width of the non-scalable border that is used if 9-slice plane is created with this texture.

Since

7.2.0

See

PIXI.NineSlicePlane

destroyed: boolean

Has the texture been destroyed?

noFrame: boolean

Does this Texture have any frame data assigned to it?

This mode is enabled automatically if no frame was passed inside constructor.

In this mode texture is subscribed to baseTexture events, and fires update on any change.

Beware, after loading or resize of baseTexture event can fired two times! If you want more control, subscribe on baseTexture itself.

Any assignment of frame switches off noFrame mode.

Example

texture.on('update', () => {});
orig: Rectangle

This is the area of original texture, before it was put in atlas.

textureCacheIds: string[]

The ids under which this Texture has been added to the texture cache. This is automatically set as long as Texture.addToCache is used, but may not be set if a Texture is added directly to the TextureCache array.

trim: Rectangle

This is the trimmed area of original texture, before it was put in atlas Please call updateUvs() after you change coordinates of trim manually.

uvMatrix: TextureMatrix

Default TextureMatrix instance for this texture. By default, that object is not created because its heavy.

valid: boolean

This will let the renderer know if the texture is valid. If it's not then it cannot be rendered.

prefixed: string | boolean

Accessors

  • get frame(): Rectangle
  • The frame specifies the region of the base texture that this texture uses. Please call updateUvs() after you change coordinates of frame manually.

    Returns Rectangle

  • set frame(frame): void
  • Parameters

    Returns void

  • get height(): number
  • The height of the Texture in pixels.

    Returns number

  • get resolution(): number
  • Returns resolution of baseTexture

    Returns number

  • get rotate(): number
  • Indicates whether the texture is rotated inside the atlas set to 2 to compensate for texture packer rotation set to 6 to compensate for spine packer rotation can be used to rotate or mirror sprites See PIXI.groupD8 for explanation

    Returns number

  • set rotate(rotate): void
  • Parameters

    • rotate: number

    Returns void

  • get width(): number
  • The width of the Texture in pixels.

    Returns number

  • get EMPTY(): Texture<Resource>
  • An empty texture, used often to not have to create multiple empty textures. Can not be destroyed.

    Returns Texture<Resource>

  • get WHITE(): Texture<CanvasResource>
  • A white texture of 16x16 size, used for graphics and other things Can not be destroyed.

    Returns Texture<CanvasResource>

Methods

  • Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns this

  • Creates a new texture object that acts the same as this one.

    Returns Texture<Resource>

    • The new texture
  • Destroys this texture

    Parameters

    • Optional destroyBase: boolean

      Whether to destroy the base texture as well

    Returns void

    Fires

    PIXI.Texture#destroyed

  • Calls each of the listeners registered for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Rest ...args: any[]

    Returns boolean

  • Return an array listing the events for which the emitter has registered listeners.

    Returns (string | symbol)[]

  • Return the number of listeners listening to a given event.

    Parameters

    • event: string | symbol

    Returns number

  • Return the listeners registered for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T

    Returns ((...args) => void)[]

  • Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any
    • Optional once: boolean

    Returns this

  • Add a listener for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns this

  • Add a one-time listener for a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any

    Returns this

  • Remove all listeners, or those of the specified event.

    Parameters

    • Optional event: string | symbol

    Returns this

  • Remove the listeners of a given event.

    Type Parameters

    • T extends string | symbol

    Parameters

    • event: T
    • Optional fn: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    • Optional context: any
    • Optional once: boolean

    Returns this

  • Updates this texture on the gpu.

    Calls the TextureResource update.

    If you adjusted frame manually, please call updateUvs() instead.

    Returns void

  • Updates the internal WebGL UV cache. Use it after you change frame or trim of the texture. Call it after changing the frame

    Returns void

  • Adds a Texture to the global TextureCache. This cache is shared across the whole PIXI object.

    Parameters

    • texture: Texture<Resource>

      The Texture to add to the cache.

    • id: string

      The id that the Texture will be stored against.

    Returns void

  • Helper function that creates a new Texture based on the source you provide. The source can be - frame id, image url, video url, canvas element, video element, base texture

    Type Parameters

    Parameters

    • source: TextureSource | TextureSource[]

      Source or array of sources to create texture from

    • Optional options: IBaseTextureOptions<RO>

      See PIXI.BaseTexture's constructor for options.

    • Optional strict: boolean

      Enforce strict-mode, see PIXI.settings.STRICT_TEXTURE_CACHE.

    Returns Texture<R>

    The newly created texture

  • Create a new Texture with a BufferResource from a typed array.

    Parameters

    • buffer: BufferType

      The optional array to use. If no data is provided, a new Float32Array is created.

    • width: number

      Width of the resource

    • height: number

      Height of the resource

    • Optional options: IBaseTextureOptions<IBufferResourceOptions>

      See PIXI.BaseTexture's constructor for options. Default properties are different from the constructor's defaults.

    Returns Texture<BufferResource>

    • The resulting new BaseTexture
  • Create a texture from a source and add to the cache.

    Type Parameters

    Parameters

    • source: string | ImageSource

      The input source.

    • imageUrl: string

      File name of texture, for cache and resolving resolution.

    • Optional name: string

      Human readable name for the texture cache. If no name is specified, only imageUrl will be used as the cache ID.

    • Optional options: IBaseTextureOptions<any>

    Returns Promise<Texture<R>>

    • Output texture
  • Useful for loading textures via URLs. Use instead of Texture.from because it does a better job of handling failed URLs more effectively. This also ignores PIXI.settings.STRICT_TEXTURE_CACHE. Works for Videos, SVGs, Images.

    Type Parameters

    Parameters

    • url: string | string[]

      The remote URL or array of URLs to load.

    • Optional options: IBaseTextureOptions<RO>

      Optional options to include

    Returns Promise<Texture<R>>

    • A Promise that resolves to a Texture.
  • Remove a Texture from the global TextureCache.

    Parameters

    • texture: string | Texture<Resource>

      id of a Texture to be removed, or a Texture instance itself

    Returns null | Texture<Resource>

    • The Texture that was removed

Generated using TypeDoc