最近工作中遇到的需求,將div轉成圖片並保存。
1、准備需要用到的js插件jquery-1.8.2.js,html2canvas.min.js(將div轉換為canvas),bluebird.js(用戶IE支持ES6 Promise特性)
2、頁面demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>div to img demo</title> <style type="text/css"> .content{ display: block; position: relative; width: 300px; height: 300px; background-color: #E6B246 } </style> </head> <body> <div class="content" id="imgDiv"> <div>測試</div> </div> <button id="btn">保存為圖片</button> </body> </html>
3、遇到的問題
問題1:生成的圖片模糊
解決方案:將canvas屬性放大兩倍,生成的時候再變回原來的1倍;
問題2:IE瀏覽器不支持ES6新特性,無法使用html2canvas插件生成畫布
解決方案:引入bluebird.js,只需引入即可;
問題3:在執行保存時,如果直接使用html2canvas插件提供的,將生成的畫布直接轉換為base64的方法,將base64直接放到a標簽的href屬性中進行下載,當生成圖片內容過多時,base64長度將超出a標簽href長度限制,無法下載。
解決方案:將base64轉換為Blob流
問題4:有些瀏覽器,比如火狐,不支持a標簽直接下載
解決方案:還是使用Blob流下載
最終代碼:
<script type="text/javascript"> $(document).ready(function(){ // canvas生成圖片 $("#btn").on("click", function () { var getPixelRatio = function (context) { // 獲取設備的PixelRatio var backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 0.5; return (window.devicePixelRatio || 0.5) / backingStore; }; //生成的圖片名稱 var imgName = "cs.jpg"; var shareContent = document.getElementById("imgDiv"); var width = shareContent.offsetWidth; var height = shareContent.offsetHeight; var canvas = document.createElement("canvas"); var context = canvas.getContext('2d'); var scale = getPixelRatio(context); //將canvas的容器擴大PixelRatio倍,再將畫布縮放,將圖像放大PixelRatio倍。 canvas.width = width * scale; canvas.height = height * scale; canvas.style.width = width + 'px'; canvas.style.height = height + 'px'; context.scale(scale, scale); var opts = { scale: scale, canvas: canvas, width: width, height: height, dpi: window.devicePixelRatio }; html2canvas(shareContent, opts).then(function (canvas) { context.imageSmoothingEnabled = false; context.webkitImageSmoothingEnabled = false; context.msImageSmoothingEnabled = false; context.imageSmoothingEnabled = false; var dataUrl = canvas.toDataURL('image/jpeg', 1.0); dataURIToBlob(imgName, dataUrl, callback); }); }); }) // edited from https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfill var dataURIToBlob = function (imgName, dataURI, callback) { var binStr = atob(dataURI.split(',')[1]), len = binStr.length, arr = new Uint8Array(len); for (var i = 0; i < len; i++) { arr[i] = binStr.charCodeAt(i); } callback(imgName, new Blob([arr])); } var callback = function (imgName, blob) { var triggerDownload = $("<a>").attr("href", URL.createObjectURL(blob)).attr("download", imgName).appendTo("body").on("click", function () { if (navigator.msSaveBlob) { return navigator.msSaveBlob(blob, imgName); } }); triggerDownload[0].click(); triggerDownload.remove(); }; </script>
demo下載:div_to_img_demo
=============================分割線:20190928更新==============================
前兩天@AJ灬 老哥在使用過程中發現,在IE11下,導出圖片,table的邊框不見了,其他瀏覽器可以。
經過驗證,問題出在border上,如果border是放在HTML標簽上設置的話,在IE11下,導出圖片,邊框就會消失。需要將border放在CSS里面設置,導出就沒問題。
錯誤示范:
正確示范: