$( "#btnsave" ).click(function(){ html var copyDom = $( "#modalcontent" ); canvas var width = copyDom.offsetWidth; //dom寬 app var height = copyDom.offsetHeight; //dom高 dom var scale = 2; //放大倍數 url var canvas = document.createElement( 'canvas' ); spa canvas.width = width*scale; //canvas寬度 code canvas.height = height*scale; //canvas高度 htm var content = canvas.getContext( "2d" ); 圖片 content.scale(scale,scale); ci var rect = copyDom. get (0).getBoundingClientRect(); //獲取元素相對於視察的偏移量 content.translate(-rect.left,-rect.top); //設置context位置,值爲相對於視窗的偏移量負值,讓圖片復位 html2canvas(copyDom[0], { dpi: window.devicePixelRatio*2, scale:scale, canvas:canvas, width:width, heigth:height, }).then(function (canvas) { var url = canvas.toDataURL(); var triggerDownload = $( "<a>" ).attr( "href" , url).attr( "download" , "詳情.png" ).appendTo( "body" ); triggerDownload[0].click(); triggerDownload.remove(); }); }) |