html2canvas如何實(shí)現(xiàn)把div保存圖片為高清圖

小編給大家分享一下html2canvas如何實(shí)現(xiàn)把div保存圖片為高清圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)公司專注于衢州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供衢州營(yíng)銷型網(wǎng)站建設(shè),衢州網(wǎng)站制作、衢州網(wǎng)頁(yè)設(shè)計(jì)、衢州網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開(kāi)發(fā)服務(wù),打造衢州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供衢州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

1.選擇html2canvas版本(這個(gè)版本可以放大倍數(shù)保證圖片清晰)

默認(rèn)生成的 canvas 圖片在 retina 設(shè)備上顯示很模糊,處理成 2 倍圖能解決這個(gè)問(wèn)題:

var w = $("#code").width();
var h = $("#code").height();
//要將 canvas 的寬高設(shè)置成容器寬高的 2 倍
var canvas = document.createElement("canvas");
canvas.width = w * 2;
canvas.height = h * 2;
canvas.style.width = w + "px";
canvas.style.height = h + "px";
var context = canvas.getContext("2d");
//然后將畫布縮放,將圖像放大兩倍畫到畫布上
context.scale(2,2);
html2canvas(document.querySelector("#code"), {
    canvas: canvas,
    onrendered: function(canvas) {
        ...
    }
});

下載方法:

.on('click','.download',function(){
                $('#mycanvas').remove();
                var _height=$('.skinReport').height();
                //滾到頂部
                $('html, body').animate({scrollTop:0});
                if(confirm('是否下載肌膚檢測(cè)報(bào)告?'))
                {
                    setTimeout(function(){
                        var canvas = document.createElement("canvas"),
                            w=$('#skinReport').width(),
                            h=$('#skinReport').height();
                        canvas.width = w * 2;
                        canvas.height = h * 2;
                        canvas.style.width = w + "px";
                        canvas.style.height = h + "px";
                        var context = canvas.getContext("2d");
//然后將畫布縮放,將圖像放大兩倍畫到畫布上
                        context.scale(2,2);
                        html2canvas(document.getElementById('skinReport'), {
                            allowTaint: false,
                            taintTest: true,
                            canvas: canvas,
                            onrendered: function(canvas) {
                                canvas.id = "mycanvas";
                                canvas.style.display = 'none';
                                document.body.appendChild(canvas);
                                //生成base64圖片數(shù)據(jù)
                                imgData = canvas.toDataURL(type);
                                //var newImg = document.createElement("img");
                                //newImg.src =  dataUrl;
                                //document.body.appendChild(newImg);
                                //console.log(imgData);
                                var _fixType = function(type) {
                                    type = type.toLowerCase().replace(/jpg/i, 'jpeg');
                                    var r = type.match(/png|jpeg|bmp|gif/)[0];
                                    return 'image/' + r;
                                };
                                // 加工image data,替換mime type
                                imgData = imgData.replace(_fixType(type),'image/octet-stream');
                                /**
                                 * 在本地進(jìn)行文件保存
                                 * @param  {String} data     要保存到本地的圖片數(shù)據(jù)
                                 * @param  {String} filename 文件名
                                 */
                                var saveFile = function(data, filename){
                                    var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
                                    save_link.href = data;
                                    save_link.download = filename;
                                    var event = document.createEvent('MouseEvents');
                                    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                                    save_link.dispatchEvent(event);
                                };
                                // 下載后的問(wèn)題名
                                var filename = aname+'肌膚檢測(cè)報(bào)告' + (new Date()).getTime() + '.' + type;
                                // download
                                saveFile(imgData,filename);
                            },
                            width:1512,
                            height:15000
                        })
                    },2500)
                }
                else
                {
                    return;
                }
            })

以上是“html2canvas如何實(shí)現(xiàn)把div保存圖片為高清圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章題目:html2canvas如何實(shí)現(xiàn)把div保存圖片為高清圖
鏈接URL:http://muchs.cn/article46/piephg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、全網(wǎng)營(yíng)銷推廣小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化