欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

html2canvas屬性和使用方法以及如何使用html2canvas將HTML內(nèi)容寫入Canvas生成圖片

 更新時間:2020年01月12日 15:47:04   作者:58384View  
為大家介紹一款JS截圖插件html2canvas.js, 它可以通過純JS對瀏覽器端經(jīng)行截屏,下面就為大家介紹一下html2canvas.js屬性和具體使用方法,并為大家提供了一個實例

如何使用JS截取HTML頁面為圖片呢,下面為大家介紹一款JS截圖插件html2canvas.js

html2canvas.js 能夠?qū)崿F(xiàn)在用戶瀏覽器端直接對整個或部分頁面進(jìn)行截屏。

html2canvas.js可以將當(dāng)頁面渲染成一個Canvas圖片,通過讀取DOM并將不同的樣式應(yīng)用到這些元素上實現(xiàn)。

它不需要來自服務(wù)器任何渲染,整張圖片都是在客戶端瀏覽器創(chuàng)建。當(dāng)

瀏覽器不支持Canvas時,將采用Flashcanvas或ExplorerCanvas技術(shù)代替實現(xiàn)。

以下瀏覽器能夠很好的支持該腳本:Firefox 3.5+, Google Chrome, Opera新的版本, IE9以上的瀏覽器。

基本語法

html2canvas(element, options);
html2canvas(document.body, {
 onrendered: function(canvas) {
  var url = canvas.toDataURL();//圖片地址
  document.body.appendChild(canvas);
 },
 width: 300,
 height: 300

或者使用ES6的promise

//兩個參數(shù):所需要截圖的元素id,截圖后要執(zhí)行的函數(shù), canvas為截圖后返回的最后一個canvas html2canvas(document.getElementById('id')).then(function(canvas) {document.body.appendChild(canvas);});

html2canvas基本參數(shù)說明

參數(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)透明
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--這個我也不知道是干嘛的

例子

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>html2canvas example</title>
  <script type="text/javascript" src="html2canvas.js"></script>
</head>
<script type="text/javascript">
function takeScreenshot() {
 console.log('test');
  html2canvas(document.getElementById('view'), {
    onrendered: function(canvas) {
      document.body.appendChild(canvas);
    },
   // width: 300,
   // height: 300
  });
}
</script>
<body>
  <div id="view" style="background:url(test.jpg) 50%; width: 700px; height: 500px;">
    <input type="button" value="截圖" onclick="takeScreenshot()">
  </div>
</body>

</html>

效果圖如下:

截圖效果如下:

最后附上html2canvas官網(wǎng)鏈接

官網(wǎng)

Github

相關(guān)文章

  • 詳細(xì)聊聊瀏覽器是如何看閉包的

    詳細(xì)聊聊瀏覽器是如何看閉包的

    閉包實質(zhì)上是函數(shù)作用域的副產(chǎn)物,下面這篇文章主要給大家介紹了關(guān)于瀏覽器是如何看閉包的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • 使用微信小程序開發(fā)彈出框應(yīng)用實例詳解

    使用微信小程序開發(fā)彈出框應(yīng)用實例詳解

    本文通過實例代碼給大家介紹了使用微信小程序開發(fā)彈出框功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-10-10
  • 在chrome中window.onload事件的一些問題

    在chrome中window.onload事件的一些問題

    在寫一些關(guān)于圖片操作的代碼的時候,一般都需要在圖片加載完成之后再執(zhí)行程序。然而在Chorme中(貌似Safari也是)對window.onload的理解與IE和FF有偏差。
    2010-03-03
  • Bootstrap CSS布局之圖像

    Bootstrap CSS布局之圖像

    這篇文章主要介為大家詳細(xì)紹了Bootstrap CSS布局之圖像的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • layui form表單提交后實現(xiàn)自動刷新

    layui form表單提交后實現(xiàn)自動刷新

    今天小編就為大家分享一篇layui form表單提交后實現(xiàn)自動刷新,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • 微信小程序?qū)崿F(xiàn)登錄頁云層漂浮的動畫效果

    微信小程序?qū)崿F(xiàn)登錄頁云層漂浮的動畫效果

    微信小程序目前的火熱程度相信不用多言,最近利用空余時間用小程序?qū)崿F(xiàn)了個動態(tài)的登錄頁效果,所以下面這篇文章主要給大家介紹了利用微信小程序?qū)崿F(xiàn)登錄頁云層漂浮動畫效果的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • JavaScript Dom實現(xiàn)輪播圖原理和實例

    JavaScript Dom實現(xiàn)輪播圖原理和實例

    這篇文章主要為大家詳細(xì)介紹了JavaScript Dom實現(xiàn)輪播圖原理和實例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • firefox TBODY 用js顯示和隱藏時出現(xiàn)錯位的解決方法

    firefox TBODY 用js顯示和隱藏時出現(xiàn)錯位的解決方法

    今天幫別人寫一個網(wǎng)頁,發(fā)現(xiàn):當(dāng)用javascript動態(tài)設(shè)置tr.style.display = "block"顯示某行時,使用IE瀏覽沒有問題,但使用firefox瀏覽時該行被移到了其它行的后面,很是詫異。
    2008-12-12
  • js回到頁面指定位置的三種方式

    js回到頁面指定位置的三種方式

    這篇文章主要介紹了js回到頁面指定位置的三種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • JavaScript實現(xiàn)向setTimeout執(zhí)行代碼傳遞參數(shù)的方法

    JavaScript實現(xiàn)向setTimeout執(zhí)行代碼傳遞參數(shù)的方法

    這篇文章主要介紹了JavaScript實現(xiàn)向setTimeout執(zhí)行代碼傳遞參數(shù)的方法,分析了向setTimeout傳遞參數(shù)的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04

最新評論