HTML5提供了canvas(畫布)標籤,搭配JavaScript使用,就能直接在網頁上繪製圖形。canvas本質上是一個矩形區域,開發者可以透過JavaScript控制其中的每一個像素。本文將帶你快速認識canvas標籤的用法。
1. canvas標籤說明
canvas是一個矩形區域,擁有width與height兩個屬性,分別代表寬與高。這兩個屬性都是可選的,也能用CSS設定,預設值為300px與150px。它在網頁中的寫法如下:
<canvas id="myCanvas" width="300" height="200" style="border:1px solid #c3c3c3;"> Your browser does not support the canvas element. </canvas>
解析:設定id是為了方便JavaScript抓取;第二行文字則是在瀏覽器不支援canvas時才會顯示的提示。
2. 偵測瀏覽器是否支援
canvas並未獲得當時所有瀏覽器的支援,因此繪圖前最好先檢查一下:
<canvas id="myCanvas" width="300" height="200" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var myCanvas = document.getElementById("myCanvas");
if (!myCanvas.getContext)
{
alert("Your browser does not support the canvas element.");
}
else
{
// do something here
}
</script>
解析:這段程式碼透過判斷getContext方法是否存在,來確認瀏覽器是否支援canvas。
3. 繪製線條與圖形
在canvas問世之前,想在網頁上畫線或圖形既麻煩又不相容。如今一切變得簡單,只要放進canvas標籤,再呼叫它對應的方法即可。先來看怎麼畫線:
<canvas id="myCanvas" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var myCanvas = document.getElementById("myCanvas");
if (!myCanvas.getContext)
{
alert("Your browser does not support the canvas element.");
}
else
{
var myContext = myCanvas.getContext("2d");
myContext.moveTo(200, 150);
myContext.lineTo(100, 100);
myContext.lineTo(200, 50);
myContext.strokeStyle = "#FF0000";
myContext.lineWidth = 4;
myContext.stroke();
}
</script>
解析:先在頁面放一個canvas並設定邊框;接著偵測支援情況,若支援就呼叫getContext取得繪圖上下文(有點像VC裡的GDI繪圖),目前只提供2D,未來可望支援3D。moveTo把當前位置移到指定座標,lineTo朝指定點畫直線,strokeStyle指定線條顏色(此例為紅色),lineWidth設定粗細(此例4px),最後呼叫stroke才會真正把線條畫出來。
再來看繪製圖形:
<canvas id="myCanvas" width="300" height="200" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var myCanvas = document.getElementById("myCanvas");
if (!myCanvas.getContext)
{
alert("Your browser does not support the canvas element.");
}
else
{
var myContext = myCanvas.getContext("2d");
myContext.fillStyle = "rgb(200,0,0)";
myContext.fillRect(10, 10, 55, 50);
myContext.fillStyle = "rgba(0, 0, 200, 0.5)";
myContext.fillRect(30, 30, 55, 50);
myContext.fillRect(100, 0, 150, 50);
myContext.strokeRect(100, 60, 150, 50);
myContext.clearRect(130, 10, 90, 30);
}
</script>
解析:fillStyle用來設定填色與透明度,設為rgb(200,0,0)代表不透明的紅色,設為rgba(0,0,200,0.5)則是藍色、透明度50%。fillRect畫出沒有邊框、只有填色的矩形,四個參數依序是左上角座標、寬與高。strokeRect則畫出帶邊框的矩形,參數同上。clearRect會清除指定矩形區域,讓該處不留下任何線條,參數同樣是座標與寬高。
4. 繪製路徑
路徑是畫出自訂圖形的好方法。用beginPath()開始繪製,接著就能畫直線、曲線,完成後以fill()填色、stroke()描邊,最後用closePath()結束路徑。範例如下:
<canvas id="myCanvas" width="300" height="200" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
var myCanvas = document.getElementById("myCanvas");
if (!myCanvas.getContext)
{
alert("Your browser does not support the canvas element.");
}
else
{
var myContext = myCanvas.getContext("2d");
myContext.fillStyle = '#0000ff';
myContext.strokeStyle = '#ff0000';
myContext.lineWidth = 2;
myContext.beginPath();
myContext.moveTo(30, 30);
myContext.lineTo(150, 30);
myContext.lineTo(150, 120);
myContext.lineTo(90, 100);
myContext.lineTo(120, 60);
myContext.lineTo(30, 30);
myContext.fill();
myContext.stroke();
myContext.closePath();
}
</script>
解析:取得渲染上下文後,先設定填色、邊框色與邊框寬度;呼叫beginPath()開始繪製路徑;用lineTo()畫出一個閉合的自訂圖形;再以fill()填色、stroke()描邊;最後用closePath()結束路徑繪製。