DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> 關於HTML
Canvas fillRect 全解:矩形、陰影、漸層與填圖樣式
編輯:關於HTML     

<canvas> 是 HTML5 新增的標籤,和其他 DOM 物件一樣有自己的屬性、方法與事件,其中一組就是繪圖 API。要開始畫,先得取得 canvas 物件與它的 2D 內容:

var mycanvas = document.getElementById("mycanvas");
if (mycanvas == null) {
  return false;
}
var context = mycanvas.getContext("2d");

基本矩形

/* fillRect() 繪製「已填色」的矩形,預設填色為黑色。
   語法:context.fillRect(x, y, width, height)
   參數:左上角 x、y 座標,以及寬與高 */
context.fillRect(5, 10, 100, 100);

/* strokeRect() 繪製矩形(不填色),筆觸預設也是黑色。
   語法:context.strokeRect(x, y, width, height) */
context.strokeRect(110, 10, 100, 100);

填色與筆觸樣式

context.fillStyle = "red";
context.fillRect(220, 10, 100, 100);

context.strokeStyle = "red";
context.strokeRect(330, 10, 100, 100);

/* 帶透明度的寫法:alpha 越小越透明 */
context.fillStyle = "rgba(255,0,0,0.5)";
context.fillRect(440, 10, 100, 100);

context.strokeStyle = "rgba(255,0,0,0.5)";
context.strokeRect(550, 10, 100, 100);

這裡的 save() 與 restore() 是一對:save 暫存目前的畫筆狀態(填色、筆觸、陰影設定等),restore 則還原到上次 save 的狀態。養成「每次改樣式前 save、用完 restore」的習慣,可以避免樣式互相污染——這是 Canvas 繪圖最常見的 bug 來源。

陰影

context.shadowColor = "blue";   // 陰影顏色,需與 shadowBlur 搭配
context.shadowBlur = 10;        // 模糊程度,數值越大範圍越廣
context.fillStyle = "red";
context.fillRect(5, 120, 100, 100);

位移則由 shadowOffsetX 與 shadowOffsetY 控制:

  • 正值向右/向下偏移,負值向左/向上偏移。
  • 設為 0 時陰影位於元素正下方。
  • offsetX 為 10,陰影落在元素右側 10 像素處;為 -10 則在左側 10 像素。
context.shadowColor = "blue";
context.shadowBlur = 10;
context.shadowOffsetX = 30;
context.fillRect(130, 120, 100, 100);

rect 與 clearRect

context.rect(450, 120, 100, 100);  // 建立矩形路徑,需搭配 fill 或 stroke
context.fillStyle = "green";
context.fill();

/* clearRect() 清除指定矩形區域內的像素
   語法:context.clearRect(x, y, width, height) */
context.clearRect(500, 140, 120, 60);

線性漸層

/* createLinearGradient() 建立線性漸層
   語法:context.createLinearGradient(x0, y0, x1, y1)
   起點與終點決定漸層方向 */
var grd = context.createLinearGradient(5, 230, 5, 340);  // 上下方向
grd.addColorStop(0, "green");
grd.addColorStop(1, "red");
context.fillStyle = grd;
context.fillRect(5, 230, 110, 110);

addColorStop(stop, color) 用來定義色標:stop 是 0.0 到 1.0 之間的數值,表示該顏色在漸層中的位置。可以呼叫多次以建立多段漸層;若不呼叫任何一次,漸層完全不會顯示。

var grd = context.createLinearGradient(120, 230, 230, 230);  // 水平方向
grd.addColorStop(0, "red");
grd.addColorStop(1, "green");

/* 三色漸層 */
var grd = context.createLinearGradient(5, 230, 5, 340);
grd.addColorStop(0, "red");
grd.addColorStop(0.3, "green");
grd.addColorStop(1, "black");

圖樣填色

/* createPattern() 在指定方向重複同一元素
   語法:context.createPattern(image, "repeat|repeat-x|repeat-y|no-repeat")
   repeat    預設,水平與垂直雙向重複
   repeat-x  僅水平重複
   repeat-y  僅垂直重複
   no-repeat 只顯示一次
   來源可以是 img、video 或另一個 canvas */
