基於html2canvas實現網頁儲存為圖片為縮圖
阿新 • • 發佈:2018-12-19
場景:微信長按網頁儲存為截圖
。
步驟:
1.引入html2canvas.js 可以在github上下載
http://html2canvas.hertzen.com/
2.程式碼
var cntElem = $("#filesEnd")[0]; var opts = { scale: scale, // 新增的scale 引數 canvas: canvas, //自定義 canvas // logging: true, //日誌開關,便於檢視html2canvas的內部執行流程 width: width, //dom 原始寬度 height: height, useCORS: true // 【重要】開啟跨域配置 }; html2canvas(shareContent, opts).then(function (canvas) { var htmlpic = canvas.toDataURL("image/jpeg",0.5); $("#thumbnailImg").attr('src',htmlpic); });
注意:canvas.toDataURL.需要在伺服器端執行,才可以正常使用