Canavs arcTo方法的理解

來源:互聯網
上載者:User

標籤:style   blog   http   color   width   2014   

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.