91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

html2canvas如何將div保存高清圖

發布時間:2021-06-28 14:39:06 來源:億速云 閱讀:369 作者:小新 欄目:web開發

這篇文章主要介紹html2canvas如何將div保存高清圖,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

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

默認生成的 canvas 圖片在 retina 設備上顯示很模糊,處理成 2 倍圖能解決這個問題:

var w = $("#code").width();
var h = $("#code").height();

//要將 canvas 的寬高設置成容器寬高的 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('是否下載肌膚檢測報告?'))
                {

                    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圖片數據

                                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');
                                /**
                                 * 在本地進行文件保存
                                 * @param  {String} data     要保存到本地的圖片數據
                                 * @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);
                                };

                                // 下載后的問題名
                                var filename = aname+'肌膚檢測報告' + (new Date()).getTime() + '.' + type;
                                // download
                                saveFile(imgData,filename);
                            },
                            width:1512,
                            height:15000
                        })
                    },2500)
                }
                else
                {
                    return;
                }

            })

以上是“html2canvas如何將div保存高清圖”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

九台市| 武胜县| 兴义市| 犍为县| 黑龙江省| 高碑店市| 荔波县| 贵港市| 松江区| 隆尧县| 凭祥市| 开原市| 镇安县| 民权县| 沿河| 大冶市| 临沧市| 额济纳旗| 南阳市| 松潘县| 恭城| 嵊州市| 湘乡市| 南江县| 双牌县| 博白县| 祁阳县| 南汇区| 井陉县| 赫章县| 洪江市| 兰西县| 苗栗市| 灵川县| 阜新市| 库尔勒市| 綦江县| 峡江县| 鹤庆县| 克山县| 琼海市|