使用html2canvas實作瀏覽器截圖,必須在伺服器環境下才能實現。本文主要介紹了使用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) //保存的图片格式转换方法 }); }
可用設定項
類型 | 預設值 | ##描述 | |
---|---|---|---|
boolean | false | Whether to allow cross-origin images to taint the canvas---允許跨網域 | |
string | #fff | Canvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景顏色,如果沒有設定預設白色此處被坑,我改為backgroundColor可用 | |
number | null | Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度設定 | |
false | Whether to render each letter seperately. Necessary if letter-spacing is used.---在設定了字間距的時候有用 | logging | |
false | Whether to log events in the console.---在console.log()中輸出訊息 | #proxy | |
undefined | Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't beges loaded.---代理地址 | taintTest | |
true | Whether to test each image if it taints the canvas before drawing them---是否在渲染前測試圖片 | timeout | |
0 | Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---圖片載入延遲,預設延遲為0,單位毫秒 | ##width | |
#null | Define the width of the canvas in pixels. If null, renders with full width of the window.---canvas寬度 | #useCORS | |
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);相關推薦:
以上是html2 canvas實作瀏覽器截圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

htmltagsareessentialforwebdevelopmentastheyandendenhancewebpages.1)semantictagsimproveaccessibilityandseo.2)semanteLayOut,語義和互動性。 3)poseriblesibilityandseoandseo.3)poseriblesoftagscanoftagscanoftagscanoptagscanoptimizeperefeneandimizeanDenSuroceRecRoscRoss-BrowserCrowserCercerComercompatibility。

一致的HTML編碼風格很重要,因為它提高了代碼的可讀性、可維護性和效率。 1)使用小寫標籤和屬性,2)保持一致的縮進,3)選擇並堅持使用單引號或雙引號,4)避免在項目中混合使用不同風格,5)利用自動化工具如Prettier或ESLint來確保風格的一致性。

在Bootstrap4中實現多項目輪播的解決方案在Bootstrap4中實現多項目輪播並不是一件簡單的事情。雖然Bootstrap...

如何實現鼠標滾動事件穿透效果?在我們瀏覽網頁時,經常會遇到一些特別的交互設計。比如在deepseek官網上,�...

無法直接通過CSS修改HTML視頻的默認播放控件樣式。 1.使用JavaScript創建自定義控件。 2.通過CSS美化這些控件。 3.考慮兼容性、用戶體驗和性能,使用庫如Video.js或Plyr可簡化過程。

在手機上使用原生select的潛在問題在開發移動端應用時,我們常常會遇到選擇框的需求。通常情況下,開發者傾...

在手機上使用原生select的弊端是什麼?在移動設備上開發應用時,選擇合適的UI組件是非常重要的。許多開發者�...

使用Three.js和Octree優化房間內第三人稱漫遊的碰撞處理在Three.js中使用Octree實現房間內的第三人稱漫遊並添加碰�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器