【html2canvas】html2canvas生成海报

html2canvas生成海报

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

它不需要来自服务器任何渲染,整张图片都是在客户端浏览器创建。当浏览器不支持Canvas时,将采用Flashcanvas或ExplorerCanvas技术代替实现。以下浏览器能够很好的支持该脚本:Firefox 3.5+, Google Chrome, Opera新的版本, IE9以上的浏览器。

html2canvas可以通过获取HTML的某个元素,然后生成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–这个我也不知道是干嘛的

html2canvas官网下载js:
http://html2canvas.hertzen.com/?utm_source=caibaojian.com

jQuery下载:
http://jquery.com/download/

jQuery的API使用文档:
http://jquery.cuishifeng.cn/


以下是自己的代码:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<title>html2canvas生成海报</title>
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/html2canvas.js"></script>
</head>
<body>
	<div>
		<input type="button" value="截图" onclick="takeScreenshot()">
		<input type="button" value="保存图片" onclick="save()">
	</div>
	<div id="view" style="width: 100%; background:#eee;padding: 20px;margin: 10px 0">
		<p>图片不见了</p>
		<img class="bg"style="height: 300px;width: 100%" src="http://img.aworld.cn/o_1csndcipj18p61e8sgtg19l31bgfl.jpg" alt="图片不见了">
	    <h4 style="color: #000; ">Hello world!</h4>
	</div>

<script type="text/javascript">
	function takeScreenshot() {
		//两个参数:所需要截图的元素id,截图后要执行的函数, canvas为截图后返回的最后一个canvas
		html2canvas(document.getElementById('view'), {
			useCORS: true
		}).then(function(canvas) {
			document.body.appendChild(canvas);
			$(document).find('canvas').attr('id', 'mycanvas');
		});
	}
	function save() {
		var canvas = document.getElementById("mycanvas"); //获取canvas
		var data = canvas.toDataURL('image/png');
		console.log(data)

	}
</script>
</body>

</html>

代码实现如下图:

在这里插入图片描述


html2canvas的使用,可详细查看参数

猜你喜欢

转载自blog.csdn.net/LuviaWu/article/details/84764557
今日推荐