tiny-vue/examples/sites/demos/pc/app/crop/get-cropped-canvas-composit...

43 lines
1.2 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div>
<tiny-button @click="getCroppedCanvas" style="margin-bottom: 20px; max-width: unset">
获取裁剪后的图片数据
</tiny-button>
<tiny-button text="图片裁剪" @click="visible = !visible" style="margin-bottom: 20px"></tiny-button>
<tiny-crop
ref="cropRef"
:cropvisible="visible"
@update:cropvisible="visible = $event"
:src="imgUrl"
:crop-type="blob"
></tiny-crop>
</div>
</template>
<script setup lang="jsx">
import { ref } from 'vue'
import { Crop as TinyCrop, Button as TinyButton, Modal } from '@opentiny/vue'
const visible = ref(false)
const cropRef = ref()
const imgUrl = ref(`${import.meta.env.VITE_APP_BUILD_BASE_URL}static/images/mountain.png`)
function getCroppedCanvas() {
const canvas = cropRef.value.getCroppedCanvas()
if (canvas.toBlob) {
// 生成 Blob 图片格式
canvas.toBlob((img) => {
Modal.message({ message: `Blob${img.size}`, status: 'info' })
})
} else if (canvas.msToBlob) {
// 生成兼容 IE 的 Blob 图片格式
const img = canvas.msToBlob()
Modal.message({ message: `Blob${img.size}`, status: 'info' })
} else {
// 生成 base64 图片格式
canvas.toDataURL('image/jpeg')
}
}
</script>