使用html2canvas實現(xiàn)瀏覽器截圖的示例代碼
發(fā)布時間:2018-01-26 14:57:36 作者:月影
我要評論

這篇文章主要介紹了使用html2canvas實現(xiàn)瀏覽器截圖的示例代碼的相關(guān)資料,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
使用html2canvas實現(xiàn)瀏覽器截圖,必須在服務(wù)器環(huán)境下才能實現(xiàn)。
作用
html2canvas可以通過純JS對瀏覽器端經(jīng)行截屏,但截圖的精確度還有待提高,部分css不可識別,所以在canvas中不能完美呈現(xiàn)原畫面樣式
/*多行溢出省略就不行,只能超出隱藏了*/ .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+
基本語法
/*參數(shù): * #screenshots 所需要截圖的元素id,截圖后要執(zhí)行的函數(shù), * backgroundColor 配置項背景色 * canvas為截圖后返回的最后一個canvas */ function screenshotsImg(){ html2canvas(document.querySelector("#screenshots"),{ backgroundColor: 'transparent',// 設(shè)置背景透明 }).then(canvas => { canvasTurnImg(canvas) //保存的圖片格式轉(zhuǎn)換方法 }); }
可用配置項
參數(shù)名稱 | 類型 | 默認(rèn)值 | 描述 |
---|---|---|---|
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的背景顏色,如果沒有設(shè)定默認(rèn)白色此處被坑,我改為backgroundColor可用 |
height | number | null | Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度設(shè)定 |
letterRendering | boolean | false | Whether to render each letter seperately. Necessary if letter-spacing is used.---在設(shè)置了字間距的時候有用 |
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.---圖片加載延遲,默認(rèn)延遲為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--跨域代理 |
設(shè)置圖片格式
1、從canvas中直接提取圖片元數(shù)據(jù)
// 圖片導(dǎo)出為 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 要保存到本地的圖片數(shù)據(jù) * @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);
案例
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
html5使用html2canvas實現(xiàn)瀏覽器截圖的示例
這篇文章主要介紹了html5使用html2canvas實現(xiàn)瀏覽器截圖的示例,非常具有實用價值,需要的朋友可以參考下2017-08-31html 基于 canvas 實現(xiàn)的一個截圖小demo
本篇文章主要介紹了html 基于 canvas 實現(xiàn)的一個截圖小demo,具有一定的參考價值,有興趣的可以了解一下2017-08-07- 本篇文章主要介紹了canvas與html5實現(xiàn)視頻截圖功能示例,非常具有實用價值,需要的朋友可以參考下。2016-12-15
Html5新特性用canvas標(biāo)簽畫多條直線附效果截圖
這篇文章主要介紹了Html5新特性用canvas標(biāo)簽畫多條直線的具體實現(xiàn),并附效果截圖,感興趣的朋友可以參考下2014-06-30- 在簡單的矩形不能滿足需求的情況下,可以同本例提供的方法來繪制復(fù)雜的形狀或路徑。下面為大家介紹下HTML5如何在canvas中繪制復(fù)雜形狀,需要的朋友可以參考下2014-06-23
HTML5 Canvas實現(xiàn)平移/放縮/旋轉(zhuǎn)deom示例(附截圖)
HTML5 Canvas中提供了實現(xiàn)圖形API,通過它可以簡單的實現(xiàn)平移,旋轉(zhuǎn),放縮等等,下面與大家分享下平移,旋轉(zhuǎn),放縮的具體實現(xiàn)及參照圖,感興趣的朋友可以參考下哈,希望對2013-07-04video結(jié)合canvas實現(xiàn)視頻在線截圖功能
這篇文章主要介紹了video結(jié)合canvas實現(xiàn)視頻在線截圖功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-25