Skip to main content

Class: TextureAtlas

texture/atlas.TextureAtlas

Combines Textures into one single image using an offscreen canvas.

If a Texture is added to the atlas that has no image then a 1x1 pixel is used with the color of the texture.

The size of the atlas should be no larger than MAX_TEXTURE_SIZE to insure the atlas can be stored on the GPU.

Also provides an api to get the locations of textures on the atlas.

Hierarchy

Constructors

constructor

โ€ข new TextureAtlas(size)

Creates a Texture instance with a color.

Parameters

NameType
sizenumber

Overrides

Texture.constructor

Defined in

src/texture/atlas.ts:37

Properties

canvas

โ€ข Private canvas: HTMLCanvasElement

Defined in

src/texture/atlas.ts:34


color

โ€ข color: Color

Inherited from

Texture.color

Defined in

src/texture/texture.ts:13


ctx

โ€ข Private ctx: CanvasRenderingContext2D

Defined in

src/texture/atlas.ts:35


image

โ€ข image: HTMLImageElement

Inherited from

Texture.image

Defined in

src/texture/texture.ts:15


imagePath

โ€ข imagePath: string

Inherited from

Texture.imagePath

Defined in

src/texture/texture.ts:14


isOld

โ€ข isOld: boolean = false

Inherited from

Texture.isOld

Defined in

src/texture/texture.ts:16


size

โ€ข Private size: number

Defined in

src/texture/atlas.ts:31


textures

โ€ข Private textures: Map<Texture, TextureAtlasImage>

Defined in

src/texture/atlas.ts:32

Methods

addImages

โ–ธ addImages(...images): Promise<boolean>

Adds the given images to the atlas and then refreshes the atlas.

Parameters

NameTypeDescription
...imagesstring[]The paths to the images to add

Returns

Promise<boolean>

Wether or not the images were added

Defined in

src/texture/atlas.ts:54


addTexture

โ–ธ addTexture(texture, refreshAtlas?): boolean

Adds a texture to the atlas.

Parameters

NameTypeDefault valueDescription
textureTextureundefinedThe texture to add
refreshAtlasbooleanfalseWether or not to refresh the atlas once the texture has been added

Returns

boolean

Wether or not the texture was added to the atlas

Defined in

src/texture/atlas.ts:78


addTextures

โ–ธ addTextures(...textures): Promise<void>

Adds the given textures to the atlas and then refreshes the atlas.

Parameters

NameTypeDescription
...texturesTexture[]The textures to add.

Returns

Promise<void>

The promise returned by this.refreshAtlas()

Defined in

src/texture/atlas.ts:100


drawTexturesOnCanvas

โ–ธ drawTexturesOnCanvas(): void

Draws all textures stored in the atlas onto the canvas.

If this.packTextures has not been called beforehand then all textures will be drawn over each other or in undesired positions.

Returns

void

Defined in

src/texture/atlas.ts:203


findTallestTexture

โ–ธ Private findTallestTexture(textures): Object

Finds the tallest texture in the given list of textures.

Parameters

NameTypeDescription
texturesTextureAtlasImage[]The list of textures to search

Returns

Object

The tallest texture and it's index in textures

NameType
inumber
tTextureAtlasImage

Defined in

src/texture/atlas.ts:224


getAllTextures

โ–ธ getAllTextures(): TextureAtlasImage[]

Gets all the textures on the atlas as an array.

Returns

TextureAtlasImage[]

An array of all textures on the atlas

Defined in

src/texture/atlas.ts:140


getSize

โ–ธ getSize(): number

Gets the size of the atlas image.

Returns

number

The size of the atlas image

Defined in

src/texture/atlas.ts:248


getTexture

โ–ธ getTexture(texture): TextureAtlasImage

Gets the location (TextureAtlasImage) of the texture on the atlas.

Parameters

NameTypeDescription
textureTextureThe texture to find

Returns

TextureAtlasImage

The TextureAtlasImage of the texture on the atlas or undefined

Defined in

src/texture/atlas.ts:131


getTextureUnit

โ–ธ getTextureUnit(): number

Gets the texture unit the texture is stored in on the GPU, only applies to texture's which use an image.

Returns

number

The texture unit the texture is stored in on the GPU, -1 if it is not currently loaded.

Inherited from

Texture.getTextureUnit

Defined in

src/texture/texture.ts:67


loadImage

โ–ธ loadImage(): Promise<void>

Loads the atlas' canvas into this.image.

Returns

Promise<void>

A promise that resolves once the image has loaded or rejects if there is an error while loading the image.

Overrides

Texture.loadImage

Defined in

src/texture/atlas.ts:160


packTextures

โ–ธ packTextures(): void

Packs all textures in the atlas onto it's canvas.

It then loads the canvas as a data URL into this.image using this.loadImage.

Returns

void

Defined in

src/texture/atlas.ts:169


refreshAtlas

โ–ธ refreshAtlas(): Promise<void>

Packs all textures in the atlas together, draws them to the canvas and loads the canvas as a data URL to this.image.

Returns

Promise<void>

A promise that resolves once the image has loaded or rejects if there is an error while loading the atlas image.

Defined in

src/texture/atlas.ts:149


removeTexture

โ–ธ removeTexture(texture, refreshAtlas?): boolean

Removes a texture from the atlas.

Parameters

NameTypeDefault valueDescription
textureTextureundefinedThe texture to remove
refreshAtlasbooleanfalseWether or not to refresh the atlas once the texture has been removed

Returns

boolean

Wether or not the texture was removed from the atlas

Defined in

src/texture/atlas.ts:115


setTextureUnit

โ–ธ setTextureUnit(unit): void

Set the texture unit to be read from when rendering the texture.

Should only be used if you know what you are doing.

Parameters

NameTypeDescription
unitnumberThe texture unit the texture is stored in on the GPU

Returns

void

Inherited from

Texture.setTextureUnit

Defined in

src/texture/texture.ts:58