canvas实现简单的画图工具中画笔效果,外加画好的笑脸

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>canvas实现简单的画图工具中画笔效果,外加画好的笑脸</title>
</head>
<body>
	<canvas width="500" height="500" style="background:#eee;"></canvas>
	<script>
		var canvas = document.getElementsByTagName('canvas')[0];
		var con = canvas.getContext('2d');
		canvas.onmousedown = function(e){
			var mx = e.layerX,
				my = e.layerY;
				con.moveTo(mx,my);
			canvas.onmousemove = function(e){
				var ex = e.layerX,
					ey = e.layerY;
				con.lineTo(ex,ey);
				con.stroke();
			}
			canvas.onmouseup = function(){
				canvas.onmousemove = null;
				canvas.onmouseup = null;
			}
		}
		//绘制矩形
		function myDrawRect(x,y,w,h){
			//从新开辟路径
			con.beginPath();
			con.moveTo(x,y);
			con.lineTo(x+w,y);
			con.lineTo(x+w,y+h);
			con.lineTo(x,y+h);
			//加上 closePath 会自动闭合路径
			con.closePath();
			con.stroke();
		}
		myDrawRect(200,200,30,40);
		myDrawRect(275,200,30,40);
		//绘制圆形
		function myDrawCircle(x,y,r){
			con.beginPath();
			for(var i=0;i<360;i++){
				con.lineTo(x+r*Math.cos(i*Math.PI/180),y+r*Math.sin(i*Math.PI/180));
				con.stroke();
			}
			con.closePath();
		}
		myDrawCircle(250,250,100);
		//半圆
		function myDrawCircle1(x,y,r){
			con.beginPath();
			for(var i=0;i<180;i++){
				con.lineTo(x+r*Math.cos(i*Math.PI/180),y+r*Math.sin(i*Math.PI/180));
				con.stroke();
			}
			//最后加上一条半径长度的直线。
			con.lineTo(x+r,y);
			con.stroke();
			con.closePath();
		}
		myDrawCircle1(250,260,70);
	</script>
</body>
</html>

  

转载于:https://www.cnblogs.com/webBlog-gqs/p/7956004.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值