對html2canvas的研究

介紹

? ? ? ? ?該腳本允許您直接在用戶瀏覽器上截取網頁或部分網頁的“屏幕截圖”。屏幕截圖基于DOM,因此它可能不是真實表示的100%準確,因為它沒有制作實際的屏幕截圖,而是根據頁面上可用的信息構建屏幕截圖。

這個怎么運作

該腳本遍歷其加載的頁面的DOM。它收集有關那里所有元素的信息,然后用它來構建頁面的表示。換句話說,它實際上并不截取頁面的截圖,而是基于從DOM讀取的屬性構建它的表示。?

因此,它只能正確呈現它理解的屬性,這意味著有許多CSS屬性不起作用。有關支持的CSS屬性的完整列表,請查看?支持的功能頁面http://html2canvas.hertzen.com/features/

限制

腳本使用的所有圖像都需要位于同源下,以便能夠在沒有代理幫助的情況下讀取它們。同樣,如果您canvas?在頁面上有其他元素,這些元素已被污染了跨源內容,它們將變得臟,并且不再被html2canvas讀取。

該腳本不會呈現插件內容,如Flash或Java小程序。

支持的瀏覽器版本:

  • Firefox 3.5+
  • Google Chrome
  • Opera 12+
  • IE9+
  • Edge
  • Safari 6+
npm install html2canvas
import html2canvas from 'html2canvas';
html2canvas(document.body).then(function(canvas) { document.body.appendChild(canvas); });
所有可用的配置選項:
NameDefaultDescription
allowTaintfalseWhether to allow cross-origin images to taint the canvas
backgroundColor#ffffffCanvas background color, if none is specified in DOM. Set?null?for transparent
canvasnullExisting?canvas?element to use as a base for drawing on
foreignObjectRenderingfalseWhether to use ForeignObject rendering if the browser supports it
imageTimeout15000Timeout for loading an image (in milliseconds). Set to?0?to disable timeout.
ignoreElements(element) => falsePredicate function which removes the matching elements from the render.
loggingtrueEnable logging for debug purposes
onclonenullCallback function which is called when the Document has been cloned for rendering, can be used to modify the contents that will be rendered without affecting the original source document.
proxynullUrl to the?proxy?which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.
removeContainertrueWhether to cleanup the cloned DOM elements html2canvas creates temporarily
scalewindow.devicePixelRatioThe scale to use for rendering. Defaults to the browsers device pixel ratio.
useCORSfalseWhether to attempt to load images from a server using CORS
widthElement?widthThe width of the?canvas
heightElement?heightThe height of the?canvas
xElement?x-offsetCrop canvas x-coordinate
yElement?y-offsetCrop canvas y-coordinate
scrollXElement?scrollXThe x-scroll position to used when rendering element, (for example if the Element uses?position: fixed)
scrollYElement?scrollYThe y-scroll position to used when rendering element, (for example if the Element uses?position: fixed)
windowWidthWindow.innerWidthWindow width to use when rendering?Element, which may affect things like Media queries
windowHeightWindow.innerHeightWindow height to use when rendering?Element, which may affect things like Media queries
所有受支持的CSS屬性和值的列表:
  • background

    • background-clip (Does not support?text)
    • background-color
    • background-image

      • url()
      • linear-gradient()
      • radial-gradient()
    • background-origin
    • background-position
    • background-size
  • border

    • border-color
    • border-radius
    • border-style (Only supports?solid)
    • border-width
  • bottom

  • box-sizing

  • content

  • color

  • display

  • flex

  • float

  • font

    • font-family
    • font-size
    • font-style
    • font-variant
    • font-weight
  • height

  • left

  • letter-spacing

  • line-break

  • list-style

    • list-style-image
    • list-style-position
    • list-style-type
  • margin

  • max-height

  • max-width

  • min-height

  • min-width

  • opacity

  • overflow

  • overflow-wrap

  • padding

  • position

  • right

  • text-align

  • text-decoration

    • text-decoration-color
    • text-decoration-line
    • text-decoration-style (Only supports?solid)
  • text-shadow

  • text-transform

  • top

  • transform (Limited support)

  • visibility

  • white-space

  • width

  • word-break

  • word-spacing

  • word-wrap

  • z-index

Unsupported CSS properties

These CSS properties are?NOT?currently supported

  • background-blend-mode
  • border-image
  • box-decoration-break
  • box-shadow
  • filter
  • font-variant-ligatures
  • mix-blend-mode
  • object-fit
  • repeating-linear-gradient()
  • writing-mode
  • zoom

html2canvas不會解決您的瀏覽器設置的內容政策限制。繪制位于當前頁面原點之外的圖像會污染它們所繪制的畫布。如果畫布受到污染,則無法再讀取。如果要加載位于頁面原點之外的圖像,可以使用代理加載圖像。

  • node.js

