美文网首页
html2canvas保存图片

html2canvas保存图片

作者: Fa1se003 | 来源:发表于2021-04-23 12:09 被阅读0次

npm安装

cnpm install --save html2canvas
import html2canvas from 'html2canvas';

            var ss = document.querySelector('')
            html2canvas(ss).then(function (canvas) {
                // document.body.appendChild(canvas);
                console.log("canvas", canvas)

                var imgData = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");

                let $a = document.createElement('a');
                $a.setAttribute('href', imgData);
                $a.setAttribute('download', 'test222.png');
                let fileLink = document.createElement('span');
                fileLink.setAttribute('style', 'cursor: pointer; -webkit-tap-highlight-color: transparent');
                $a.appendChild(fileLink);
                let body = document.getElementsByTagName('body')[0];
                body.appendChild($a);
                fileLink.click();
                body.removeChild($a);
            });


相关文章

网友评论

      本文标题:html2canvas保存图片

      本文链接:https://www.haomeiwen.com/subject/shjcrltx.html