应用HTML截图并储存为当地照片的完成编码

实际编码以下所示:
 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF⑻">
        <title>html2canvas_download</title>
        <style>
            a {
                cursor: pointer;
                color: rgb(85, 26, 139);
                text-decoration: underline;
            }
        </style>
    </head>
    <body>
        <div id="oDiv" style="width: 300px; height: 300px; margin: 10px; background: red; border: 5px solid gray;">
            <h1>hello world!</h1>
        </div>
        <!-- <script type="text/javascript" src="../dist/html2canvas.js"></script> -->
        <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>
        <script type="text/javascript">
            var oDiv = document.getElementById('oDiv');
            // body截图
            // html2canvas(document.body).then(function(canvas) {
            //     document.body.appendChild(canvas);
            // });
            html2canvas(oDiv).then(function(canvas) {
                document.body.appendChild(canvas);
                var oCavans = document.getElementsByTagName('canvas')[0];
                var strDataURI = oCavans.toDataURL();
                downLoadFn(strDataURI);
            });
            //分辨访问器种类
            function myBrowser() {
                var userAgent = navigator.userAgent; //获得访问器的userAgent标识符串
                var isOpera = userAgent.indexOf("Opera") > ⑴;
                if(isOpera) {
                    return "Opera"
                }; //分辨是不是Opera访问器
                if(userAgent.indexOf("Firefox") > ⑴) {
                    return "FF";
                } //分辨是不是Firefox访问器
                if(userAgent.indexOf("Chrome") > ⑴) {
                    return "Chrome";
                }
                if(userAgent.indexOf("Safari") > ⑴) {
                    return "Safari";
                } //分辨是不是Safari访问器
                if(userAgent.indexOf("compatible") > ⑴ && userAgent.indexOf("MSIE") > ⑴ && !isOpera) {
                    return "IE";
                }; //分辨是不是IE访问器
                if(userAgent.indexOf("Trident") > ⑴) {
                    return "Edge";
                } //分辨是不是Edge访问器
            }
            //IE访问器照片储存当地
            function SaveAs5(imgURL) {
                var oPop = window.open(imgURL, "", "width=1, height=1, top=5000, left=5000");
                for(; oPop.document.readyState != "complete";) {
                    if(oPop.document.readyState == "complete") break;
                }
                oPop.document.execCommand("SaveAs");
                oPop.close();
            }
            // chrome14+,firefox20+,pera15+,Edge 13+,Safari未完成
            function download(strDataURI) {
                var link = document.createElement('a');
                link.innerHTML = 'download_canvas_image';
                link.download = 'mypainting.png';
                link.addEventListener('click', function(ev) {
                    link.href = strDataURI;
                }, false);
                document.body.appendChild(link);
            };
            function downLoadFn(url) {
                if(myBrowser() === "IE" || myBrowser() === "Edge") {
                    SaveAs5(url);
                } else {
                    download(url);
                }
            }
        </script>
    </body>
</html>

总结

以上所述是网编给大伙儿详细介绍的应用HTML截图并储存为当地照片的完成编码,期待对大伙儿有一定的协助,假如大伙儿有任何疑惑请给我留言,网编会立即回应大伙儿的。在此也十分谢谢大伙儿对脚本制作之家网站的适用!