Skip to content

事件中心

事件一览表

事件名说明返回值
afterInit容器初始化完成初始化完成时的裁剪结果
imageLoaded设置图片加载完成图片属性
imageError图片设置失败回调失败原因
imageUpdate图片更新回调图片属性
preview实时预览预览结果(string)

afterInit

  • 描述

Avatar Clipper 初始化完成后执行,可用于获取第一次截图结果,以初始化图片地址。

  • 用法
ts
clipper.event.on("afterInit", (result: string) => {});
  • 示例

imageLoaded

  • 描述

图片加载完成,可获取图片初始数据。

提示

图片加载完成后,图片数据已初始化完成,此时可进行图片操作,使用场景如下:

1️⃣ 获取图片初始化后的缩放比例,以实现 command 丝滑缩放效果;

2️⃣ 获取图片初始化后的图片位置,以实现 command 丝滑移动效果;

温馨提示

imageLoaded 当且仅当图片加载完成后执行,为了实时获取更新的图片信息,建议使用 imageUpdate 事件代替。 每一次 imageLoaded 事件被触发,也会同步触发 imageUpdate 事件。

✅️ 用法示例:
ts
/**
 * 定义图片的基础属性,用于后期变换使用
 */
const imageAttrs = { scaleX: 1, scaleY: 1, rotation: 0 };

// 监听  imageLoaded 事件,初始化图片参数
clipper.event.on("imageLoaded", (attrs) => {
	imageAttrs.scaleX = attrs.scaleX!;
	imageAttrs.scaleY = attrs.scaleY!;
	imageAttrs.rotation = attrs.rotation!;
});
// ⚠️ 请注意:如果使用 imageLoaded 事件,则手动缩放图片时,imageAttrs的缩放比例不会变化,需要自行维护。

// 建议使用 imageUpdate 事件
clipper.event.on("imageUpdate", (attrs) => {
	imageAttrs.scaleX = attrs.scaleX!;
	imageAttrs.scaleY = attrs.scaleY!;
	imageAttrs.rotation = attrs.rotation!;
});
/** 缩小图片 **/
function reduce() {
	clipper.command.updateImageAttrs({ scaleX: imageAttrs.scaleX - scaleStep, scaleY: imageAttrs.scaleY - scaleStep });
}

/** 放大图片 **/
function enlarge() {
	clipper.command.updateImageAttrs({ scaleX: imageAttrs.scaleX + scaleStep, scaleY: imageAttrs.scaleY + scaleStep });
}
  • 用法
ts
clipper.event.on("imageLoaded", (imageAttrs: ImageAttrs) => {});

imageError

  • 描述

图片加载异常

  • 用法
ts
clipper.event.on("imageError", (error: string | Event) => {});
  • 返回值
string | Event
  • 示例

imageUpdate

  • 描述

图片更新事件(拖拽|缩放|旋转|设置图片源|...)

  • 用法
ts
clipper.event.on("imageUpdate", (imageAttrs: ImageAttrs) => {});

preview

  • 描述

获取实时预览图片信息

  • 用法
ts
clipper.event.on("preview", (result: string) => {});
  • 示例