首页 > 其他分享 >html2canvas插件使用小结

html2canvas插件使用小结

时间:2023-04-17 17:59:18浏览次数:38  
标签:.--- 插件 Whether canvas html2canvas images document 小结

简介

html2canvas 能够实现在用户浏览器端直接对整个或部分页面进行截屏。这个html2canvas脚本将当页面渲染成一个canvas图片,通过读取DOM并将不同的样式应用到这些元素上实现。

它不需要来自服务器任何渲染,整张图片都是在客户端浏览器创建。当浏览器不支持Canvas时,将采用Flashcanvas或ExplorerCanvas技术代替实现。

以下浏览器能够很好的支持该脚本:Firefox 3.5+, Google Chrome, Opera新的版本, IE9以上的浏览器。

html2canvas可以通过获取HTML的某个元素,然后生成Canvas,能让用户保存为图片。 这个项目主要是生成canvas,那么我们如果需要生成图片还需要将它转化为图片地址。

基本语法

html2canvas(element, options)

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

或者使用ES6的promise

 //两个参数:所需要截图的元素id,截图后要执行的函数, canvas为截图后返回的最后一个canvas
 html2canvas(document.getElementById('id')).then(function(canvas) {
   document.body.appendChild(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的背景颜色,如果没有设定默认透明
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--是否尝试使用CORS从服务器加载图像

例子:

<!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>

标签:.---,插件,Whether,canvas,html2canvas,images,document,小结
From: https://www.cnblogs.com/yyshow/p/17326615.html

相关文章

  • 功能不够用?使用C++编写通达信插件及接入Python(二)
    参考:https://zhuanlan.zhihu.com/p/613157262一、准备工作(参考上一篇)安装VS2019 安装pycharm下载 http://help.tdx.com.cn/book.asp《通达信DLL函数编程规范.rar》二、下载python3.x的32位版本,http://www.python.org,随便找个32位版就行了。我准备下载Windowsembeddabl......
  • jQuery Templates模板插件
    jQuery团队提供了一个模板插件,但非常可惜的是jquery团队放弃对其维护,我们对其方法和语法进行简单简绍源码官方的网址:http://api.jquery.com/category/plugins/templates文档的网址:http://api.jquery.com/category/plugins/templates使用该插件必须先引入对应js //依赖jquery<sc......
  • 沉浸式翻译 插件推荐 同时显示中文和英文
    安装https://immersive-translate.owenyoung.com/installation特殊浏览器用油猴安装。我用的双核浏览器,内核比较老,就只能用油猴的方式装了,非常好用。......
  • [技术讨论]软件/插件/库的授权许可讨论
    引入对话这里是一个对话,让我想到了一个需要讨论的问题:老迈村长11:26:29 海浪 11:20:31作为开发,是作为个人来开发一个软件,但是用到里面的一个库,这个库针对个人和公司是不一样的价格那么如果是我做的这个软件用到了这个库,提供给公司使用,那么这个lincense是公司的价格还是个人的价......
  • SQL Pretty Printer(V4.0.0)推荐插件
    SQLPrettyPrinter(V4.0.0)推荐插件安装在SQLServer中我们经常需要编写各种SQL脚本,例如存储过程和函数等,由于在编写过程中,经常会进行调整,格式很乱,可读性很差。对于有强迫症的人来说,看这样的代码非常痛苦,必须要手动对代码进行格式化,如果代码脚本比较多,这个事情也是非常费时费力的。......
  • 星级评分插件
    网上看到的一个星级评分插件,觉得写的非常不错,对其原理进行分析。从代码来看最主要的是使用css样式的background-position属性background-position-20-20表示背景图向左移动20px、向上移动20pxbackground-position2020表示背景图向右移动20px、向下移动20px<!DOCTYPEhtmlPUBLI......
  • imagesLoaded插件
    imagesLoaded插件是一个在图片加载成功后才做一系列操作官方网站:https://github.com/desandro/imagesloaded/使用语法:$(selector).imagesLoaded([callback]); ImagesLoaded可以呼吁一个元素中的图像,图像直接,或两者的结合。selector选择器支持:1.直接为图片2.内嵌图片3.两......
  • struts2整合convention插件
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><consta......
  • gitee github 左侧栏树形显示插件 Octotree codetree 浏览器插件
    起因看到一位仁兄用gitee做仓库https://gitee.com/zhengqingya/java-developer-document然后左侧栏挺方便(抖音视频)下载chrome扩展市场搜octotree用于githubcodetree用于gitee双核浏览器扩展市场搜octotree用于githubgitcodetree用于gitee......
  • MySQL McAfee审计插件Audit Plugin安装
     MySQLMcAfee审计插件AuditPlugin安装 官网下载:https://github.com/trellix-enterprise/mysql-audit/releases官方文档:https://github.com/trellix-enterprise/mysql-audit/wiki防爬虫:https://www.cnblogs.com/PiscesCanon/p/17324406.html  注意要对应你的数据库软......