為什么不渲染我的圖像?

html2canvas不會解決您的瀏覽器設置的內容政策限制。繪制位于當前頁面原點之外的圖像會污染它們所繪制的畫布。如果畫布受到污染,則無法再讀取。因此,html2canvas實現了一些方法來檢查圖像在應用之前是否會污染畫布。如果您將allowTaint選項設置為false,則不會繪制圖像。

如果要加載位于頁面原點之外的圖像,可以使用代理加載圖像。

為什么生產的截屏是空的或者是切斷了一半?

await html2canvas(element, {windowWidth: element.scrollWidth,windowHeight: element.scrollHeight
})

Chrome

Maximum height/width: 32,767 pixels Maximum area: 268,435,456 pixels (e.g., 16,384 x 16,384)

Firefox

Maximum height/width: 32,767 pixels Maximum area: 472,907,776 pixels (e.g., 22,528 x 20,992)

Internet Explorer

Maximum height/width: 8,192 pixels Maximum area: N/A

iOS

The maximum size for a canvas element is 3 megapixels for devices with less than 256 MB RAM and 5 megapixels for devices with greater or equal than 256 MB RAM

?

為什么CSS屬性X沒有正確呈現或僅部分呈現?

由于每個CSS屬性都需要手動編碼才能正確呈現,因此html2canvas?永遠不會有完整的CSS支持。該庫試圖在可能的范圍內支持最常用的CSS屬性。如果某些CSS屬性缺失或不完整,并且您認為它應該是庫的一部分,請為其創建測試用例并為其創建新問題。

如何讓html2canvas在瀏覽器擴展中工作?

您不應在瀏覽器擴展中使用html2canvas。大多數瀏覽器都支持從擴展中的選項卡捕獲屏幕截圖。Chrome和?Firefox的相關信息。






























?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?



?

?

轉載于:https://www.cnblogs.com/zhouyideboke/p/11315289.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247229.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247229.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247229.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[Vue warn]: You are using the runtime-only build of Vue 牽扯到Vue runtime-compiler與runtime-only區別

