Skip to content

decodeImage

Loads an image and waits until it is decoded, so it can be painted without a flash or a decoding pause (e.g. before swapping screens).

Outside a browser (no Image), it rejects with a TypeError.

Available since v3.1.5

import { decodeImage } from '@helpers4/browser';
// or, from the all-in-one package (same code, one install):
import { decodeImage } from 'helpers4/browser';
decodeImage(src: string): Promise<HTMLImageElement>
ParameterTypeDescription
srcstringThe image URL

Promise<HTMLImageElement> — The decoded image element

Insert the image only once it is decoded.

const image = await decodeImage('/screens/home.png');
container.replaceChildren(image);

A missing or corrupt image rejects, so it can be replaced by a placeholder.

const image = await decodeImage(url).catch(() => placeholder);

View source on GitHub