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是图片裁剪区域的高度。

示例​

图片资源(带 native 和 rect)​

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,
},
};

视频资源(仅 src 和 type)​

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

音频资源(仅 src 和 type)​

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

其他资源(仅 src 和 type)​

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

注意事项​

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