本篇文章給大家分享的是有關使用html2canvas怎么截圖瀏覽器,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
作用
html2canvas可以通過純JS對瀏覽器端經行截屏,但截圖的精確度還有待提高,部分css不可識別,所以在canvas中不能完美呈現原畫面樣式
/*多行溢出省略就不行,只能超出隱藏了*/
.book_inf{
position: relative;
overflow : hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}支持的瀏覽器
Firefox 3.5+
Google Chrome
Opera 12+
IE9+
Safari 6+
基本語法
/*參數:
* #screenshots 所需要截圖的元素id,截圖后要執行的函數,
* backgroundColor 配置項背景色
* canvas為截圖后返回的最后一個canvas
*/
function screenshotsImg(){
html2canvas(document.querySelector("#screenshots"),{
backgroundColor: 'transparent',// 設置背景透明
}).then(canvas => {
canvasTurnImg(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的背景顏色,如果沒有設定默認白色此處被坑,我改為backgroundColor可用 |
| 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--跨域代理 |
設置圖片格式
1、從canvas中直接提取圖片元數據
// 圖片導出為 png 格式 var type = 'png'; var imgData = canvas.toDataURL(type);
2、將mime-type改為image/octet-stream,強制讓瀏覽器直接download
/**
* 獲取mimeType
* @param {String} type the old mime-type
* @return the new mime-type
*/
var _fixType = function(type) {
type = type.toLowerCase().replace(/jpg/i, 'jpeg');
var r = type.match(/png|jpeg|bmp|gif/)[0];
return 'image/' + r;
};
// 加工image data,替換mime type
imgData = imgData.replace(_fixType(type),'image/octet-stream');3、圖片download到本地
/**
* 在本地進行文件保存
* @param {String} data 要保存到本地的圖片數據
* @param {String} filename 文件名
*/
var saveFile = function(data, filename){
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename;
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
};
// 下載后的文件名
var filename = 'baidufe_' + (new Date()).getTime() + '.' + type;
// download
saveFile(imgData,filename);以上就是使用html2canvas怎么截圖瀏覽器,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。