//canvas-> img function convert2canvas() { var el = document.getElementById("dayimg");//要截圖的div var saveImg = document.getElementById("saveImg"); var canvas = document.createElement("canvas"); var scale = window.devicePixelRatio;//獲取設備的顯示參數 var ctx = canvas.getContext("2d") var rect = el.getBoundingClientRect(); //獲取元素相對於視察的偏移量 var w = el.offsetWidth; var h = w/0.69; console.log(w) canvas.width = w * scale; canvas.height = h * scale; canvas.style.width = w; canvas.style.height = h; ctx.scale(scale, scale); ctx.translate(-rect.left, -rect.top); //設置context位置,值爲相對於視窗的偏移量負值,讓圖片復位 html2canvas(el, { scale : scale, canvas : canvas, width : w, height : h, logging : false, useCORS : true }).then(function(canvas) { var dataUrl = canvas.toDataURL("jpeg"); saveImg.src = dataUrl; }); $(".dayimg").hide() }