canvas 画线条

<!DOCTYPE html>
<html>
	<head>
	   <style>
	   canvas{
	   background:#ccc;
	   }
	   </style>
		<title>noTitle</title>
		<meta charset="">
		<link rel="stylesheet" href="">
		<script>
		  window.οnlοad=function  () {
		    var canvas=document.getElementById("canvas");
			var cobj=canvas.getContext("2d");
//			cobj.strokeStyle="#ff0000";
//			cobj.rect(0,0,100,100);声明绘制一个矩形
//			cobj.stroke();开始绘制
//			cobj.fill();
//            drawRect (0,0,300,300)
//             function drawRect (x,y,w,h) {
//			   cobj.rect(x,y,w,h);
//			   cobj.stroke();
//             }
		  }
		</script>
	</head>
	<body>
	<canvas id="canvas" width=500 height=500>
	     请您升级浏览器
	</canvas>
	</body>
</html>
<pre name="code" class="html"><!DOCTYPE html>
<html>
	<head>
	   <style>
	   canvas{
	   background:#ccc;
	   }
	   </style>
		<title>noTitle</title>
		<meta charset="">
		<link rel="stylesheet" href="">
		<script>
		  window.οnlοad=function  () {
		    var canvas=document.getElementById("canvas");
			var cobj=canvas.getContext("2d");
//			cobj.moveTo(0,0);起点
//			cobj.lineTo(100,100);终点
//			cobj.stroke();开始绘制
//			cobj.beginPath();打断连续画图,重新设置起点
//			cobj.lineTo(0,200);
//			cobj.lineTo(300,300);
//			cobj.lineTo(100,400);
//			cobj.stroke();

             canvas.οnmοusedοwn=function  (e) {//模拟画板
			   var mx=e.layerX;
			   var my=e.layerY;
			   cobj.moveTo(mx,my);
			   canvas.οnmοusemοve=function  (e) {
			     var endx=e.layerX;
			     var endy=e.layerY;
				 cobj.lineTo(endx,endy);
				 cobj.stroke();
			   }
			   canvas.οnmοuseup=function  () {
			    canvas.οnmοusemοve=null;
				canvas.οnmοuseup=null;
			   }
             }
		  }
		</script>
	</head>
	<body>
	<canvas id="canvas" width=500 height=500>
	     请您升级浏览器
	</canvas>
	</body>
</html>


 
<pre name="code" class="html"><!DOCTYPE html>
<html>
	<head>
	   <style>
	   canvas{
	   background:#ccc;
	   }
	   </style>
		<title>noTitle</title>
		<meta charset="">
		<link rel="stylesheet" href="">
		<script>
		  window.οnlοad=function  () {
		    var canvas=document.getElementById("canvas");
			var cobj=canvas.getContext("2d");
			//实现画矩形
			 drawRect(100,100,200,200);
			 drawCircle(150,150,50);
            function drawRect (x,y,w,h) {
			  cobj.beginPath();
			  cobj.moveTo(x,y);
			  cobj.lineTo(x+w,y);
			  cobj.lineTo(x+w,y+h);
			  cobj.lineTo(x,y+h);
			  cobj.closePath();
			  cobj.stroke();
            }
			//画圆形
			 function drawCircle (x,y,r) {
			    cobj.beginPath();
               for (var i=0; i<360; i++) {
			     cobj.lineTo(x+r*Math.cos(i*Math.PI/180),y+r*Math.sin(i*Math.PI/180));//i为角度,则lineto的参数为弧度
				cobj.stroke();
               }
			 }
			}
		</script>
	</head>
	<body>
	<canvas id="canvas" width=500 height=500>
	     请您升级浏览器
	</canvas>
	</body>
</html>


 
<pre name="code" class="html"><!DOCTYPE html>
<html>
	<head>
	   <style>
	   canvas{
	   background:#ccc;
	   }
	   </style>
		<title>noTitle</title>
		<meta charset="">
		<link rel="stylesheet" href="">
		<script>
		  window.οnlοad=function  () {
		    var canvas=document.getElementById("canvas");
			var cobj=canvas.getContext("2d");
			cobj.lineWidth=15;//设置线条的粗细
			//cobj.lineCap="square";设置起点的样式为方形,round为圆形
			//cobj.lineJoin ="bevel";设置拐角的类型,round为圆角,bevel为斜角
			cobj.miterLimit=3;
			cobj.moveTo(100,100);
			cobj.lineTo(200,200);
			cobj.lineTo(180,100);
			cobj.stroke();
			}
		</script>
	</head>
	<body>
	<canvas id="canvas" width=500 height=500>
	     请您升级浏览器
	</canvas>
	</body>
</html>



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值