Appearance
xtf-image-crop
组件说明
xtf-image-crop 是图片裁剪,用于移动和缩放图片以生成裁剪结果。当前实现通过 confirm 等事件反馈交互、提供实例方法。
- 实际 Demo 路由:
#/pages/components/xtf-image-crop/index - Demo 源码
基础用法
1. 最小可用
提供能够直接看到或操作组件的最少业务内容。
vue
<template>
<view class="doc-minimum">
<XtfImageCrop src="/static/logo.png" />
</view>
</template>
<script setup lang="uts">
import XtfImageCrop from "@/uni_modules/xtf-linkuiunix/components/xtf-image-crop/xtf-image-crop.uvue"
</script>
<style>
.doc-minimum { display: flex; flex-direction: column; padding: 24rpx; }
</style>2. 核心数据与主要事件
使用组件真实数据入口,并记录当前源码声明的主要事件。
vue
<template>
<view class="doc-core">
<XtfImageCrop src="/static/logo.png" @confirm="onPrimaryEvent" />
</view>
</template>
<script setup lang="uts">
import XtfImageCrop from "@/uni_modules/xtf-linkuiunix/components/xtf-image-crop/xtf-image-crop.uvue"
function onPrimaryEvent(payload: any): void { console.log("xtf-image-crop:confirm", payload) }
</script>
<style>
.doc-core { display: flex; flex-direction: column; padding: 24rpx; }
</style>3. 动态 cropResizable 状态
运行时切换 cropResizable,观察组件对应状态变化。
vue
<template>
<view class="doc-dynamic">
<XtfImageCrop src="/static/logo.png" :crop-resizable="state" />
<button class="doc-action-button" @click="toggleState">切换 cropResizable</button>
<text class="doc-state-text">cropResizable: {{ state }}</text>
</view>
</template>
<script setup lang="uts">
import XtfImageCrop from "@/uni_modules/xtf-linkuiunix/components/xtf-image-crop/xtf-image-crop.uvue"
import { ref } from "vue"
const state = ref(false)
function toggleState(): void { state.value = !state.value; console.log("cropResizable", state.value) }
</script>
<style>
.doc-dynamic { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
.doc-state-text { margin-top: 12rpx; color: #475569; font-size: 24rpx; }
</style>4. 调用 refresh()
重新计算或刷新;签名为 refresh(): void。
vue
<template>
<view class="doc-method">
<XtfImageCrop src="/static/logo.png" ref="componentRef" />
<button class="doc-action-button" @click="callMethod">重新计算或刷新</button>
</view>
</template>
<script setup lang="uts">
import XtfImageCrop from "@/uni_modules/xtf-linkuiunix/components/xtf-image-crop/xtf-image-crop.uvue"
import { ref } from "vue"
const componentRef = ref<any>(null)
function callMethod(): void { componentRef.value?.refresh(); console.log("调用 refresh") }
</script>
<style>
.doc-method { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
</style>5. 父布局变化后显式 refresh()
弹层展开、隐藏容器恢复或父布局变化完成后调用 refresh;Canvas/Signature 应先收到 ready。此方法返回 void,不代表设备绘图或导出已完成。
vue
<template>
<view class="doc-layout-refresh">
<view :style="{ width: layoutWidth }"><XtfImageCrop src="/static/logo.png" ref="componentRef" /></view>
<button class="doc-action-button" @click="refreshLayout">切换父宽度并刷新尺寸</button>
</view>
</template>
<script setup lang="uts">
import XtfImageCrop from "@/uni_modules/xtf-linkuiunix/components/xtf-image-crop/xtf-image-crop.uvue"
import { nextTick, ref } from "vue"
const componentRef = ref<any>(null)
const layoutWidth = ref("100%")
async function refreshLayout(): Promise<void> { layoutWidth.value = layoutWidth.value == "100%" ? "80%" : "100%"; await nextTick(); componentRef.value?.refresh(); console.log("refresh 已请求") }
</script>
<style>
.doc-layout-refresh { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
</style>全部属性
| 属性 | 源码类型 | withDefaults 默认值 | 实际行为 / 范围 | 适用范围 |
|---|---|---|---|---|
src | string | "" | 参与组件运行逻辑;源码在 7 处读取该值;消费位置:script 行 124、script 行 132、script 行 138、script 行 167 | 需要覆盖组件默认业务值时设置。 |
width | number | 640 | 参与组件运行逻辑;源码在 5 处读取该值;消费位置:script 行 35、script 行 172、script 行 177、script 行 178 | 需要覆盖组件默认业务值时设置。 |
height | number | 480 | 参与组件运行逻辑;源码在 8 处读取该值;消费位置:script 行 32、script 行 35、script 行 169、script 行 172 | 需要覆盖组件默认业务值时设置。 |
confirmText | string | "确定" | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
tipText | string | "" | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
cropShape | string | "rect" | 参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 31、script 行 168 | 需要覆盖组件默认业务值时设置。 |
cropWidth | number | 320 | 设置对应区域宽度;消费位置:script 行 35、script 行 36、script 行 168 | 需要调整布局或视觉表现时设置。 |
cropHeight | number | 240 | 设置对应区域高度;消费位置:script 行 35、script 行 36、script 行 168 | 需要调整布局或视觉表现时设置。 |
cropResizable | boolean | false | 参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 72、script 行 87 | 需要覆盖组件默认业务值时设置。 |
exportFormat | string | "png" | 参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 179 | 需要覆盖组件默认业务值时设置。 |
exportQuality | number | 1 | 参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 180 | 需要覆盖组件默认业务值时设置。 |
backgroundColor | string | "" | 设置对应区域的颜色;消费位置:script 行 181 | 需要调整布局或视觉表现时设置。 |
事件
| 事件名称 | 源码触发边界 | 回调参数 | 参数说明 |
|---|---|---|---|
confirm | setup 内触发(script 行 217);参数表达式:{ dataUrl, width, height, cropRect: rect, cropShape: circle ? "circle" : "rect", format };前置 guard:!active()、!dataUrl.startsWith("data:image/" + format + ";") | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
error | setup 内触发(script 行 133);参数表达式:{ type: "image-info", message: "图片尺寸无效" };前置 guard:disposed || id != requestId || src != props.src、!validSize(Number(info.width)) || !validSize(Number(info.height))setup 内触发(script 行 138);参数表达式:{ type: "image-info", error }confirm 内触发(script 行 172);参数表达式:{ type: "size", message: "输出尺寸需为 1 至 4096,裁剪比例尺寸需大于 0" };前置 guard:exporting.value || disposed、!validOptions() || props.width > 4096 || props.height > 4096confirm 内触发(script 行 173);参数表达式:{ type: "source", message: "图片尚未就绪" };前置 guard:exporting.value || disposed、!validOptions() || props.width > 4096 || props.height > 4096、props.src.length == 0 || !selectionInitialized.valuefail 内触发(script 行 184);参数表达式:{ type, error };前置 guard:!active() | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
ready | setup 内触发(script 行 137);参数表达式:{ src, width: sourceWidth.value, height: sourceHeight.value };前置 guard:disposed || id != requestId || src != props.src、!validSize(Number(info.width)) || !validSize(Number(info.height)) | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
change | emitChange 内触发(script 行 50);参数表达式:{ x: selectionLeft.value, y: selectionTop.value, width: selectionWidth.value, height: selectionHeight.value, cropRect: cropRect() } | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
插槽
| 插槽 | Scope 参数 | 实际渲染位置 |
|---|---|---|
| - | - | 当前模板没有公开 slot。 |
方法
实例方法
| 方法 | 签名 / 返回值 | 调用边界 |
|---|---|---|
refresh | refresh(): void | 重新计算或刷新;通过组件 ref 调用,参数和返回值按签名处理。 |
公开状态
| 状态 | 类型 / 包装 | 读取边界 |
|---|---|---|
| - | - | 当前组件没有公开状态。 |
主题说明
- 当前组件 CSS 未读取
var(--xtf-*)主题变量;颜色或尺寸可能来自 props、固定样式或父级上下文。
平台与实现边界
- 基线为 HBuilderX 5.26+ 与 uni-app x;当前报告已验证 Web Chromium 的对应 Demo,不据此承诺 Safari、小程序或 App 原生端等价。
- Android、iOS、HarmonyOS、Vapor 原生路径、触摸手势、字体、Canvas、安全区和系统权限能力,只有源码或完整性报告明确覆盖的部分才可视为已验证。
- 本文按当前源码生成,不补入旧版
xtf-linkui存在但迁移版未实现的 props、事件、slot 或方法。 - 当前仅兼容接收、未发现运行消费的 props:
confirmText、tipText。 - refresh(): void 在 nextTick 重新测量 stage;有效尺寸变化会 initializeSelection,业务应注意裁剪框随之重新初始化,方法不负责导出。WEB || APP 条件编译 UniResizeObserver,WEB 另监听窗口 resize,小程序需显式调用。卸载时断开观察与窗口监听,旧图片加载结果受请求标识隔离。
- 详细公共边界见 平台与实现边界,逐项运行证据见项目根目录 COMPONENT_COMPLETENESS.md。