Class: ImageManager
Fetches, loads, and provides images to the game.
Constructors
new ImageManager()
new ImageManager(
game
,baseUrls
):ImageManager
Parameters
• game: Game
• baseUrls: GameBaseUrls
Returns
Source
Properties
_scratchCanvas?
private
optional
_scratchCanvas:HTMLCanvasElement
Source
baseUrls
private
baseUrls:GameBaseUrls
Source
canvasKit
private
canvasKit:CanvasKit
Source
ctx?
private
optional
ctx:CanvasRenderingContext2D
Source
game
private
game:Game
Source
images
images:
Record
<string
,M2Image
> ={}
Source
missingLocalizationImagePaint?
optional
missingLocalizationImagePaint:Paint
Source
scale?
private
optional
scale:number
Source
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
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
arrayBufferToBase64Async()
private
arrayBufferToBase64Async(buffer
):Promise
<string
>
Parameters
• buffer: ArrayBuffer
Returns
Promise
<string
>
Source
checkImageNamesForDuplicates()
private
checkImageNamesForDuplicates(browserImages
):void
Parameters
• browserImages: BrowserImage
[]
Returns
void
Source
configureImageLocalization()
private
configureImageLocalization(m2Image
):void
Parameters
• m2Image: M2Image
Returns
void
Source
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
A m2c2kit image
Remarks
Typically, a user won't call this because they use a higher-level abstraction (m2c2kit Sprite).
Source
inferImageSubtypeFromUrl()
private
inferImageSubtypeFromUrl(url
?):string
Parameters
• url?: string
Returns
string
Source
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
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
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
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
reinitializeLocalizedImages()
reinitializeLocalizedImages():
void
Sets an image to be re-rendered within the current locale.
Returns
void
Source
removeScratchCanvas()
private
removeScratchCanvas():void
Returns
void
Source
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.