html2canvas html截图插件
以下我总结了一些注意事项,在代码中注释了,仅供参考。
html2canvas.js点击
付:完整使用的demo ,如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<title>html2Canvas demo</title>
<script>
document.documentElement.style.fontSize = window.screen.width / 7.5 + 'px';
</script>
<style>
body,
html,
div,
p,
ul,
li,
a,
img,
span,
button,
header,
footer,
section {
padding: 0;
margin: 0;
}
*, :before, :after {
-webkit-tap-highlight-color: transparent;
-webkit-user-select: none;
outline: none;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
::-webkit-scrollbar {
width: 0;
opacity: 0;
}
button{
font-family: simsun,"microsoft yahei", arial, "Helvetica Neue", Helvetica, STHeiTi, sans-serif;
}
body {
font-family: "microsoft yahei", arial, "Helvetica Neue", Helvetica, STHeiTi, sans-serif;
color: #000;
background-color: #f5f5f5;
-webkit-overflow-scrolling: touch;
}
.share-container {
padding-top: 0.72rem;
width: 2.35rem;
margin: 0 auto;
}
.share-content {
padding-top: 0.72rem;
height:3rem;
background-color: blue;
border-radius: 5px;
width: 100%;
}
.text{
font-size: 0.36rem;
color: #f2f2f2;
}
.btn-share {
width: 64%;
height: 0.89rem;
background-color: #3baaff;
border-radius: 0.89rem;
border: 1px solid #3baaff;
color: white;
font-size: 0.36rem;
margin: 0.75rem 0 0.67rem;
}
.btn-share:active{
background-color: #1b96c8;
}
</style>
</head>
<body>
<section class="main-container">
<header class="share-container" id="shareContainer">
<div class="share-content" id="shareContent">
<div class="text">
<p>文字,图片等内容</p>
</div>
</div>
</header>
<footer class="footer-center">
<button class="btn-share" id="btnShare">截 图</button>
</footer>
</section>
<script src="js/html2canvas.js"></script>
<script>
//定义查找元素方法
function $(selector) {
return document.querySelector(selector);
}
var main = {
init:function(){
main.setListener();
},
//设置监听事件
setListener:function(){
var btnShare = document.getElementById("btnShare");
btnShare.onclick = function(){
main.html2Canvas();
}
},
//获取像素密度
getPixelRatio:function(context){
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
return (window.devicePixelRatio || 1) / backingStore;
},
//绘制dom 元素,生成截图canvas
html2Canvas: function () {
var shareContent = $("#shareContent");// 需要绘制的部分的 (原生)dom 对象 ,注意容器的宽度不要使用百分比,使用固定宽度,避免缩放问题
var width = shareContent.offsetWidth; // 获取(原生)dom 宽度
var height = shareContent.offsetHeight; // 获取(原生)dom 高
var offsetTop = shareContent.offsetTop; //元素距离顶部的偏移量
var canvas = document.createElement('canvas'); //创建canvas 对象
var context = canvas.getContext('2d');
var scaleBy = main.getPixelRatio(context); //获取像素密度的方法 (也可以采用自定义缩放比例)
canvas.width = width * scaleBy; //这里 由于绘制的dom 为固定宽度,居中,所以没有偏移
canvas.height = (height + offsetTop) * scaleBy; // 注意高度问题,由于顶部有个距离所以要加上顶部的距离,解决图像高度偏移问题
context.scale(scaleBy, scaleBy);
var opts = {
allowTaint:true,//允许加载跨域的图片
tainttest:true, //检测每张图片都已经加载完成
scale:scaleBy, // 添加的scale 参数
canvas:canvas, //自定义 canvas
logging: true, //日志开关,发布的时候记得改成false
width:width, //dom 原始宽度
height:height //dom 原始高度
};
html2canvas(shareContent, opts).then(function (canvas) {
console.log("html2canvas");
var body = document.getElementsByTagName("body");
body[0].appendChild(canvas);
});
}
};
//最后运行代码
main.init();
</script>
</body>
</html>
运行上面的demo 前有以下 注意点:
注意元素的样式的使用:
外层元素width 不能使用百分比 ,避免导致图片与文字间缩放比例问题错误使用方式如
.container {
width:50%;
margin: 0 auto;
}
需要改成如:
.container {
width:300px;
margin: 0 auto;
}
html2canvas html截图插件的更多相关文章
- html2canvas JS截图插件
github/download:https://github.com/niklasvh/html2canvas/releases 参考文章:基于html2canvas实现网页保存为图片及图片清晰度优化 ...
- html2canvas 网页截图 下载 上传
利用html2canvas插件 对网页截图 并下载和上传图片. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...
- IText&Html2canvas js截图 绘制 导出PDF
Html2canvas JS截图 HTML <div id="divPDF"> 需要截图的区域 </div> JS <script src=" ...
- chrome比较好用的网站整页(超长网页)截图插件
chrome比较好用的网站整页(超长网页)截图插件:fireshot capture 试用过比较好用
- js实现html转pdf+html2canvas.js截图不全的问题
最近做项目中遇到要把整个页面保存为PDF文件,网上找了一下实现的方法都是 html2canvas.js+jsPdf.js 来实现.实现的过程是 先用html2canvas.js把html页面转成图片, ...
- 微信图片生成插件,页面截图插件 html2canvas,截图失真 问题的解决方案
html2canvas 是一个相当不错的 JavaScript 类库,它使用了 html5 和 css3 的一些新功能特性,实现了在客户端对网页进行截图的功能.html2canvas 通过获取页面的 ...
- jquery截图插件的使用
首先感谢http://www.htmleaf.com/Demo/201504211717.html这款插件. 使用之初,对于插件的结构很是糊涂,首先文件的核心是cropper.js,其次才是mian. ...
- JS 使用html2canvas实现截图功能的问题记录和解决方案
在实现“截图”功能时,遇到几个bug,研究了一个上午,终于全部解决了: 下面给大家分享下: 1."图片资源跨域",导致无法截图. 浏览器会提示下面的错误 DOMException: ...
- html2canvas滚动截图
滚动截图 项目需求要进行动态的滚动截图搜索一下发现html2canvas可以实现截图,但是滚动截图网上搜罗了一遍发现不是很完善所以记录下 首先npm一下安装依赖: npm install html2c ...
随机推荐
- xenserver挂载新硬盘
注意:新加硬盘请不要加入raid,否则不认盘 一: 1.1:查看磁盘列表 fdisk -l [root@xenserver zz]# fdisk -l Disk /dev/sdb: 7999.4 GB ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- instanceof & isAssignableFrom的异同
instance 关注的是实例是否为类或接口的一个实例 isAssignableFrom 关注的是Class对象是否相同,或者Class1是Class2的超类或接口 Class1.isAssignab ...
- linux——系统内核参数优化
vim /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 fs.file-max = 999999 net.ipv4.tcp_max_tw_buckets = ...
- 通过pid杀死进程
bool ****::KillProcess(DWORD pid) { // When the all operation fail this function terminate the " ...
- @MapperScan使用
@MapperScan:要扫描mapper类包的路径 还可以扫描多个包,如: @MapperScan({"com.kfit.demo","com.kfit.user&qu ...
- 【oracle入门】数据模型
数据模式也是一这种模型,它是数据库中用于提供信息表示的操作手段的形式架构,是数据库中用来对现实世界惊喜抽象的工具.数据模型按不同的应用层次分为3种类型,分别为概念数据模型.逻辑数据模型.物理数据模型. ...
- Js/Bind()的认识
1.bind( eventType [, eventData], handler(eventObject))2.向绑定的对象上面提供一些事件方法的行为.其中三个参数的意义分别代表: 一.eventTy ...
- 3.1 unittest简介
3.1 unittest简介 前言 熟悉java的应该都清楚常见的单元测试框架Junit和TestNG.python里面也有单元测试框架-unittest,相当于是一个python版的junit.py ...
- jquery移除元素时会自动解绑事件
.html() When .html() is used to set an element's content, any content that was in that element is co ...