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
โณ
TextureAtlas
Constructors
constructor
โข new TextureAtlas(size)
Creates a Texture instance with a color.
Parameters
| Name | Type |
|---|---|
size | number |
Overrides
Defined in
src/texture/atlas.ts:37
Properties
canvas
โข Private canvas: HTMLCanvasElement
Defined in
src/texture/atlas.ts:34
color
โข color: Color
Inherited from
Defined in
src/texture/texture.ts:13
ctx
โข Private ctx: CanvasRenderingContext2D
Defined in
src/texture/atlas.ts:35
image
โข image: HTMLImageElement
Inherited from
Defined in
src/texture/texture.ts:15
imagePath
โข imagePath: string
Inherited from
Defined in
src/texture/texture.ts:14
isOld
โข isOld: boolean = false
Inherited from
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
| Name | Type | Description |
|---|---|---|
...images | string[] | 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
| Name | Type | Default value | Description |
|---|---|---|---|
texture | Texture | undefined | The texture to add |
refreshAtlas | boolean | false | Wether 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
| Name | Type | Description |
|---|---|---|
...textures | Texture[] | 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
| Name | Type | Description |
|---|---|---|
textures | TextureAtlasImage[] | The list of textures to search |
Returns
Object
The tallest texture and it's index in textures
| Name | Type |
|---|---|
i | number |
t | TextureAtlasImage |
Defined in
src/texture/atlas.ts:224
getAllTextures
โธ getAllTextures(): TextureAtlasImage[]
Gets all the textures on the atlas as an array.
Returns
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
| Name | Type | Description |
|---|---|---|
texture | Texture | The texture to find |
Returns
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
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
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
| Name | Type | Default value | Description |
|---|---|---|---|
texture | Texture | undefined | The texture to remove |
refreshAtlas | boolean | false | Wether 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
| Name | Type | Description |
|---|---|---|
unit | number | The texture unit the texture is stored in on the GPU |
Returns
void
Inherited from
Defined in
src/texture/texture.ts:58