Js : 元素

基本用法:
必須先設置其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();
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章