HTML5新增了一個<canvas.../>屬性。該元素自身並不繪制圖形,只是相當於一張空畫布。如果開發者需要向<canvas.../>上繪制圖形則必須使用JavaScript腳本進行繪制。
為了向<canvas.../>元素上繪圖,必須經過如下3步。
獲取<canvas.../>元素對應的DOM對象,這是一個Canvas對象。
調用Canvas對象的getContext()方法,該方法返回一個CanvasRenderingContext2D對象,該對象即可繪制圖形。
調用CanvasRenderingContext2D對象的方法繪圖。
繪制幾何圖形:
CanvasRenderingContext2D只提供了兩個方法來繪制幾何圖形:
fillRect(double x,double y,double width,double height):填充一個矩形區域。
strokeRect(double x,double y,double width,double height):繪制一個矩形邊框。
注:此處的(x,y)坐標相對於畫布左上角頂點定位,左上角頂點默認坐標為(0,0),x軸向右為正方向,y軸向下為正方向。
下面程序使用這兩個方法來繪制幾個簡單的矩形:
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 繪制矩形 </title>
</head>
<body>
<h2> 繪制矩形 </h2>
<canvas id="mc" width="400" height="280"
style="border:1px solid black"></canvas>
<script type="text/javascript">
// 獲取canvas元素對應的DOM對象
var canvas = document.getElementById('mc');
// 獲取在canvas上繪圖的CanvasRenderingContext2D對象
var ctx = canvas.getContext('2d');
// 設置填充顏色
ctx.fillStyle = '#f00';
// 填充一個矩形
ctx.fillRect(30 , 20 , 120 , 60);
// 設置線條顏色
ctx.strokeStyle = "#00f";
// 設置線條寬度
ctx.lineWidth = 10;
// 繪制一個矩形邊框
ctx.strokeRect(30 , 130 , 120 , 60);
// 設置線條顏色
ctx.strokeStyle = "#0ff";
// 設置線條連接風格
ctx.lineJoin = "round";
// 繪制一個矩形邊框
ctx.strokeRect(80 , 160 , 120 , 60);
// 設置線條顏色
ctx.strokeStyle = "#f0f";
// 設置線條連接風格
ctx.lineJoin = "bevel";
// 繪制一個矩形邊框
ctx.strokeRect(130 , 190 , 120 , 60);
</script>
</body>
</html>
效果圖:
使用路徑繪制圓角矩形:
正如前面所提,CanvasRenderingContext2D對象只提供了兩個繪制矩形的方法,並沒有直接提供繪制圖形,橢圓等幾何圖形的方法,為了在Canvas上繪制更復雜的圖形,必須在Canvas上啟用路徑,借助於路徑來繪制圖形。
在Canvas上使用路徑,可按如下步驟進行:
beginPath():丟棄任何當前定義的路徑並且開始一條新的路徑。它把當前的點設置為 (0,0)。
closePath() :方法創建從當前點到開始點的路徑。
調用CanvasRenderingContext2D對象的beginPath()方法開始定義路徑。
調用CanvasRenderingContext2D的各種方法添加子路徑。
調用CanvasRenderingContext2D的closePath()方法關閉路徑。
調用CanvasRenderingContext2D的fill()或stroke()方法來填充路徑或繪制路徑邊框。
我們接下來調用調用CanvasRenderingContext2D提供的幾個方法來繪制一個圓角矩形:
arcTo(double x1,double y1,double x2,double y2,double radius):向Canvas的當前路徑上添加一段弧,確定這段弧的方式是:假設從當前點到P1(x1,y1)繪制一條線條,再從P1(x1,y1)到P2(x2,y2)繪制一條線條,arcTo則繪制一段同時與上面兩條線條相切,且半徑為radius的圓弧。
lineTo(double x,double y):把Canvas的當前路徑從當前結束點連接到x,y對應的點。
moveTo(double x,double y):把Canvas的當前路徑的結束點移動到x,y對應的點。
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> arcTo示意 </title>
</head>
<body>
<h2> arcTo示意 </h2>
<canvas id="mc" width="400" height="280"
style="border:1px solid black"></canvas>
<script type="text/javascript">
/*
該方法負責繪制圓角矩形
x1、y2:是圓角矩形左上角的坐標。
width、height:控制圓角舉行的寬、高
radius:控制圓角矩形的四個圓角的半徑
*/
function createRoundRect(ctx , x1 , y1 , width , height , radius)
{
ctx.beginPath();
// 移動到左上角
ctx.moveTo(x1 + radius , y1);
// 添加一條連接到右上角的線段
ctx.lineTo(x1 + width - radius, y1);
// 添加一段圓弧
ctx.arcTo(x1 + width , y1, x1 + width, y1 + radius, radius);
// 添加一條連接到右下角的線段
ctx.lineTo(x1 + width, y1 + height - radius);
// 添加一段圓弧
ctx.arcTo(x1 + width, y1 + height , x1 + width - radius
, y1 + height , radius);
// 添加一條連接到左下角的線段
ctx.lineTo(x1 + radius, y1 + height);
// 添加一段圓弧
ctx.arcTo(x1, y1 + height , x1 , y1 + height - radius , radius);
// 添加一條連接到左上角的線段
ctx.lineTo(x1 , y1 + radius);
// 添加一段圓弧
ctx.arcTo(x1 , y1 , x1 + radius , y1 , radius);
ctx.closePath();
}
// 獲取canvas元素對應的DOM對象
var canvas = document.getElementById('mc');
// 獲取在canvas上繪圖的CanvasRenderingContext2D對象
var ctx = canvas.getContext('2d');
ctx.lineWidth = 3;
createRoundRect(ctx , 30 , 30 , 200 , 100 , 20);
ctx.stroke();
</script>
</body>
</html>
效果圖:
