使用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; }支援的瀏覽器/*參數:* #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 typeimgData = 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, false, false, false, false, false, false, false, false, false, 0, null); save_link.dispatchEvent(event);}; //下載後的檔案名稱var filename = 'baidufe_' + (new Date()).getTime() + '.' + type;// downloadsaveFile(imgData,filename);案例
案例
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。