html2canvas截屏

1、安装

npm install --save html2canvas

2、导入

import html2canvas from "html2canvas"

3、基本语法

let dom = document.getElementById("id"); 
html2canvas(dom, {
    backgroundColor: "#fff",
}).then((res) => {
    let url = res.toDataURL("image/png");
    console.log(url)
});

4、可用参数

参数名称类型默认值描述
allowTaintbooleanfalse允许跨域
widthnumbernullcanvas宽度
heightnumbernullcanvas高度
backgroundstring#fffcanvas的背景颜色,如果没有设定默认透明
letterRenderingbooleanfalse在设置了字间距的时候有用
loggingbooleanfalse在console.log()中输出信息
proxystringundefined代理地址
taintTestbooleantrue是否在渲染前测试图片
timeoutnumber0图片加载延迟,默认延迟为0,单位毫秒
useCORSbooleanfalse

5、截屏并下载

        运行效果:

        源码:

<template>
    <div>
       <span title="截屏" class="iconfont icon-screenshot" @click="handleClickSreen"></span>
       <el-dialog :area="[900,614]" title="截屏" :visible.sync="isShowDialog">
           <img :src="canvasUrl" class="screen-img" />
           <div slot="footer" class="dialog-footer">
               <a download="截屏" :href="canvasUrl">下 载</a>
               <el-button @click="isShowDialog = false">取 消</el-button>
           </div>
       </el-dialog>        
    </div>
</template>
<script>
    import html2canvas from "html2canvas"
    export default {
        data() {
            return {
                isShowDialog: false,//是否展示截图弹窗
                canvasUrl: ""
            }
        },        
        methods: {
            // 截屏
            handleClickSreen() {
                let dom = document.getElementById("app");
                html2canvas(dom)
                    .then((res) => {
                        this.canvasUrl = res.toDataURL("image/png");
                        if (this.canvasUrl) {
                            this.isShowDialog = true;
                        }
                    });
            },        
        }
    }
</script>
<style scoped lang="less">
    .dialog-footer a{
        display: inline-block;
        background-color: #e72528;
        width: 96px;
        height: 32px;
        border: 1px solid #e72528;
        color: #fff;
        line-height: 30px;
        text-align: center;
        cursor: pointer;
        border-radius: 2px;
        text-decoration: none;
        transition: all 0.2s;
        margin-right: 8px;
        &:hover {
            background-color: #EC5153;
            border-color: #EC5153;
        }
    }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值