var img = document.getElementById("icon");
var pattern = context.createPattern(img, "repeat");
context.fillStyle = pattern;
context.fillRect(570, 230, 110, 110);
context.fillRect(0, 350, 110, 110);

要注意圖樣的座標原點是畫布左上角,而非填繪的矩形左上角——這解釋了為什麼兩個不同位置的矩形會顯示出同一張圖的不同部分。

完整範例

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>Canvas fillRect 範例</title>
<script type="text/javascript">
window.onload = function () {
  var mycanvas = document.getElementById("mycanvas");
  if (mycanvas == null) { return false; }
  var context = mycanvas.getContext("2d");

  context.save();
  context.fillRect(5, 10, 100, 100);          // 預設黑色
  context.restore();

  context.save();
  context.strokeRect(110, 10, 100, 100);      // 預設黑色筆觸
  context.restore();

  context.save();
  context.fillStyle = "red";
  context.fillRect(220, 10, 100, 100);
  context.restore();

  context.save();
  context.strokeStyle = "red";
  context.strokeRect(330, 10, 100, 100);
  context.restore();

  context.save();
  context.fillStyle = "rgba(255,0,0,0.5)";    // 半透明填色
  context.fillRect(440, 10, 100, 100);
  context.restore();

  context.save();
  context.strokeStyle = "rgba(255,0,0,0.5)";
  context.strokeRect(550, 10, 100, 100);
  context.restore();

  context.save();
  context.fillStyle = "red";
  context.shadowColor = "blue";               // 陰影需與模糊度搭配
  context.shadowBlur = 10;
  context.fillRect(5, 120, 100, 100);
  context.restore();

  context.save();
  context.fillStyle = "red";
  context.shadowColor = "blue";
  context.shadowBlur = 10;
  context.shadowOffsetX = 30;                 // 陰影水平偏移
  context.fillRect(130, 120, 100, 100);
  context.restore();

  context.save();
  context.fillStyle = "red";
  context.shadowColor = "blue";
  context.shadowBlur = 10;
  context.shadowOffsetY = 30;                 // 陰影垂直偏移
  context.fillRect(300, 150, 100, 100);
  context.restore();

  context.save();
  context.rect(450, 120, 100, 100);           // 路徑需搭配 stroke
  context.strokeStyle = "blue";
  context.stroke();
  context.restore();

  context.save();
  context.rect(560, 120, 100, 100);
  context.fillStyle = "green";
  context.fill();
  context.restore();

  context.clearRect(500, 140, 120, 60);       // 清除指定區域

  context.save();
  var grd = context.createLinearGradient(5, 230, 5, 340);
  grd.addColorStop(0, "green");
  grd.addColorStop(1, "red");
  context.fillStyle = grd;
  context.fillRect(5, 230, 110, 110);
  context.restore();

  context.save();
  var grd = context.createLinearGradient(120, 230, 230, 230);
  grd.addColorStop(0, "red");
  grd.addColorStop(1, "green");
  context.fillStyle = grd;
  context.fillRect(120, 230, 110, 110);
  context.restore();

  context.save();
  var grd = context.createLinearGradient(5, 230, 5, 340);
  grd.addColorStop(0, "red");
  grd.addColorStop(0.3, "green");
  grd.addColorStop(1, "black");
  context.fillStyle = grd;
  context.fillRect(450, 230, 110, 110);
  context.restore();

  context.save();
  var img = document.getElementById("icon");
  var pattern = context.createPattern(img, "repeat");
  context.fillStyle = pattern;
  context.fillRect(570, 230, 110, 110);
  context.fillRect(0, 350, 110, 110);
  context.restore();
}
</script>
</head>
<body>
<img src="images/1.gif" id="icon" />
<canvas width="900" height="600" id="mycanvas" style="border:1px solid blue;"></canvas>
</body>
</html>

延伸參考:W3School Canvas 參考手冊。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved