Skip to main content

Images

Images can be rendered at specific locations on the canvas. The component provides quality-of-life improvements over raw canvas image drawing, including anchor positioning and automatic aspect ratio handling.

import { InteractiveImage } from '@wangyaoshen/locus-interaction';

const image = new InteractiveImage({
src: '/path/to/image.png',
position: [100, 100],
width: 200,
height: 150,
anchor: 'center',
});

Anchor Positions

The anchor property determines which point of the image corresponds to the specified position:

// Top-left anchor (default)
const tlImage = new InteractiveImage({
src: '/image.png',
position: [100, 100],
anchor: 'tl', // top-left
});

// Center anchor
const centerImage = new InteractiveImage({
src: '/image.png',
position: [250, 150],
anchor: 'center',
});

// Bottom-right anchor
const brImage = new InteractiveImage({
src: '/image.png',
position: [400, 200],
anchor: 'br', // bottom-right
});

Available anchor values:

  • 'tl' - top-left
  • 'tr' - top-right
  • 'bl' - bottom-left
  • 'br' - bottom-right
  • 'center' - center

Sizing

Images can be sized explicitly or scaled proportionally:

// Explicit dimensions
const explicitSize = new InteractiveImage({
src: '/image.png',
position: [100, 100],
width: 200,
height: 100,
});

// Maintain aspect ratio with width
const proportionalWidth = new InteractiveImage({
src: '/image.png',
position: [100, 100],
width: 200,
preserveAspectRatio: true,
});

Props

NameTypeDefaultDescription
srcstring-Image source URL
positionVector2[0, 0]Image position [x, y]
widthnumber-Image width
heightnumber-Image height
anchorAnchor'tl'Position anchor point
opacitynumber1Image opacity (0-1)
preserveAspectRatiobooleanfalseMaintain aspect ratio