Skip to main content

Resource 资源

类型定义

Resource 类型定义

export type Resource = {
src: string;
type: "image" | "video" | "audio" | "other";
native?: {
width: number;
height: number;
};
rect?: {
x: number;
y: number;
width: number;
height: number;
};
};

字段说明

字段类型必选描述
srcstring资源的路径或 URL。
type'image''video''audio''other'资源的类型:'image'(图片)、'video'(视频)、'audio'(音频)、'other'(其他)。

native 字段(仅 type'image' 时可用)

字段类型必选描述
widthnumber图片的原始宽度(像素)。
heightnumber图片的原始高度(像素)。

rect 字段(仅 type'image' 时可用)

字段类型必选描述
xnumber图片裁剪区域的左上角 x 坐标。
ynumber图片裁剪区域的左上角 y 坐标。
widthnumber图片裁剪区域的宽度。
heightnumber图片裁剪区域的高度。

示例

图片资源(带 nativerect

const imageResource: Resource = {
src: "https://example.com/image.jpg",
type: "image",
native: {
width: 1920,
height: 1080,
},
rect: {
x: 100,
y: 200,
width: 800,
height: 600,
},
};

视频资源(仅 srctype

const videoResource: Resource = {
src: "https://example.com/video.mp4",
type: "video",
};

音频资源(仅 srctype

const audioResource: Resource = {
src: "https://example.com/audio.mp3",
type: "audio",
};

其他资源(仅 srctype

const otherResource: Resource = {
src: "https://example.com/data.json",
type: "other",
};

注意事项

  1. nativerect 字段
    • 仅当 type'image' 时可用,其他类型无需提供。
  2. rect 的作用
    • 用于指定图片的裁剪区域,单位为像素,基于原始图片尺寸(native)。
  3. type 的扩展性
    • 如果未来需要支持更多资源类型,可以扩展 type 的枚举值。