h5實現簽名

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>簽名</title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			body{
				background: gray;
			}
			#test{
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				margin: auto;
				background:white;
			}
		</style>
	</head>
	<body>
		<canvas id="test" width="500" height="500"></canvas>
	</body>
	<script type="text/javascript">
		
		window.onload=function(){
			
			var canvas =document.getElementById("test");
			if(canvas.getContext){
				var ctx = canvas.getContext("2d");
			}
			
			canvas.onmousedown=function(ev){
				ev = ev || window.event;
				if(canvas.setCapture){
					canvas.setCapture();
				}
                
				ctx.beginPath()
                ctx.moveTo(ev.clientX - canvas.offsetLeft, ev.clientY - canvas.offsetTop)
				document.onmousemove=function(ev){
                    ctx.save()
                    ctx.strokeStyle = 'green'
                    ctx.lineWidth = 12
					ev = ev || event;
                    ctx.lineTo(ev.clientX - canvas.offsetLeft, ev.clientY - canvas.offsetTop)
                    ctx.stroke()
                    ctx.restore()
				}
				document.onmouseup=function(){
					document.onmousemove=document.onmouseup=null;
					if(document.releaseCapture){
						document.releaseCapture();
					}
				}
                
				return false;
               
			}
			
		}
		
	</script>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章