HTML5 canvas 中的arcTo()方法的用法

來源:互聯網
上載者:User

標籤:style   blog   class   code   c   tar   

除了arc()之外,Canvas的繪圖環境對象還提供了另一個用於建立圓弧路徑的方法,那就是arcTo()。改方法接受了5個參數:

arcTo(x1,x2,y1,y2,radius)

arcTo()方法的參數分別代表兩個點擊圓形半徑。該方法一指定的半徑來繪製一條圓弧,此圓弧與當前點到第一個點(x1,y1)的連線相切,而且第一個點到第二點(x2,y2)的連線也相切。該方法的這些特性,使得它非常適合用了繪製矩形的原角。

使用arcTo()方法:

html:

<!Doctyp html>

<html>

         <head>

                    <title>Rounded Rectangles</title>

<style>

    #canvas{

                     background:lightskyblue;

                     -webkit-box-shadow:4px 4px 8px  rgba(0,0,0,0.5);

                       -moz-box-shadow: 4px  4px 8px rgba(0,0,0,0.5);

                       box-shadow:4px 4px 8px rgba(0,0,0,0.5);

                   } 

   </style>

</head>

      <body>

     <canvas id=‘canvas‘ width=‘575‘  height=‘200‘>

                 canvas not supported

         </canvas>

      <script src=‘example.js‘></script>

</body>

</html>

 

example.js

var context = document.getElementById(‘canvas‘).getContext(‘2d‘);function roundedRect(cornerX, cornerY, width, height, cornerRadius) {   if (width > 0) context.moveTo(cornerX + cornerRadius, cornerY);   else           context.moveTo(cornerX - cornerRadius, cornerY);   context.arcTo(cornerX + width, cornerY,                 cornerX + width, cornerY + height,                 cornerRadius);   context.arcTo(cornerX + width, cornerY + height,                 cornerX, cornerY + height,                 cornerRadius);   context.arcTo(cornerX, cornerY + height,                 cornerX, cornerY,                 cornerRadius);   if (width > 0) {      context.arcTo(cornerX, cornerY,                    cornerX + cornerRadius, cornerY,                    cornerRadius);   }   else {      context.arcTo(cornerX, cornerY,                    cornerX - cornerRadius, cornerY,                    cornerRadius);   }}function drawRoundedRect(strokeStyle, fillStyle, cornerX, cornerY, width, height, cornerRadius) {   context.beginPath();   roundedRect(cornerX, cornerY, width, height, cornerRadius);      context.strokeStyle = strokeStyle;   context.fillStyle = fillStyle;   context.stroke();   context.fill();}drawRoundedRect(‘blue‘,   ‘yellow‘,  50,  40,  100,  100, 10);drawRoundedRect(‘purple‘, ‘green‘,  275,  40, -100,  100, 20);drawRoundedRect(‘red‘,    ‘white‘,  300, 140,  100, -100, 30);drawRoundedRect(‘white‘,  ‘blue‘,   525, 140, -100, -100, 40);
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.