Blogger Information
Blog 29
fans 1
comment 1
visits 24863
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
canvas上传base64图片
谦谦允水的博客
Original
1078 people have browsed it

实例

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

运行实例 »

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


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post