html5 canvas 头像上传

简介: html5 canvas 头像上传
html5 canvas 头像上传
<html>  
<head>  
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
	<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
	<script src="https://cdn.bootcss.com/popper.js/1.9.3/umd/popper.min.js" integrity="sha384-knhBOwpf8/28D6ygAjJeb0STNDZqfPbKaWQ8wIz/xgSc0xXwKsKhNype8fmfMka2" crossorigin="anonymous"></script>
	<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</head>  
<body>  

	<div id="canvasDiv">
		<canvas id="crop" width="480" height="270" style="background-image:url(bg.png);">
	</div>
	<div>  
		<div>  
			<input type="file" name="imgUpload" id="imgUpload">  
		</div>  
		<div styel="width:100px; height:100px;padding-top:100px;">
			<img id="showImg"></img>
		</div>  
	</div>
<script>
	var canvas = document.getElementById('crop');
	var ctx = canvas.getContext('2d');
	var src;	
	var canvasWidth = canvas.width;
	var canvasHeight = canvas.height;
	var wheelRate = 1;
	var tx = 0;
	var ty = 0;
	var downX = 0;
	var downY = 0;
	var moveX = 0;
	var moveY = 0;
	
	var moveFlag = false;

	var img = new Image();
	
	var imgWidth = 0;
	var imgHeight = 0;
	var widthRate = 0;
	var heightRate = 0;
	var rate = 0;
	var squareSideLength = 0;
	
	var x = 0;
	var y = 0;
	
	var imgTx = 0;
	var imgTy = 0;
	
	$("#imgUpload").change(function(e) {
		for (var i = 0; i < e.target.files.length; i++) {
			var file = e.target.files.item(i);            
			var freader = new FileReader();  
			freader.readAsDataURL(file);  
			freader.onload = function(e) {
				src = e.target.result; 
				wheelRate = 1;
				tx = 0;
				ty = 0;
				$("#uploadhead").attr("src",src);
				img.src = src;
				imgWidth = img.width;
				imgHeight = img.height;
				squareSideLength = imgWidth < imgHeight ? imgWidth : imgHeight;
				widthRate = canvasWidth / imgWidth;
				heightRate = canvasHeight / imgHeight;
				drawCanvas();
			}  
		}
	});  
	
	canvas.onmousewheel = function(e){
		var evt = window.event ||e;
		var delta = evt.detail ? -evt.detail/3 : evt.wheelDelta/120;
		if(delta < 0){
			wheelRate *= 0.9;
		}else {
			if(wheelRate < 1){
				wheelRate /=  0.9;
			}
			
		}
		ctx.restore();
		ctx.clearRect(0,0,canvasWidth,canvasHeight);
		drawCanvas();
	}
	
	canvas.onmousedown = function(e){
		var evt = window.event ||e;
		downX = evt.clientX;
		downY = evt.clientY;
		moveX = tx;
		moveY = ty;
		moveFlag = true;
	}
	
	canvas.onmousemove = function(e){
		if(moveFlag){
			var evt = window.event ||e;
			tx = evt.clientX - downX + moveX;
			ty = evt.clientY - downY + moveY;
			if(x + tx/rate < 0){
				tx = -x*rate;
			}
			if(x + tx/rate + squareSideLength*wheelRate > imgWidth){
				tx = (imgWidth - x - squareSideLength*wheelRate)*rate;
			}
			if(y + ty/rate < 0){
				ty = -y*rate;
			}
			if(y + ty/rate + squareSideLength*wheelRate > imgHeight){
				ty = (imgHeight - y - squareSideLength*wheelRate)*rate;
			}
			
			drawCanvas();
			
		}
	}
	
	document.onmouseup = function(){
		moveFlag = false;
	}
	
	function drawCanvas(){
		ctx.restore();
		ctx.clearRect(0,0,canvasWidth,canvasHeight);
		rate = widthRate < heightRate ? widthRate : heightRate;
		ctx.save();
		ctx.scale(rate,rate);
		ctx.translate((canvas.width/rate - imgWidth)/2,(canvas.height/rate - imgHeight)/2);
		//ctx.drawImage(img, 0, 0);
		ctx.globalAlpha=0.5;
		ctx.translate(-(canvas.width/rate - imgWidth)/2,-(canvas.height/rate - imgHeight)/2);
		//ctx.fillRect(0,0,canvas.width/rate,canvas.height/rate);
		ctx.translate((canvas.width/rate - imgWidth)/2,(canvas.height/rate - imgHeight)/2);
		//var sx = (canvasWidth/rate - imgWidth)/2;
		//var sy = (canvasHeight/rate - imgHeight)/2;
		x = (imgWidth - squareSideLength)/2;
		y = (imgHeight - squareSideLength)/2;
		//ctx.drawImage(img, x, y, squareSideLength, squareSideLength, x, y, squareSideLength, squareSideLength);
		ctx.globalAlpha=1.0;
		ctx.drawImage(img, x + tx/rate, y + ty/rate, squareSideLength*wheelRate, squareSideLength*wheelRate, x + tx/rate, y + ty/rate, squareSideLength*wheelRate, squareSideLength*wheelRate);
		imgData = ctx.getImageData(x - tx, y - ty, squareSideLength*wheelRate, squareSideLength*wheelRate);
		canvasToImage();
		ctx.restore();
		ctx.clearRect(0,0,canvasWidth,canvasHeight);
		rate = widthRate < heightRate ? widthRate : heightRate;
		ctx.save();
		ctx.scale(rate,rate);
		ctx.translate((canvas.width/rate - imgWidth)/2,(canvas.height/rate - imgHeight)/2);
		ctx.drawImage(img, 0, 0);
		ctx.globalAlpha=0.5;
		ctx.translate(-(canvas.width/rate - imgWidth)/2,-(canvas.height/rate - imgHeight)/2);
		ctx.fillRect(0,0,canvas.width/rate,canvas.height/rate);
		ctx.translate((canvas.width/rate - imgWidth)/2,(canvas.height/rate - imgHeight)/2);
		//var sx = (canvasWidth/rate - imgWidth)/2;
		//var sy = (canvasHeight/rate - imgHeight)/2;
		x = (imgWidth - squareSideLength)/2;
		y = (imgHeight - squareSideLength)/2;
		//ctx.drawImage(img, x, y, squareSideLength, squareSideLength, x, y, squareSideLength, squareSideLength);
		ctx.globalAlpha=1.0;
		ctx.drawImage(img, x + tx/rate, y + ty/rate, squareSideLength*wheelRate, squareSideLength*wheelRate, x + tx/rate, y + ty/rate, squareSideLength*wheelRate, squareSideLength*wheelRate);
		
			
	}
	
	function canvasToImage(){
		base64Str = canvas.toDataURL("image/png");
		$('#showImg').attr('src', base64Str);
		imgTx = squareSideLength*rate/2 - 240*wheelRate - tx;
		imgTy = squareSideLength*rate/2 - 135*wheelRate - ty;
		console.log(squareSideLength*rate/wheelRate);
		$('#showImg').css('transform', 'scale(' + (1/wheelRate) +') translateX(' + imgTx + 'px) translateY(' + imgTy + 'px) ');
	}
