国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > HTML5 > 正文

使用html2canvas實現瀏覽器截圖的示例代碼

2024-09-05 07:19:28
字體:
來源:轉載
供稿:網友

使用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;        }

支持的瀏覽器

  1. Firefox 3.5+
  2. Google Chrome
  3. Opera 12+
  4. IE9+
  5. 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 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, 0, null);    save_link.dispatchEvent(event);};   // 下載后的文件名var filename = 'baidufe_' + (new Date()).getTime() + '.' + type;// downloadsaveFile(imgData,filename);

案例

案例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 许昌市| 怀宁县| 福海县| 永平县| 嘉善县| 九江县| 左云县| 辽阳县| 安康市| 潼关县| 文安县| 阜康市| 大兴区| 温宿县| 双辽市| 额尔古纳市| 金乡县| 广汉市| 壤塘县| 邵阳市| 高青县| 连山| 浦县| 城步| 韶山市| 庆安县| 海口市| 手游| 通榆县| 红原县| 贺州市| 永平县| 新龙县| 茂名市| 大冶市| 右玉县| 会宁县| 阿拉善右旗| 天津市| 健康| 霍山县|