DOM元素转为图片

DOM元素转化图片

用到HTML2canvas:
http://html2canvas.hertzen.com/dist/html2canvas.min.js
IOS13.4版本在微信运行无效 在手机浏览器中正常

<!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;}
    #myCanvas{
        width: 100%;
        height: auto;
    }
    </style>
</head>
<body>
    <div id="tu_wei" style="width: 100%;height: 300px;background-color:rgba(0, 0, 0, 0);">
        <p style="font-size:16px;color:rgb(22, 17, 17);">你好呀!</p>
    </div>
    <!-- <button>生成图片</button><br> -->
    <div class="cover" style="display:bolck;">
        <div class="CoverImg" style="width:100%;height:100%;position:fixed;left:0;top:0;background: rgba(0, 0, 0, 0.5)">
             <img src="" alt="" id="myCanvas">
        </div>
    </div>
            ·
</body>
<script src="./js/jquery.js"></script>
<script src="http://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script>
    
    fofo()
    function fofo() {
        html2canvas(document.querySelector("#tu_wei"), {
             useCORS: true,
             logging: true,
             timeout: 500,
             allowTaint: false,
             }).then(canvas => {
            var imgUrl = canvas.toDataURL("image/jpeg",1);
            console.log(imgUrl);
            var img = document.querySelector("#myCanvas");
            img.src = imgUrl;
            
        });
    }
    
    //移动端长按屏幕方法
    $.fn.longPress = function(fn) { //长按监听
        var timeout = undefined;
        var $this = this;
        for (var i = 0; i < $this.length; i++) {
            $this[i].addEventListener('touchstart', function(event) {
                timeout = setTimeout(fn, 2000); //长按时间超过2000ms,则执行传入的方法
            }, false);
            $this[i].addEventListener('touchend', function(event) {
                clearTimeout(timeout); //长按时间少于2000ms,不会执行传入的方法
            }, false);
        }
    }
    $('body').longPress(function() {//长按执行
        fofo()
    });
</script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值