首页 > 代码库 > Canavs arcTo方法的理解
Canavs arcTo方法的理解
arcTo方法有四个參数
參数1,2为第一个控制点的x,y坐标,參数2为第二个控制点的坐标,參数3为绘制圆弧的半径.
起点和第一个控制点组成的延长线与第一个控制点和第二个控制点组成的延长线都是和圆弧相切的,这个圆弧也就是被夹在两条延长线中间.圆越大,两条延长线能形成的角度能夹住的圆弧就越小.
以下写了一个简单的动画帮助理解
<!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>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。