Files
aida_front/src/component/Canvas/index.vue

113 lines
2.7 KiB
Vue
Raw Normal View History

2024-11-22 09:20:25 +08:00
<template>
2025-07-19 14:04:48 +08:00
<div class="canvasBox">
2025-06-09 10:25:54 +08:00
<!-- 画布编辑器 - 永久启用红绿图模式 -->
<div class="canvas-wrapper">
<CanvasEditor
ref="canvasEditor"
:enabledRedGreenMode="false"
/>
</div>
2025-07-19 14:04:48 +08:00
<div class="btn">123
<div class="gallery_btn" @click="exportElement">Export</div>
</div>
2025-06-09 10:25:54 +08:00
</div>
2024-11-22 09:20:25 +08:00
</template>
2025-06-09 10:25:54 +08:00
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import CanvasEditor from "./CanvasEditor/index.vue";
2024-11-22 09:20:25 +08:00
2025-06-09 10:25:54 +08:00
// 画布编辑器引用
const canvasEditor = ref(null);
2025-04-16 10:43:54 +08:00
2025-06-09 10:25:54 +08:00
const getJSON = () => {
if (canvasEditor.value) {
const json = canvasEditor.value.getJSON();
console.log("获取的JSON数据", json);
localStorage.setItem("redGreenModeJSON", json);
}
};
2024-11-22 09:20:25 +08:00
2025-06-09 10:25:54 +08:00
const loadJSON = () => {
if (canvasEditor.value) {
const currentJSON = localStorage.getItem("redGreenModeJSON");
canvasEditor.value.loadJSON(currentJSON);
console.log("加载的JSON数据", currentJSON);
}
};
2025-07-19 14:04:48 +08:00
const exportElement = ()=>{
canvasEditor.value.exportImage({isContainBg:true,isContainFixed:false}).then((rv)=>{
console.log(rv)
downloadBase64Image(rv,'canvas')
})
}
function downloadBase64Image(base64Data, filename) {
// 1. 提取MIME类型自动处理各种Base64前缀
const mimeMatch = base64Data.match(/^data:(.+?);base64,/);
if (!mimeMatch) {
console.error('Invalid Base64 data');
return;
}
// 2. 获取扩展名
const mimeType = mimeMatch[1];
const extension = mimeType.split('/')[1] || 'png';
2025-04-09 14:09:19 +08:00
2025-07-19 14:04:48 +08:00
// 3. 转换Base64为Blob
const byteString = atob(base64Data.split(',')[1]);
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
const blob = new Blob([ab], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${filename}.${extension}`;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 100);
}
2025-06-09 10:25:54 +08:00
// 组件挂载时绑定键盘事件
onMounted(() => {});
2025-04-01 15:25:15 +08:00
2025-06-09 10:25:54 +08:00
// 组件卸载时移除键盘事件
onUnmounted(() => {});
2024-11-22 09:20:25 +08:00
</script>
2025-06-09 10:25:54 +08:00
<style scoped lang="less">
2025-07-19 14:04:48 +08:00
.canvasBox {
2025-06-09 10:25:54 +08:00
width: 100%;
// height: 100vh;
2024-11-22 09:20:25 +08:00
height: 100%;
2025-07-19 14:04:48 +08:00
display: flex;
// flex-direction: column;
}
.btn{
margin-top: auto;
2025-06-09 10:25:54 +08:00
}
.canvas-wrapper {
flex: 1;
position: relative;
}
.canvas-wrapper-btns {
position: fixed;
top: 0;
right: 150px;
z-index: 1000;
display: flex;
gap: 20px;
& > div {
cursor: pointer;
padding: 10px 20px;
background-color: #f0f0f0;
border-radius: 5px;
transition: background-color 0.3s;
}
2024-11-22 09:20:25 +08:00
}
2025-06-09 10:25:54 +08:00
</style>