<template>
<div>
<el-button type="primary" :loading="loading" @click="capture">截取示例卡片</el-button>
<div ref="target" class="capture-card">
<h3>订单 A001</h3>
<p>服务内容:系统安装与使用培训</p>
<p>状态:待交付</p>
</div>
<p role="status">{{ message }}</p>
<el-dialog v-model="visible" title="截图预览" width="min(640px, 92vw)" append-to-body>
<img v-if="image" :src="image" alt="订单卡片截图" class="capture-result" />
</el-dialog>
</div>
</template>
<script setup>
import { onBeforeUnmount, ref } from 'vue'
import { $Screenshot } from '@smallwei/avue'
const target = ref(null)
const image = ref('')
const loading = ref(false)
const visible = ref(false)
const message = ref('点击按钮生成截图,不上传数据')
let active = true
async function capture() {
loading.value = true
message.value = '正在生成截图'
try {
const canvas = await $Screenshot(target.value, { backgroundColor: '#fff', scale: 1, logging: false })
if (!active) return
image.value = canvas.toDataURL('image/png')
visible.value = true
message.value = '截图完成:' + canvas.width + ' × ' + canvas.height
} catch (error) {
if (active) message.value = '截图失败:' + error.message
} finally {
if (active) loading.value = false
}
}
onBeforeUnmount(() => { active = false })
</script>
<style scoped>
.capture-card { margin-top: 16px; padding: 20px; max-width: 500px; border: 1px solid #dcdfe6; color: #303133; background: white; }
.capture-card h3 { margin-top: 0; }
.capture-result { display: block; max-width: 100%; height: auto; }
</style>