博客列表 >canvas上传base64图片

canvas上传base64图片

谦谦允水的博客
谦谦允水的博客原创
2018年03月26日 13:59:221068浏览

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>canvas多张图片上传</title>
	<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<style type="text/css">
	#ip{font-size: 28px;color: red}
	#img{display: none;}
</style>
<body>
	<canvas id="canvas" width="300" height="300" style="border:1px solid #ccc;display: block; margin: 0 auto;">
	</canvas>
	<img src="" id="img">
	<input type="file" id="file" name="">
</body>
<script type="text/javascript">
	$('#file').change(function(e){
		var file=e.target.files[0];
		var url=window.URL.createObjectURL(file);
		var img=document.getElementById('img');
		img.src=url;
		img.onload=function(){
			var canvas=document.getElementById('canvas');
			var sy=document.getElementById('sy');
			var ctx=canvas.getContext('2d');
			var tempw=img.width;
			var temph=img.height;
			img.width=500;
			img.height=500/tempw*temph;	
			canvas.width=img.width;
			canvas.height=img.height;	
			ctx.drawImage(img,0,0,canvas.width, canvas.height);
			// ctx.fillStyle="rgba(252,255,255,0.7)";
			// ctx.fillRect(0,0,150,75);
			ctx.font="30px 微软雅黑";
			ctx.fillText("PengLi",10,50);
			//ctx.drawImage(sy,0,0);
			var dataUrl=canvas.toDataURL("image/jpeg",0.9);	
			console.log(dataUrl);
		}		
	})
</script>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议