</script>  
</body>  
</html>  

相关实践学习
Serverless极速搭建Hexo博客
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
目录
相关文章
|
4月前
|
移动开发 前端开发 JavaScript
纯JavaScript实现HTML5 Canvas六种特效滤镜
纯JavaScript实现HTML5 Canvas六种特效滤镜
145 6
|
3月前
|
移动开发 前端开发 JavaScript
基于 HTML5 和 Canvas 开发的在线图片编辑器
基于 HTML5 和 Canvas 开发的在线图片编辑器
79 0
|
4月前
|
移动开发 前端开发 API
HTML5 Canvas渐进填充与透明
HTML5 Canvas渐进填充与透明
150 7
|
4月前
|
移动开发 前端开发 JavaScript
HTML5 Canvas鼠标与键盘事件
HTML5 Canvas鼠标与键盘事件
54 5
|
4月前
|
移动开发 前端开发 JavaScript
HTML5 Canvas平移,放缩,旋转演示
HTML5 Canvas平移,放缩,旋转演示
47 4
|
4月前
|
移动开发 前端开发 API
HTML5 Canvas 填充与描边(Fill And Stroke)
HTML5 Canvas 填充与描边(Fill And Stroke)
75 3
|
1月前
|
XML 移动开发 前端开发
HTML5 SVG和canvas的性能探讨
HTML5 中的 SVG(可缩放矢量图形)和 Canvas(画布)分别用于网页图形绘制。SVG 基于矢量图形,使用 XML 描述,适合静态或少量动态内容(如图标、图表),易于编辑且保持高分辨率;Canvas 则基于位图,通过 JavaScript 绘制,更适合快速更新大量图形的场景(如游戏、动态动画),但在复杂图形计算时可能遇到性能瓶颈。总体而言,SVG 适用于静态和少量动态内容,而 Canvas 更适合高频率更新和性能要求高的场景。
|
1月前
|
移动开发 前端开发 JavaScript
HTML5 Canvas详解及应用
HTML5 Canvas 允许通过 JavaScript 在网页上动态绘制图形、动画等视觉内容。首先在 HTML 中定义 `&lt;canvas&gt;` 元素,并通过 JavaScript 获取画布上下文进行绘制。常见方法包括绘制矩形、路径、圆形和文本,以及处理图像和创建动画效果。适用于游戏开发、数据可视化、图像编辑和动态图形展示等多种应用场景。需要注意性能优化、无状态绘制及自行处理事件等问题。
|
28天前
|
移动开发 前端开发 数据挖掘
用HTML5中的 画布(Canvas)在“圳品”信息系统网页上绘制显示饼图
用HTML5中的 画布(Canvas)在“圳品”信息系统网页上绘制显示饼图
|
2月前
|
Dart 前端开发 Java