[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build. 1. Vue的編譯渲染過程 template --> ast --> render函數 -…

親歷2013年TED大會:全球最潮靈感大會

本文轉自:http://mooc.guokr.com/opinion/436837/ 本文由《外灘畫報》供稿 文/華琪(發自美國) 編輯/吳慧雯 什么是TED的世界?在這里,你可以輕易發現各種名人出沒的痕跡,和各個領域里最具遠見卓識和創造…

Java生鮮電商平臺-電商會員體系系統的架構設計與源碼解析

Java生鮮電商平臺-電商會員體系系統的架構設計與源碼解析 說明:Java生鮮電商平臺中會員體系作為電商平臺的基礎設施,重要性不容忽視。我去年整理過生鮮電商中的會員系統,但是比較粗,現在做一個最好的整理架構. 設計電商會員體系需…

為什么要上大學?

為了讓自己成為更有意思的人。 (文/美國圣母大學哲學教授 Gary Gutting)再不久,千千萬萬的大學生又將走完一個學期。他們中的很多人以及他們的家人,可能為剛剛過去的幾個月或是幾年投入了相當可觀的時間、努力以及金錢…

React AntD 表格查看修改時默認選中幾行數據

hook定義selectedRowKeys const [selectedRowKeys, setSelectedRowKeys] useState([]); const [selectedRowsState, setSelectedRows] useState([]); 初始化時利用setSelectedRowKeys給selectedRowKeys塞值,時行數據的rowKey的數組。 設置table屬性rowSelection <Table…

python面向對象三大特性、類的約束、print帶顏色輸出及super補充

面向對象三大特性、類的約束、print帶顏色輸出及super補充 簡述&#xff1a; python面向對象的三大特性&#xff1a; 1.繼承是一種創建新類的方式&#xff0c;在python中&#xff0c;新建的類可以繼承一個或多個父類&#xff0c;父類又可稱為基類或超類&#xff0c;新建的類稱為…

dayjs也可回顯AntD DatePicker的值

遇到的問題&#xff1a;react 使用AntD 表單里有多個RangePicker,查看修改時要回顯值。 antd的DatePicker需要的是moment對象。但是項目里引的是dayjs庫 解決方式&#xff1a; 方式一:直接多引moment.js庫&#xff0c;字符串轉moment對象 moment(2022-02) 方式二:不甘心引兩…

打造“神犇”是教育的未來嗎?

這年頭&#xff0c;品學兼優、身懷特長的“神犇”&#xff0c;拼的不僅是天賦異稟和后天努力&#xff0c;更是身后爹媽的錢包&#xff0c;而本該實現社會公平的教育&#xff0c;反而加速和凝固了社會的不公。 高等教育的終極目標真的是造就學業超人嗎&#xff1f;《紐約時報》刊…

洛谷 P3243 【[HNOI2015]菜肴制作】

第一眼看到這題&#xff0c;頓時懵逼&#xff0c;一個 \(SB\) 拓撲序竟然是黑題&#xff0c;當場笑噴。 \(Of\) \(course\)&#xff0c;這題我是用堆做的。&#xff08;其實是優先隊列&#xff0c;手寫堆這么垃圾我怎么可能會用呢&#xff09; \((1)\) 首先建圖。如果 \(x\) 需…

AntD 官網樣例 InputRef報錯原因

在官網可編輯表格typescript樣例里 const inputRef useRef<InputRef>(null); InputRef項目報錯原因是ant design的版本問題! antd 4.19版本重寫了input 可通過InputRef來使用input組件的ref

電路原理圖檢查的十大步驟詳解

最近一直在做嵌入式系統&#xff0c;畫原理圖。最后&#xff0c;為了保證原理圖準確無誤&#xff0c;檢查原理圖花費我近兩周的時間&#xff0c;在此&#xff0c;把我在檢查原理圖方面的心得體會總結在此&#xff0c;供大家參考&#xff0c;說得不對的地方歡迎大家指出。 往往我…

亞倫?斯沃茨:怎樣有效利用時間

編者按&#xff1a;今天是著名黑客亞倫?斯沃茨&#xff08;Aaron Swartz&#xff09;頭七的日子。斯沃茨14歲就參與創造RSS 1.0規格的制定&#xff0c;曾在斯坦福大學就讀了一年&#xff0c;是社交新聞網站Reddit的三位創始人之一……斯沃茨自殺時才年僅26歲。這26歲的短暫生命…

AntD 可編輯行表格

本地數據代碼模板自用,官網例子改改 // 編輯行的自定義表格 import React, { useState } from "react"; import {Table,Input,InputNumber,Popconfirm,Form,Typography,Divider, } from "antd";interface Item {key: string;name: string;age: number;add…

SharePoint 2013 - System Features

1. Embed Information & Convert to PDF 功能&#xff0c;在文檔的preview界面&#xff08;hover panel&#xff09;; 2. Share功能可以選擇是否發送郵件 -- Done 4. Shredded Storage, 將文檔的內容和每次的更改分開存儲&#xff0c;每次只存儲更改的內容&#xff0c;而不…

三心二意,助你好運?

經驗說&#xff1a;做事要專心致志。 實驗說&#xff1a;專心致志常常讓人缺少一雙發現的眼睛。 專心致志從來都被當做一個美德來歌頌。從來我們就認為要想成為偉大的人就必須要像牛頓老師那樣把鐘當成吃的放到鍋里煮才行&#xff0c;至少至少也得有能在集市上看書的本事。否則…

React Antd Upload自定義上傳customRequest

單獨的上傳圖片接口要傳參,action方式不太適合,需要使用自定義上傳customRequest覆蓋 公司代碼不可弄,就發一個可用的demo例子 import React, { useState } from "react"; import { render } from "react-dom"; import "antd/dist/antd.css"; i…

HDU 4109 Instrction Arrangement

題目鏈接&#xff1a;https://vjudge.net/problem/HDU-4109 題目大意 有 N 個指令&#xff0c;標號從 0 ~ N - 1&#xff0c;和 M 個指令間的先后關系&#xff0c;每個關系都有一個權值 w&#xff0c;表示后一個指令在前一個指令開始時間之后 w 納秒才開始執行。現在要并發執行…

養成一個習慣需要多久?

有人說養成一個習慣需要21天&#xff0c;有人說30天……養成習慣到底需要多少天&#xff1f; _hzw 2012-11-09 18:04又快到年末了。 年初時&#xff0c;你曾許諾要養成一些好習慣&#xff0c;比如要常運動、多讀書、少微博。有人說說養成一個習慣只需要堅持21天&#xff1b;21天…

vue3 eslint The template root requires exactly one element問題

vue項目中手動引入eslint npm install eslint --save-dev eslint初始化 npx eslint --init 運行時npm run lint 簡單語法問題--fix會自動修復 但是vue3項目出現只能有一個根節點的問題 原因在于 eslintrc.js里extends中plugin:vue/essential的問題 看eslint-plugin-vue官網…

你所知道的學習方法,都是錯的!

你所知道的學習方法&#xff0c;都是錯的&#xff01; 上課的時候記筆記&#xff1f;哪門功課不行&#xff0c;就集中精力專項突擊&#xff1f;自習的時候不要晃&#xff0c;選好一個地兒安安穩穩地待那兒學習&#xff1f;你還在這樣學習嗎&#xff1f;不要被騙了&#xff1a;這…