HTML5中canvas元素,繪製圓形

來源:互聯網
上載者:User

標籤:function   return   上下文   html5   false   

  HTML5中canvas元素,繪製圓形需要使用路徑,開始時要取得圖形上下文,首先使用路徑來勾勒圖形的輪廓,然後設定顏色,進行繪製。

<!DOCTYPE html><head><meta charset="UTF-8" /><script>    function draw(id) {        var canvas = document.getElementById(id);        if (canvas == null) return false;        var context = canvas.getContext(‘2d‘);        context.fillStyle = "#EEEEFF";        context.fillRect(0, 0, 400, 300);        var n = 0;        for (var i = 0; i < 10; i++) {            context.beginPath();            context.arc(i * 25, i * 25, i * 10, 0, Math.PI * 2, true);            context.closePath();            context.fillStyle = ‘rgba(255, 0, 0, 0.25)‘;            context.fill();        }    }</script></head><body onLoad="draw(‘canvas‘);"><canvas id="canvas" width="400" height="300"/></body></html>

  該方法共使用六個參數:

  

context.beginPath();

  該方法不使用參數,通過調用該方法,開始路徑的繪製。在幾次迴圈的建立路徑的過程中,每次開始建立是都要調用beginPath函數。

context.arc(x,y,radius,startAngle,endAngle,anticlockwise)

  該方法使用六個參數,x為繪製圓形的起點橫座標,y為繪製圓形圖形的起點座標,radius為繪製圓形半徑,starAngle為開始角度,endAngle為結束角度,anticlockwise為是否按順時針方向進行繪製。

  arc方法不僅可以用來繪製圓形,也可以用來繪製圓弧。

context.closePath();

  將路徑關閉後,路徑的建立工作就完成了,但還沒有真正繪製任何圖形。

  context.fillStyle = ‘rgba(255, 0, 0, 0.25)‘;  context.fill();

  使用建立好的路徑繪製圖形。

  Related posts:


HTML5中canvas元素,繪製圓形

聯繫我們

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