html2canvas保存图片

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保存图片)