基于html2canvas给单页面或者图片打水印

责编:menVScode 2020-09-30 14:18 阅读(91)

html2canvas文档地址:

http://html2canvas.hertzen.com/configuration/

代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            background: #fff;
            position: relative;
        }

        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .text {
            font-size: 20px;
            text-align: center;
            position: absolute;
            top: 50%;
            left: 30%;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="https://sucai.suoluomei.cn/sucai_zs/images/20191204145036-pic.png" alt="">
        <div class="text">产品大秘籍</div>
    </div>
    <img class="store" src="" alt="">
</body>
<script src="https://sucai.suoluomei.cn/sucai_zs/file/20200902165908-html2canvas.min.js"></script>
<script>
    setTimeout(() => {
        var main = document.querySelector('.box');
        html2canvas(main, {
            useCORS: true
        }).then((canvas) => {
            var dataUrl = canvas.toDataURL();
            document.querySelector('.store').src = dataUrl
        })
    }, 200)
</script>

</html>

效果图


前端交流群: MVC前端网(menvscode.com)-qq交流群:551903636

邮箱快速注册

忘记密码