html2canvas实现页⾯截图效果
  昨天打开pc端的知乎,⽆意中发现右下⾓有⼀个很有意思的功能,意见反馈。当我们点击这个按钮的时候,就会弹出⼀个弹出层,让我们反映问题。下⾯有⼀个全页⾯的缩
率图:
  我是第⼀次看到这种效果,到底他这个缩率图怎么实现的呢?通过万能的百度,我了解到了⼀个叫 html2canvas 实现的。⼜是html5,好吧,我不得不承认你太强⼤了。
  我们把这个下载下来之后,放到 core ⽂件夹之内。先给⼤家看⼀下我的⽬录结构吧:
  core存放 html2canvas,static放 js,css等资源,index.html 就是我们展⽰的页⾯。
  html2canvas 就是将Dom节点在Canvas⾥边画出来。他的⽅法也很简单,本⽂只⽤到了⼀个⽅法html2canvas。它接收两个参数第⼀个是要画的元素,第⼆个是回调函数。
下⾯我们准备⼀个页⾯:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--360浏览器优先以webkit内核解析-->
<title>H+ 后台主题UI框架 - 主页⽰例</title>
<link rel="shortcut icon" href="favicon.ico">
<link href="static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link href="static/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
<link href="static/css/animate.min.css" rel="stylesheet">
<link href="static/css/style.min.css?v=4.1.0" rel="stylesheet">
<style>
#xlt canvas{max-width:100%}
</style>
</head>
<body class="gray-bg">
<div class="ibox-title">
<button class="btn btn-danger btn-circle btn-lg" type="button" title="建议反馈" id="yjfk">
<i class="fa fa-heart"></i>
</button>
</div>
<div class="row  border-bottom white-bg dashboard-header">
<div class="col-sm-12">
<blockquote class="text-warning" >您是否需要⾃⼰做⼀款后台、会员中⼼等等的,但是⼜缺乏html等前端知识…
<br>您是否⼀直在苦苦寻⼀款适合⾃⼰的后台主题…
<br>您是否想做⼀款⾃⼰的web应⽤程序…
<br>…………
<h4 class="text-danger">那么,现在H+来了</h4>
</blockquote>
<hr>
</div>
<div class="col-sm-3">
<h2>Hello,Guest</h2>
<small>移动设备访问请扫描以下⼆维码:</small>
<br>
<br>
<img src="img/qr_code.png" width="100%" >
<br>
</div>
<div class="col-sm-5">
<h2>
H+ 后台主题UI框架
</h2>
<p>H+是⼀个完全响应式,基于Bootstrap3.3.6最新版本开发的扁平化主题,她采⽤了主流的左右两栏
式布局,使⽤了Html5+CSS3等现代技术,她提供了诸多的强⼤的可以重新组合的UI组件,并集成了最新的jQuery版本(v2.1.4),当然,也集            <p>
<b>当前版本:</b>v4.1.0
</p>
<p>
<b>定价:</b><span class="label label-warning">¥988(不开发票,不议价)</span>
</p>
<br>
<p>
<a class="btn btn-success btn-outline" href="wpa.qq/msgrd?v=3&uin=516477188&site=qq&menu=yes" target="_blank">
<i class="fa fa-qq"> </i> 联系我
</a>
<a class="btn btn-white btn-bitbucket" href="www.zi-han" target="_blank">
<i class="fa fa-home"></i> 访问博客
</a>
</p>
</div>
<div class="col-sm-4">
<h4>H+具有以下特点:</h4>
<ol>
<li>完全响应式布局(⽀持电脑、平板、⼿机等所有主流设备)</li>
<li>基于最新版本的Bootstrap 3.3.6</li>
<li>提供3套不同风格的⽪肤</li>
<li>⽀持多种布局⽅式</li>
<li>使⽤最流⾏的的扁平化设计</li>
<li>提供了诸多的UI组件</li>
<li>集成多款国内优秀插件,诚意奉献</li>
<li>提供盒型、全宽、响应式视图模式</li>
<li>采⽤HTML5 & CSS3</li>
<li>拥有良好的代码结构</li>
<li>更多……</li>
</ol>
</div>
</div>
<div class="wrapper wrapper-content">
<div class="row">
<div class="col-sm-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>⼆次开发</h5>
</div>
<div class="ibox-content">
<p>我们提供基于H+的⼆次开发服务,具体费⽤请联系作者。</p>
<p>同时,我们也提供以下服务:</p>
<ol>
<li>基于WordPress的⽹站建设和主题定制</li>
<li>PSD转WordPress主题</li>
<li>PSD转XHTML</li>
<li>Html页⾯(CSS+XHTML+jQuery)制作</li>
</ol>
</div>
</div>
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>联系信息</h5>
</div>
<div class="ibox-content">
<p><i class="fa fa-send-o"></i> 博客:<a href="www.zi-han" target="_blank">www.zi-han</a>
</p>
<p><i class="fa fa-qq"></i> QQ:<a href="wpa.qq/msgrd?v=3&uin=516477188&site=qq&menu=yes" target="_blank">516477188</a>                        </p>
<p><i class="fa fa-weixin"></i> :<a href="javascript:;">zheng-zihan</a>
</p>
<p><i class="fa fa-credit-card"></i> ⽀付宝:<a href="javascript:;" class="⽀付宝信息">zheng-zihan@qq / *⼦涵</a>
</p>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>更新⽇志</h5>
</div>
<div class="ibox-content no-padding">
<div class="panel-body">
<div class="panel-group" id="version">
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v41">v4.1.0</a><code class="pull-right">2015.12.21</code>
</h5>
</div>
<div id="v41" class="panel-collapse collapse in">
<div class="panel-body">
<div class="alert alert-warning">此版本是⼀个维护版本,主要是升级和修复bug,让我们共同期待5.0版的到来</div>
<ol>
<li>增加不⽀持IE8的页⾯提⽰</li>
<li>修复页⾯链接和表单提交默认在新窗⼝中打开的问题</li>
<li>更新suggest插件,修复错位问题</li>
<li>升级bootstrap版本到3.3.6版本</li>
<li>升级layer到2.1版本</li>
<li>升级echarts到2.2.7版本</li>
<li>升级webuploader到0.1.5版本</li>
<li>修复⽹络条件不好情况下,页⾯加载提⽰遮挡页⾯⽆法操作的问题</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v40">v4.0.0</a><code class="pull-right">2015.10.21</code>
</h5>
</div>
<div id="v40" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>升级bootstrap到最新版本3.3.5;</li>
<li>升级jquery版本到最新版本2.1.4;</li>
<li>升级Font Awesome到最新版本4.4.0;</li>
<li>修复了登录页⾯的⼀处错误;</li>
<li>修复了主页⾯出现多个滚动条的问题;</li>
<li>修复了已知的各种浏览器兼容问题;</li>
<li>修复了layphoto和suggest等页⾯的显⽰问题;</li>
<li>新增Glyphicons字体图标的预览;</li>
<li>新增对不⽀持的浏览器的友好提⽰;</li>
<li>新增视频/⾳乐播放器的⽀持;</li>
<li>新增Bootstrap Table(推荐);</li>
<li>进⼀步完善了开发⽂档;</li>
<li>提供了离线⽀持,开箱即⽤;</li>
<li>对IE系列的浏览器⽀持更好。</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v32">v3.2.0</a><code class="pull-right">2015.09.22</code>
</h5>
</div>
<div id="v32" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>修复360浏览器个别情况下崩溃的问题;</li>
<li>优化360浏览器的显⽰效果;</li>
<li>修复layim弹出聊天窗⼝出错的问题;</li>
<li>优化登录、注册、锁屏页⾯,使其在右侧主体区域打开时转到新页⾯打开;</li>
<li>优化了移动设备的展⽰效果。</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v31">v3.1.0</a><code class="pull-right">2015.09.18</code>
</h5>
</div>
<div id="v31" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>修复登录页⾯⽂本框输⼊字符不可见的问题;</li>
<li>修复⽕狐下conTabs关闭操作按钮不下拉的问题;</li>
<li>修复IOS系统下各种浏览器右侧内容不滚动的问题;</li>
<li>优化了主页⽰例三的显⽰;</li>
<li>修复了jqgrid编辑弹框导致页⾯错位的问题;</li>
<li>使百度Echarts图标⽀持响应式,并优化了其显⽰;</li>
<li>修复Summernote⽂本编辑器下拉菜单显⽰不全的问题;</li>
<li>修复Toastr通知图标显⽰多个的问题。</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v301">v3.0.x</a><code class="pull-right">2015.09.04</code>
</h5>
</div>
<div id="v301" class="panel-collapse collapse">
<div class="panel-body">
<p>⾃H+ v3.0版发布后,⼜陆续发布了7个⼩版本,修复了⼀些兼容性问题,但是没有对外发布,更新⽇志略过……</p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v30">v3.0.0</a><code class="pull-right">2015.09.03</code>
</h5>
</div>
<div id="v30" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>今天是情⼈节,H+终于跨到了v3.0,就是是情⼈节礼物吧,感谢你们的不离不弃,⼀路相伴! <a href="#" class="viewlog"><i class="fa fa-eye"></i> 查看升级⽇志</a>
</li>
<li><span class="text-danger">注意:</span>本次更新调整较⼤,不建议直接覆盖升级!</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v22">v2.2.0</a><code class="pull-right">2015.05.12</code>
</h5>
</div>
<div id="v22" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>根据⽤户的反馈,根据⽤户的反馈,移除了CDN⽀持,CDN服务将于2015年6⽉30⽇之后结束⽀持,如果您正在使⽤CDN服务,请尽快完成迁移,对于给您造成的不便,我们表⽰⾮常抱歉;
</li>
<li>升级Bootstrap到最新版本v3.3.4;
</li>
<li>修改了style.css,修复了其中的⼀些bug;
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v21">v2.1.0</a><code class="pull-right">2015.03.19</code>
</h5>
</div>
<div id="v21" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>增加cdn服务⽀持,cdn节点使⽤阿⾥云服务,可保证您的项⽬随时使⽤最新版本的H+,免去反复升级的⿇烦;
</li>
<li>修复⼀些问题;</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v20">v2.0.0</a><code class="pull-right">2015.03.02</code>
</h5>
</div>
<div id="v20" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>增加RTL布局及RTL⽀持,可点击右上⾓齿轮按钮选择RTL模式查看;
</li>
<li>增加上下布局; <a href="index_4.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>在360浏览器6.0以上版本中强制以webkit内核解析,体验更佳;
</li>
<li>增加<a href="toastr_notifications.html">Toastr通知</a>、<a href="nestable_list.html">嵌套列表</a>、<a href="timeline_v2.html">时间轴</a>、<a href="forum_main.html">论坛</a>、<a href="code_editor.ht                                                </li>
<li>升级<a href="icons.html">Font Awesome</a>,<a href="form_simditor.html">Simditor</a>等到最新版本;
</li>
<li>优化部分内容
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v18">v1.8.0</a><code class="pull-right">2015.02.23</code>
</h5>
</div>
<div id="v18" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>修复⼀些错误;
</li>
<li>修复了WebUploader中的⼀些问题; <a href="form_webuploader.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v17">v1.7.0</a><code class="pull-right">2015.02.11</code>
</h5>
</div>
<div id="v17" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>更新jquery版本到官⽅最新版v2.1.1;
</li>
<li>更新Bootstrap版本到官⽅最新版v3.3.0;
</li>
<li>增加jqGrid组件; <a href="table_jqgrid.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
jquery弹出div窗口
<li>修复Summernote编辑器中的⼀个严重错误; <a href="form_editors.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>修改了⼀些已知的bug,并修复了演⽰⽰例中的⼀些错误;
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h5 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v16">v1.6.0</a><code class="pull-right">2014.01.26</code>
</h5>
</div>
<div id="v16" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>优化资源⽂件路径,删除多余⽂件
</li>
<li>增加Markdown编辑器 <a href="form_markdown.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>增加拾⾊器ColorPicker <a href="form_advanced.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>优化部分页⾯代码
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v15">v1.5.0</a><code class="pull-right">2014.01.17</code>
</h4>
</div>
<div id="v15" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>增加了Bootstrap3表单构建器,表单设计更轻松; <a href="form_builder.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>修改了webim的⾼度;
</li>
<li>修复了因缺少jquery.min.map⽂件⽽导致页⾯加载进度条速度过慢的问题;
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><a data-toggle="collapse" data-parent="#version" href="#v14">v1.4.0</a><code class="pull-right">2015.01.05</code>
</h4>
</div>
<div id="v14" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>修复了百度ECharts图表显⽰不正确的问题; <a href="graph_echarts.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>新增表单验证⽰例,使⽤jQuery Validate插件实现; <a href="form_validate.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>新增树形视图⽰例; <a href="tree_view.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>修复弹框遮罩的Bug;
</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v13">v1.3.0</a><code class="pull-right">2014.12.21</code>
</h4>
</div>
<div id="v13" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>加⼊阿⾥巴巴团队的字体图标库,字体图标可以⾃定义啦; <a href="iconfont.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>新增头像裁剪上传组件FullAvatareditor; <a href="form_avatar.html" title="去看看"><i class="fa f
a-eye"></i></a>
</li>
<li>集成⽹页弹层插件layer; <a href="layer.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>集成⽇期选择器layerDate; <a href="layerdate.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li class="text-danger"><b>增加web即时通讯功能,可以在系统内了;</b> <a href="webim.html" title="去看看"><i class="fa fa-eye"></i></a>                                                </li>
<li>增加主题预览功能,点击右上侧齿轮图标预览;</li>
<li>增加左侧边栏固定功能;</li>
<li>修复了多处问题。</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v12">v1.2.0</a><code class="pull-right">2014.12.05</code>
</h4>
</div>
<div id="v12" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>新增百度WebUploader拖动上传⽂件组件;<a href="form_webuploader.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>新增国产优秀富⽂本编辑器插件Simditor;<a href="form_simditor.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>新增百度ECharts统计图表插件;<a href="form_simditor.html" title="去看看"><i class="fa fa-eye"></i></a>
</li>
<li>修复了⼏处问题。</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v11">v1.1.0</a><code class="pull-right">2014.11.18</code>
</h4>
</div>
<div id="v11" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>修复了⼏处问题。</li>
</ol>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#version" href="#v11">v1.0.0</a><code class="pull-right">2014.11.10</code>
</h4>
</div>
<div id="v11" class="panel-collapse collapse">
<div class="panel-body">
<ol>
<li>Hplus正式发布。</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>购买说明</h5>
</div>
<div class="ibox-content">
<p>购买后我可以获得什么?</p>
<ol>
<li>所有源码(未压缩、带注释版本);</li>
<li>说明⽂档;</li>
<li>终⾝免费升级服务;</li>
<li>必要的技术⽀持;</li>
<li>付费⼆次开发服务;</li>
<li>授权许可;</li>
<li>……</li>
</ol>
<hr>
<p>如果需要购买H+主题,可直接付款到⽀付宝:<a href="javascript;">zheng-zihan@qq</a>,收款⼈:<a href="javascript;">*⼦涵</a>。也可以使⽤⼿机⽀付宝或者扫码⽀付:</p>                        <div class="alert alert-warning">
付款完成后请及时联系作者,或在付款备注中留下邮箱或QQ,⽅便作者及时联系您。
<br>如果图⽚太⼩,可以点击图⽚放⼤。
</div>
<p id="pay-qrcode">
<a href="javascript:;"><img src="img/pay.png" width="100%" alt="请使⽤⼿机⽀付宝或者扫码⽀付">
</a>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="ibox-content" id="msgBox">
<div class="form-horizontal">
<div class="form-group">
<div class="col-sm-12">
<textarea class="form-control" placeholder="告诉我们你的建议或遇到的问题。"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-12" id="xlt">
<div class="sk-spinner sk-spinner-chasing-dots" >
<div class="sk-dot1"></div>
<div class="sk-dot2"></div>
</div>
</div>
</div>
</div>
</div>
<script src="static/js/jquery.min.js?v=2.1.4"></script>
<script src="static/js/bootstrap.min.js?v=3.3.6"></script>
<script src="static/layer/layer.js"></script>
</body>
</html>
  下⾯我们加⼊ js 就可以实现这个了:
<script src="core/dist/html2canvas.js"></script>
<script type="text/javascript">
$("#yjfk").click(function(){
html2canvas(document.body).then(function(canvas) {
$("#xlt").html(canvas);
});
//页⾯层
layer.open({
title:'意见反馈',
type: 1,
skin: 'layui-layer-rim', //加上边框
area: ['500px', '700px'], //宽⾼
content: $("#msgBox")
});
})
</script>
  简单的讲解⼀下:document.body是我要画的DOM元素,即将整个body都画下来。回调函数中,我们将这个canvas渲染到我们需要的展⽰的元素中就⾏。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。