欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂告訴你html2canvas把div保存圖片高清圖的方法

2018-05-14 13:29:10 2295瀏覽

本篇文章扣丁學(xué)堂html5培訓(xùn)小編給大家分享html2canvas把div保存圖片高清圖的方法示例及相關(guān)資料,對html5開發(fā)技術(shù)感興趣的小伙伴可以看一下以作參考。

扣丁學(xué)堂告訴你html2canvas把div保存圖片高清圖的方法

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


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


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('是否下載肌膚檢測報告?'))
                {

                    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);
                                };

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

            })



以上就是給大家分享的html2canvas把div保存圖片高清圖的方法,想要了解更多html5相關(guān)內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)查詢,扣丁學(xué)堂不僅有專業(yè)的老師和與時俱進(jìn)的課程體系,還有大量的html5視頻教程供學(xué)員觀看學(xué)習(xí),想要學(xué)習(xí)html5的小伙伴快選擇專業(yè)的html5在線學(xué)習(xí)機(jī)構(gòu)扣丁學(xué)堂學(xué)習(xí)吧??鄱W(xué)堂H5技術(shù)交流群:559883758。


關(guān)注微信公眾號獲取更多學(xué)習(xí)資料

【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】



查看更多關(guān)于“HTML5開發(fā)技術(shù)資訊”的相關(guān)文章>>


標(biāo)簽: 扣丁學(xué)堂告訴你html2canvas把div保存圖片高清圖的方法 HTML5培訓(xùn) HTML5視頻教程 HTML5在線課程 HTML5在線學(xué)習(xí) HTML5在線視頻 HTML5從入門到精通 html5基礎(chǔ)教程

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部