使用html2canvas实现页面截图某区域
安装
javascript
npm install html2canvas
页面使用
引入
javascript
import html2canvas from 'html2canvas';
实现截图
javascript
<template>
截图
<div ref="captureArea" class="capture-area">
<!-- 这是你想要截图的区域 -->
<p>这个区域将会被截图。</p>
<el-table :data="tableData" border style="width: 100%">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</div>
<button @click="takeScreenshot">截图</button>
<img v-if="screenshot" :src="screenshot" alt="Screenshot" />
</template>
<script setup lang="ts">
import { ref } from "vue";
import html2canvas from 'html2canvas';
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
const captureArea: any = ref(null);
const screenshot: any = ref(null);
const takeScreenshot = async () => {
// 使用 html2canvas 截图
const canvas = await html2canvas(captureArea.value);
screenshot.value = canvas.toDataURL('image/png');
}
</script>
<style>
.capture-area {
padding: 20px;
background-color: #f9f9f9;
border: 1px solid #ccc;
}
</style>