麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > HTML > 正文

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

2024-08-26 00:20:22
字體:
來源:轉載
供稿:網友

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

案例

案例

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

 

注:相關教程知識閱讀請移步到HTML教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 7777视频| 国产精品美女久久久免费 | 毛片av网址 | 国产精品亚洲综合一区二区三区 | 亚洲成年人免费网站 | 一区二区三区在线视频观看58 | 天堂成人一区二区三区 | 91视频站 | 国产资源在线观看 | 亚州综合 | 中日无线码1区 | 国内精品久久久久久久久久 | 免费网站看毛片 | 久久久一区二区三区四区 | 午夜视频在线观看免费视频 | 涩涩屋av| 国产九色在线播放九色 | 成人一级毛片 | 国产毛片网 | 久草在线观看资源 | 免费久久久久久久 | 欧洲狠狠鲁| 欧美日性 | 精品在线免费播放 | 国内精品一级毛片免费看 | 国产三级在线观看a | 久久影片| 国产乱色精品成人免费视频 | 欧美激情 在线播放 | 中文字幕电影免费播放 | 国产精品亚洲一区二区三区在线观看 | 国产欧美亚洲精品a | 色99久久| 粉嫩蜜桃麻豆免费大片 | 热99在线视频 | 亚洲成人午夜精品 | caoporn国产一区二区 | 久久草在线视频 | 99国产精品欲a | 一区二区三区欧美在线观看 | 日韩.www|