vue/uniapp H5页面截图

1.引入html2canvas

复制代码
npm install html2canvas

2.代码实现

javascript 复制代码
<template>
  <view class="container">
    <!-- 截图的目标区域,设置 id="capture-box" -->
    <view id="capture-box" class="content-box">
      123456
    </view>
  </view>
</template>
<script>
import html2canvas from "html2canvas"
export default {
    mounted(){
        const element = document.getElementById('capture-box');
        const canvas = html2canvas(element, {
              // 配置项
              useCORS: true, // 允许加载跨域图片(重要)
              scale: window.devicePixelRatio, // 提高清晰度(适配 Retina 屏幕)
              logging: false, // 关闭日志
              backgroundColor: '#ffffff', // 确保背景色正确(默认透明可能变黑)
              windowHeight: element.scrollHeight // 确保截图高度包含所有滚动内容
        });
        const base64Image = canvas.toDataURL('image/png');
    }
}
注:报错Error in mounted hook: "TypeError: canvas.toDataURL is not a function"
  1. 版本差异 :你引入的 html2canvas 版本较旧(例如 0.5.x 版本),该版本默认返回的是 DOM <canvas> 元素对象,确实有 toDataURL 方法;但如果你使用的是 1.x 版本,默认行为可能会发生细微变化,或者在某些异步处理中变量被意外覆盖。
  2. 变量命名冲突 :这是最常见的"低级错误"。在 Vue 的 <script> 中,你可能把接收截图结果的变量命名为 canvas,这可能会和 HTML 原生的 <canvas> 标签对象或者组件内的变量混淆,尤其是在 v-for 循环中使用了 canvas 作为 key 或 item 名字。
方案一:检查变量命名冲突
javascript 复制代码
const canvas = html2canvas(element)
const base64 = canvas.toDataURL();

改为

const myCanvas = html2canvas(element)
const base64 = myCanvas.toDataURL();
方案二:使用异步处理
javascript 复制代码
methods: {
    async handleCapture() {
        const element = document.getElementById('capture-box');
        const canvas = await html2canvas(element, {
          useCORS: true, // 允许加载跨域图片(重要)
          scale: window.devicePixelRatio, // 提高清晰度(适配 Retina 屏幕)
          logging: false, // 关闭日志
          backgroundColor: '#ffffff', // 确保背景色正确(默认透明可能变黑)
          windowHeight: element.scrollHeight // 确保截图高度包含所有滚动内容
        });
        const base64Image = canvas.toDataURL('image/png');

        console.log('截图成功', base64Image);
        
    }
}
方案三:检查变量命名冲突

有时异步处理可能导致变量丢失,可以尝试使用 .then() 调用,这是 html2canvas 最传统的写法,最稳妥:

javascript 复制代码
<template>
  <view class="container">
    <!-- 截图的目标区域,设置 id="capture-box" -->
    <view id="capture-box" class="content-box">
      123456
    </view>
  </view>
</template>
<script>
import html2canvas from "html2canvas"
export default {
    mounted(){
        const element = document.getElementById('capture-box');
        html2canvas(element, {
            // 配置项
            useCORS: true, // 允许加载跨域图片(重要)
            scale: window.devicePixelRatio, // 提高清晰度(适配 Retina 屏幕)
            logging: false, // 关闭日志
            backgroundColor: '#ffffff', // 确保背景色正确(默认透明可能变黑)
            windowHeight: element.scrollHeight // 确保截图高度包含所有滚动内容
        }).then(res=>{
            console.log(res.toDataURL('image/png'));
        })
    }
}
相关推荐
用户29869853014几秒前
React 前端实现 Excel 与 OpenXML 双向转换:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel
-梅3 分钟前
linux(番外)线程,锁,条件变量封装;日志与线程池
linux·c语言·前端·c++
S_WEAN13 分钟前
【C++】STL - Priority_queue的使用及模拟实现与仿函数
开发语言·c++
Rain的Java大神之路17 分钟前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端
星落梦生42 分钟前
Tlias前端案例涉及知识点 (14)
开发语言·前端·javascript
Moment1 小时前
27B 的本地模型也能做知识图谱?太强了!😍😍😍
前端·后端·面试
维克兜率天2 小时前
【维克】KDJ量化实战:如何用它判断买卖点?
前端
维克兜率天4 小时前
【维克】RSI相对强弱指数:判断多空力量的温度计
前端
FRing0311 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
wefg111 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++