图像
图像可以在画布上的特定位置渲染。该组件提供了比原始画布图像绘制更好的生活质量改进,包括锚定位和自动纵横比处理。
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 属性确定图像的哪个点对应于指定的位置:
// 左上角锚点(默认)
const tlImage = new InteractiveImage({
src: '/image.png',
position: [100, 100],
anchor: 'tl', // 左上角
});
// 中心锚点
const centerImage = new InteractiveImage({
src: '/image.png',
position: [250, 150],
anchor: 'center',
});
// 右下角锚点
const brImage = new InteractiveImage({
src: '/image.png',
position: [400, 200],
anchor: 'br', // 右下角
});
可用的锚点值:
'tl'- 左上角'tr'- 右上角'bl'- 左下角'br'- 右下角'center'- 中心
尺寸调整
图像可以显式调整大小或按比例缩放:
// 显式尺寸
const explicitSize = new InteractiveImage({
src: '/image.png',
position: [100, 100],
width: 200,
height: 100,
});
// 保持宽高比
const proportionalWidth = new InteractiveImage({
src: '/image.png',
position: [100, 100],
width: 200,
preserveAspectRatio: true,
});
属性
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
src | string | - | 图像源 URL |
position | Vector2 | [0, 0] | 图像位置 [x, y] |
width | number | - | 图像宽度 |
height | number | - | 图像高度 |
anchor | Anchor | 'tl' | 位置锚点 |
opacity | number | 1 | 图像不透明度 (0-1) |
preserveAspectRatio | boolean | false | 保持宽高比 |