Skip to content

Image

An Image renders a CanvasImageSource (such as an HTMLImageElement, HTMLCanvasElement, or ImageBitmap) to any context. When interpolated, the image crossfades from one source to another.

Example

Click Next Image to crossfade between images using interpolateImage.

Usage

ts
import {
    createImage,
} from '@ripl/core';

const img = new Image();
img.src = '/photo.jpg';

img.onload = () => {
    const image = createImage({
        image: img,
        x: 50,
        y: 50,
        width: 200,
        height: 150,
    });
};

Properties

PropertyTypeRequiredDescription
imageCanvasImageSourceYesThe image source to render (e.g. HTMLImageElement, HTMLCanvasElement, ImageBitmap)
xnumberYesX coordinate of the top-left corner
ynumberYesY coordinate of the top-left corner
widthnumberNoRender width. Defaults to the source's intrinsic width.
heightnumberNoRender height. Defaults to the source's intrinsic height.

Plus all Element style properties.

Interpolation

The interpolateImage factory produces a crossfade between two image sources. It composites both images onto an offscreen canvas, blending from one to the other over the transition.

ts
import {
    interpolateImage,
} from '@ripl/core';

// Pass the interpolator directly as the state value
renderer.transition(imageElement, {
    duration: 800,
    state: { image: interpolateImage(currentImage, nextImage) },
});