Skip to main content

Class: ImageManager

Fetches, loads, and provides images to the game.

Constructors

new ImageManager()

new ImageManager(game, baseUrls): ImageManager

Parameters

game: Game

baseUrls: GameBaseUrls

Returns

ImageManager

Source

ImageManager.ts:25

Properties

_scratchCanvas?

private optional _scratchCanvas: HTMLCanvasElement

Source

ImageManager.ts:18


baseUrls

private baseUrls: GameBaseUrls

Source

ImageManager.ts:22


canvasKit

private canvasKit: CanvasKit

Source

ImageManager.ts:17


ctx?

private optional ctx: CanvasRenderingContext2D

Source

ImageManager.ts:19


game

private game: Game

Source

ImageManager.ts:21


images

images: Record<string, M2Image> = {}

Source

ImageManager.ts:16


missingLocalizationImagePaint?

optional missingLocalizationImagePaint: Paint

Source

ImageManager.ts:23


scale?

private optional scale: number

Source

ImageManager.ts:20

Accessors

scratchCanvas

get private scratchCanvas(): HTMLCanvasElement

Returns the scratchCanvas, which is an extra, non-visible canvas in the DOM we use so the native browser can render images like svg, png, jpg, that we later will convert to CanvasKit Image.

Returns

HTMLCanvasElement

Source

ImageManager.ts:472

Methods

addImage()

Internal

addImage(image): void

Adds a m2c2kit image (M2Image) to the images ready for the game.

For m2c2kit library use only

Parameters

image: M2Image

A m2c2kit image

Returns

void

Remarks

Typically, a programmer won't call this because images will be automatically rendered and loaded in initializeImages(). One reason this function is called in-game is when the game takes a screenshot and adds it as an outgoing image for transitions.

Source

ImageManager.ts:463


arrayBufferToBase64Async()

private arrayBufferToBase64Async(buffer): Promise<string>

Parameters

buffer: ArrayBuffer

Returns

Promise<string>

Source

ImageManager.ts:403


checkImageNamesForDuplicates()

private checkImageNamesForDuplicates(browserImages): void

Parameters

browserImages: BrowserImage[]

Returns

void

Source

ImageManager.ts:195


configureImageLocalization()

private configureImageLocalization(m2Image): void

Parameters

m2Image: M2Image

Returns

void

Source

ImageManager.ts:110


getImage()

Internal

getImage(imageName): M2Image

Returns a m2c2kit image (M2Image) that has been loaded by the ImageManager.

For m2c2kit library use only

Parameters

imageName: string

The name given to the previously rendered image

Returns

M2Image

A m2c2kit image

Remarks

Typically, a user won't call this because they use a higher-level abstraction (m2c2kit Sprite).

Source

ImageManager.ts:447


inferImageSubtypeFromUrl()

private inferImageSubtypeFromUrl(url?): string

Parameters

url?: string

Returns

string

Source

ImageManager.ts:414


initializeImages()

Internal

initializeImages(browserImages): Promise<void>

Loads image assets and makes them ready to use during the game initialization.

For m2c2kit library use only

Parameters

browserImages: undefined | BrowserImage[]

array of BrowserImage objects

Returns

Promise<void>

A promise that completes when all images have loaded

Remarks

Typically, a user won't call this because the m2c2kit framework will call this automatically.

Source

ImageManager.ts:42


loadImages()

loadImages(browserImages): Promise<void>

Loads an array of images and makes them ready for the game.

Parameters

browserImages: BrowserImage[]

an array of BrowserImage

Returns

Promise<void>

A promise that completes when all images have loaded

Remarks

Using the browser's image rendering, this method converts the images (png, jpg, svg, or svg string) into m2c2kit images (M2Image). Rendering is an async activity, and thus this method returns a promise. Rendering of all images is done in parallel.

Source

ImageManager.ts:60


localizeImageUrl()

private localizeImageUrl(url, locale): string

Localizes the image URL by appending the locale to the image URL, immediately before the file extension.

Parameters

url: string

url of the image

locale: string

locale in format of xx-YY, where xx is the language code and YY is the country code

Returns

string

localized url

Remarks

For example, https://url.com/file.png in en-US locale becomes https://url.com/file.en-US.png. A URL without an extension will throw an error.

Source

ImageManager.ts:167


prepareDeferredImage()

Internal

prepareDeferredImage(image): Promise<void>

Makes ready to the game a m2c2kit image (M2Image) that was previously loaded, but whose browser rendering was deferred.

For m2c2kit library use only

Parameters

image: M2Image

M2Image to render and make ready

Returns

Promise<void>

A promise that completes when the image is ready

Source

ImageManager.ts:218


reinitializeLocalizedImages()

reinitializeLocalizedImages(): void

Sets an image to be re-rendered within the current locale.

Returns

void

Source

ImageManager.ts:180


removeScratchCanvas()

private removeScratchCanvas(): void

Returns

void

Source

ImageManager.ts:492


renderM2Image()

private renderM2Image(image): Promise<void>

Uses the browser to render an image to a CanvasKit Image and make it ready to the game as an M2Image.

Parameters

image: M2Image

The M2Image to render

Returns

Promise<void>

A promise of type void that resolves when the image has been rendered

Remarks

This is complex because we rely on the browser's rendering and HTML image element processing. This involves a number of steps, including events, callbacks, and error handling. In addition, there are two types of images to be rendered: 1) url to an image (e.g., jpg, png, svg), and 2) svg string.

Source

ImageManager.ts:261