当前位置: 首页 > article >正文

Vue Dom截图插件,截图转Base64 html2canvas

安装插件

npm install html2canvas --save

插件使用

<template>
  <div style="padding: 10px;">
    <div ref="imageTofile" class="box">发生什么事了</div>
    <button @click="toImage" style="margin: 10px;">截图</button>
    <div>
      <img :src="htmlUrl" style="height: 288px; width: auto" />
    </div>
  </div>
</template>

<script>
import html2canvas from "html2canvas";
export default {
  data() {
    return {
      htmlUrl: "",
    };
  },

  methods: {
    toImage() {
      // 第一个参数是需要生成截图的元素,第二个是自己需要配置的参数,宽高等
      html2canvas(this.$refs.imageTofile, {
        useCORS: true, // 如果截图的内容里有图片,可能会有跨域的情况,加上这个参数,解决文件跨域问题
      }).then((canvas) => {
        let url = canvas.toDataURL("image/png");
        this.htmlUrl = url;
        // 把生成的base64位图片上传到服务器,生成在线图片地址
        console.log("base64 => ", this.htmlUrl);
      });
    },
  },
};
</script>
<style scoped>
.box {
  width: 512px;
  height: 288px;
  background-color: yellow;
}
</style>

效果

在这里插入图片描述


http://www.kler.cn/a/535393.html

相关文章:

  • go流程控制
  • Ubuntu部署Deepseek-R1模型(8b)
  • Kafka 可靠性探究—副本刨析
  • 深入理解和使用定时线程池ScheduledThreadPoolExecutor
  • Linux 基础命令
  • 嵌入式工程师必学(143):模拟信号链基础
  • H5+CSS+JS制作好看的轮播图
  • OSPF基础(2):数据包详解
  • 51单片机07 串口通信
  • 【C语言】常量指针和指针常量,指针数组和数组指针,指针函数和函数指针怎么区分?
  • vue2-nextTick
  • JAVA面试框架篇
  • 注册中心不知选哪个?Zookeeper、Eureka、Nacos、Consul和Etcd 5种全方位剖析对比
  • Python利用VideoCapture和FFmpeg读取多个rtsp流性能的比较
  • idea整合deepseek实现AI辅助编程
  • 【React】表单校验:从基础到集成库
  • Chapter 4-1. Troubleshooting Congestion in Fibre Channel Fabrics
  • π0开源了且推出自回归版π0-FAST——打造机器人动作专用的高效Tokenizer:比扩散π0的训练速度快5倍但效果相当
  • 2025_1_31 C语言中关于数组和指针
  • CentOS 7.9-2207更换实时内核
  • 【2025最新计算机毕业设计】基于SSM的智能停车场管理系统【提供源码+答辩PPT+文档+项目部署】(高质量源码,可定制,提供文档,免费部署到本地)
  • 使用 cipher /w 清除磁盘删除残留数据(Windows) - 随笔
  • Android版Kotlin版RxJava2+Retrofit2+OkHttp3的基础、封装和项目中的使用
  • 【工具篇】ChatGPT:开启人工智能新纪元
  • React 打印插件 -- react-to-print
  • C++中的pair,pair和map的结合