-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(demo->screenshot): optimize the screenshot code && add print and…
… download (#3885)
- Loading branch information
Showing
1 changed file
with
58 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,77 @@ | ||
<template> | ||
<PageWrapper title="截图示例"> | ||
<Row :gutter="24"> | ||
<Col :span="3"> | ||
<Card title="截图"> | ||
<a-button type="primary" @click="screenShot">点击截图</a-button> | ||
<div class="mt-8" v-show="open"> | ||
<a-button type="primary" @click="Dele">点击删除</a-button> | ||
</div> | ||
</Card> | ||
</Col> | ||
<Col :span="21"> | ||
<Card title="截图内容" v-show="open"> | ||
<div ref="picture"></div> | ||
</Card> | ||
</Col> | ||
</Row> | ||
<CollapseContainer title="截图操作"> | ||
<a-button type="primary" class="mr-2" @click="handleScreenshot">截取当前body</a-button> | ||
<a-button type="primary" class="mr-2" :disabled="!showPicture" @click="handleDelScreenshot" | ||
>删除截图</a-button | ||
> | ||
<a-button type="primary" class="mr-2" :disabled="!showPicture" @click="handlePrintScreenshot" | ||
>打印截图</a-button | ||
> | ||
<a-button | ||
type="primary" | ||
class="mr-2" | ||
:disabled="!showPicture" | ||
@click="handleDownloadScreenshot" | ||
>下载截图</a-button | ||
> | ||
</CollapseContainer> | ||
|
||
<Card title="截图内容" class="mt-4"> | ||
<div ref="pictureRef" v-show="showPicture"></div> | ||
</Card> | ||
</PageWrapper> | ||
</template> | ||
<script lang="ts" setup> | ||
import { PageWrapper } from '@/components/Page'; | ||
import html2canvas from 'html2canvas'; | ||
import { ref } from 'vue'; | ||
import { Card, Col, Row } from 'ant-design-vue'; | ||
import { Card } from 'ant-design-vue'; | ||
import { CollapseContainer } from '@/components/Container'; | ||
import printJS from 'print-js'; | ||
import { downloadByBase64 } from '@/utils/file/download'; | ||
const pictureRef = ref(); | ||
const showPicture = ref<boolean>(false); | ||
const canvasUrl = ref<string>(''); | ||
const picture = ref(); | ||
const open = ref(false); | ||
function screenShot() { | ||
if (open.value) { | ||
return; | ||
} | ||
function handleScreenshot() { | ||
if (showPicture.value) return; | ||
html2canvas(document.body, { | ||
backgroundColor: '#ffffff', | ||
allowTaint: true, //开启跨域 | ||
useCORS: true, | ||
scrollY: 0, | ||
scrollX: 0, | ||
}).then(function (canvas) { | ||
canvas.style.width = '100%'; | ||
canvas.style.height = '100%'; | ||
picture.value.appendChild(canvas); | ||
open.value = true; | ||
}) | ||
.then(function (canvas) { | ||
canvas.style.width = '100%'; | ||
canvas.style.height = '100%'; | ||
pictureRef.value.appendChild(canvas); | ||
showPicture.value = true; | ||
canvasUrl.value = canvas.toDataURL(); | ||
}) | ||
.catch((err) => { | ||
console.log('绘制失败', err); | ||
}); | ||
} | ||
function handleDelScreenshot() { | ||
pictureRef.value.innerHTML = ''; | ||
canvasUrl.value = ''; | ||
showPicture.value = false; | ||
} | ||
function handlePrintScreenshot() { | ||
if (!canvasUrl.value) return; | ||
printJS({ | ||
printable: canvasUrl.value, | ||
type: 'image', | ||
base64: true, | ||
}); | ||
} | ||
function Dele() { | ||
picture.value.innerHTML = ''; | ||
open.value = false; | ||
function handleDownloadScreenshot() { | ||
downloadByBase64(canvasUrl.value, 'screen_shot.png'); | ||
} | ||
</script> |