前言
最近遇到了個功能,要把表單轉化成圖片來保存,這讓我想到了一個插件HTML2CANVAS,這里給大家分享下它的用法和我使用的過程

html2canvas 能夠實現在用戶瀏覽器端直接對整個或部分頁面進行截屏。這個html2canvas腳本將當頁面渲染成一個canvas圖片,通過讀取DOM並將不同的樣式應用到這些元素上實現。
它不需要來自服務器任何渲染,整張圖片都是在客戶端瀏覽器創建。當瀏覽器不支持Canvas時,將采用Flashcanvas或ExplorerCanvas技術代替實現。
以下瀏覽器能夠很好的支持該腳本:Firefox 3.5+, Google Chrome, Opera新的版本, IE9以上的瀏覽器。
html2canvas可以通過獲取HTML的某個元素,然后生成Canvas,能讓用戶保存為圖片。 這個項目主要是生成canvas,那么我們如果需要生成圖片還需要將它轉化為圖片地址。
基本語法
html2canvas(element, options);
html2canvas(document.body, {
onrendered: function(canvas) {
var url = canvas.toDataURL();//圖片地址
document.body.appendChild(canvas);
},
width: 300,
height: 300
});
或者使用ES6的promise
//兩個參數:所需要截圖的元素id,截圖后要執行的函數, canvas為截圖后返回的最后一個canvas
html2canvas(document.getElementById('id')).then(function(canvas) {document.body.appendChild(canvas);});
可用參數
| 參數名稱 | 類型 | 默認值 | 描述 |
|---|---|---|---|
| allowTaint | boolean | false | Whether to allow cross-origin images to taint the canvas---允許跨域 |
| background | string | #fff | Canvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景顏色,如果沒有設定默認透明 |
| height | number | null | Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度設定 |
| letterRendering | boolean | false | Whether to render each letter seperately. Necessary if letter-spacing is used.---在設置了字間距的時候有用 |
| logging | boolean | false | Whether to log events in the console.---在console.log()中輸出信息 |
| proxy | string | undefined | Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.---代理地址 |
| taintTest | boolean | true | Whether to test each image if it taints the canvas before drawing them---是否在渲染前測試圖片 |
| timeout | number | 0 | Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---圖片加載延遲,默認延遲為0,單位毫秒 |
| width | number | null | Define the width of the canvas in pixels. If null, renders with full width of the window.---canvas寬度 |
| useCORS | boolean | false | Whether to attempt to load cross-origin images as CORS served, before reverting back to proxy--這個我也不知道是干嘛的 |
例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>html2canvas example</title>
<script type="text/javascript" src="html2canvas.js"></script>
</head>
<script type="text/javascript">
function takeScreenshot() {
console.log('test');
html2canvas(document.getElementById('view'), {
onrendered: function(canvas) {
document.body.appendChild(canvas);
},
// width: 300,
// height: 300
});
}
</script>
<body>
<div id="view" style="background:url(test.jpg) 50%; width: 700px; height: 500px;">
<input type="button" value="截圖" onclick="takeScreenshot()">
</div>
</body>
</html>
如何在vue+ts環境調用
前提條件
項目安裝html2canvas,jq npm install html2canvas npm install jquery
HTML部分
<div id="#printArea">需要打印的區域</div>
<div class="print">
<iframe id='iframe' style="display:none;"></iframe>
</div>
JS部分
頁面引入 html2canvas、jquery
import html2canvas from 'html2canvas';
import $ from 'jquery';
// 打印
private onPrint() {
const el: any = $('#printArea')[0];
const iframe = window.frames[0];
iframe.document.close();
html2canvas(el, {
scale: 1,
width: el.offsetWidth,
height: el.offsetHeight,
allowTaint: true,
useCORS: true,
}).then(function (canvas) {
const context: any = canvas.getContext('2d');
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
const src64: any = canvas.toDataURL();
const newImg: any = document.createElement('img');
newImg.crossOrigin = 'Anonymous';
newImg.src = src64;
iframe.document.write('<img src="' + newImg.src + '" />');
setTimeout(() => {
iframe.window.print();
});
});
}
效果圖

如果對您有所幫助,歡迎您點個關注,我會定時更新技術文檔,大家一起討論學習,一起進步。

