標籤: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元素,繪製圓形