码迷,mamicode.com
首页 > 其他好文 > 详细

Canavs arcTo方法的理解

时间:2014-05-18 10:40:59      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   tar   

arcTo方法有四个参数

参数1,2为第一个控制点的x,y坐标,参数2为第二个控制点的坐标,参数3为绘制圆弧的半径.

起点和第一个控制点组成的延长线与第一个控制点和第二个控制点组成的延长线都是和圆弧相切的,这个圆弧也就是被夹在两条延长线中间.圆越大,两条延长线能形成的角度能夹住的圆弧就越小.

bubuko.com,布布扣

下面写了一个简单的动画帮助理解

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
canvas {
	width: 400px;
	height: 400px;
	background-color: #EEEEEE;
}
</style>
<script>
	window.onload = function() {
		var canvas = document.getElementById(‘canvas‘);
		var arcToPoint1 = {x:120, y:40};
		var arcToPoint2 = {x:60, y:80}
		var context = canvas.getContext(‘2d‘);
		if (!canvas || !canvas.getContext) {
			return;
		} else {
			timer = setInterval(function(){
				if (arcToPoint2.x < 150) {
					arcTo (context, arcToPoint1, arcToPoint2);
					arcToPoint2.x += 2;
				} else {
					clearInterval(timer);
				}
			}, 300);
		}
	}
	
	function arcTo () {
		var startPoint = {x: 20, y: 40};
		var context = arguments[0];
		var arcToPoint1 = arguments[1];
		var arcToPoint2 = arguments[2];
		var context = canvas.getContext(‘2d‘);
		
		context.clearRect(0,0,context.canvas.width, context.canvas.height)
		
		context.beginPath();
		context.moveTo(startPoint.x, startPoint.y);
		context.strokeStyle = "red";
		context.lineWidth = 1;
		context.arcTo(arcToPoint1.x, arcToPoint1.y, arcToPoint2.x, arcToPoint2.y, 40);
		context.stroke();
		
		context.beginPath();
		context.moveTo(startPoint.x, startPoint.y);
		context.strokeStyle = "black";
		context.lineWidth = 1;
		context.lineTo(arcToPoint1.x, arcToPoint1.y);
		context.fillText(‘arcToPoint1‘, arcToPoint1.x + 10, arcToPoint1.y - 5) 
		context.stroke();
		
		context.beginPath();
		context.moveTo(arcToPoint1.x, arcToPoint1.y);
		context.strokeStyle = "black";
		context.lineWidth = 1;
		context.lineTo(arcToPoint2.x, arcToPoint2.y);
		context.fillText(‘arcToPoint2‘, arcToPoint2.x + 10, arcToPoint2.y + 10) 
		context.stroke();
	}
</script>
</head>
<body>
	<canvas id="canvas">
		此游览器不支持canvas标签
	</canvas>
</body>
</html>


Canavs arcTo方法的理解,布布扣,bubuko.com

Canavs arcTo方法的理解

标签:style   blog   class   code   c   tar   

原文地址:http://blog.csdn.net/songzheng_741/article/details/26084829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!