日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

html2canvas将HTML内容写入Canvas生成图片

發(fā)布時(shí)間:2023/12/19 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 html2canvas将HTML内容写入Canvas生成图片 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

html2canvas?能夠?qū)崿F(xiàn)在用戶瀏覽器端直接對(duì)整個(gè)或部分頁(yè)面進(jìn)行截屏。這個(gè)html2canvas腳本將當(dāng)頁(yè)面渲染成一個(gè)canvas圖片,通過(guò)讀取DOM并將不同的樣式應(yīng)用到這些元素上實(shí)現(xiàn)。

它不需要來(lái)自服務(wù)器任何渲染,整張圖片都是在客戶端瀏覽器創(chuàng)建。當(dāng)瀏覽器不支持Canvas時(shí),將采用Flashcanvas或ExplorerCanvas技術(shù)代替實(shí)現(xiàn)。以下瀏覽器能夠很好的支持該腳本:Firefox 3.5+, Google Chrome, Opera新的版本, IE9以上的瀏覽器。

html2canvas可以通過(guò)獲取HTML的某個(gè)元素,然后生成Canvas,能讓用戶保存為圖片。

這個(gè)項(xiàng)目主要是生成canvas,那么我們?nèi)绻枰蓤D片還需要將它轉(zhuǎn)化為圖片地址。以下內(nèi)容參考這篇:https://www.jianshu.com/p/6a07e974a7e8

基本語(yǔ)法

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

或者使用ES6的promise

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

可用參數(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è)置了字間距的時(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---是否在渲染前測(cè)試圖片
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--這個(gè)我也不知道是干嘛的

例子

<!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">
        <input type="button" value="截圖" onclick="takeScreenshot()">
    </div>
</body>

</html>

效果圖


Paste_Image.png


  • 截圖后


Paste_Image.png


html2canvas官網(wǎng)

官方網(wǎng)址 項(xiàng)目下載

總結(jié)

以上是生活随笔為你收集整理的html2canvas将HTML内容写入Canvas生成图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。