基本用法:
必須先設置其width,height屬性,指定可以繪圖的區域大小,如果不設置樣式將看不到該元素。
<canvas id="draw" width="500px" height="500px">Drawing Something</canvas>
要在畫布canvas上畫圖需要取得繪圖上下文,需調用getContext()方法,並傳入上下文的名字,傳入”2d”,就取得2D上下文。
一定要檢測getContext()方法。
var draw=document.getElementById("draw");
if(draw.getContext())
var context=draw.getContext("2d");
填充和描邊
var draw=document.getElementById("draw");
if(draw.getContext()){
var context=draw.getContext("2d");
context.strokeStyle="red";
context.fillStyle="blue";
}
繪製矩形(單位都是像素px)
fillRect(x,y,width,height)
strokeRect()
clearRect()
繪製路徑
beginPath();
closePath();