DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
24 個 Canvas 基礎知識總整理,開發時隨時查閱
編輯:HTML5詳解     

Canvas 的 API 數量不少,但真正高頻使用的其實就是那幾十個。這裡把最常用到的 24 個知識點整理成一份速查表,分成矩形、路徑、文字、變換、圖像、漸層等區塊,寫作的時候可以直接對照。

基礎繪圖與樣式(1–8)

  1. 填滿矩形 fillRect(x, y, width, height)
  2. 矩形邊框 strokeRect(x, y, width, height)
  3. 擦除矩形 clearRect(x, y, width, height)
  4. 填充樣式 fillStyle = "red",可為顏色、漸層或影像
  5. 描邊樣式 strokeStyle = "red"
  6. 線條寬度 lineWidth = 4
  7. 線條末端形狀 lineCap = "butt":butt(對接)/round(圓頭)/square(方頭),預設 butt
  8. 線條相交樣式 lineJoin = "miter":miter(尖角)/round(圓角)/bevel(斜角),預設 miter

路徑繪製(9–18)

  1. 開始路徑 beginPath()
  2. 結束路徑 closePath(),會自動補上一條回到起點的線段
  3. 繪製圓弧 arc(x, y, radius, startAngle, endAngle, anticlockwise),角度以「弧度」為單位
  4. 繪製切線弧 arcTo(x1, y1, x2, y2, radius):從上一點畫弧到 (x2, y2),並以指定半徑通過 (x1, y1)
  5. 移動游標 moveTo(x, y):只移動位置,不產生線條
  6. 畫直線 lineTo(x, y):從上一點連一條直線過來
  7. 二次貝茲曲線 quadraticCurveTo(cx, cy, x, y),一个控制點
  8. 三次貝茲曲線 bezierCurveTo(cx1, cy1, cx2, cy2, x, y),兩個控制點
  9. 矩形路徑 rect(x, y, width, height):產生的是矩形「路徑」,可後續填滿或描邊,與 fillRect 直接成像不同
  10. 繪製文字:
    • 填充文字:fillText("hello", x, y, maxWidth)
    • 文字描邊:strokeText("hello", x, y, maxWidth)
    • 文字樣式:font = "bold 14px Arial"
    • 水平對齊:textAlign = 'start'(start/end/left/right/center,預設 start)
    • 垂直對齊:textBaseline = 'alphabetic'(top/hanging/middle/alphabetic/ideographic/bottom,預設 alphabetic)
    • 測量寬度:context.measureText(text).width

座標變換(19)

  1. 變換
    • rotate(angle):繞原點旋轉,參數為弧度。等效寫法:transform(Math.cos(a), Math.sin(a), -Math.sin(a), Math.cos(a), 0, 0),其中 a 為弧度。
    • scale(x, y):縮放,等效 transform(x, 0, 0, y, 0, 0)
    • translate(x, y):把座標原點移到 (x, y),等效 transform(1, 0, 0, 1, x, y)
    • transform(a, b, c, d, e, f):以矩陣「相乘」的方式疊加變換
    • setTransform(a, b, c, d, e, f):先把矩陣重置為單位矩陣,再套用變換

角度與弧度換算永遠是:弧度 = 角度 × Math.PI / 180。

圖形合成(20)

透過 globalCompositeOperation 可以控制新圖形與既有圖形如何合成:

context.fillStyle = "blue";
context.fillRect(10, 10, 100, 100);
context.globalCompositeOperation = 'lighter';
context.fillStyle = "red";
context.arc(110, 60, 50, 0, Math.PI * 2, false);
context.fill();

可選值:

  • source-over(預設):新圖形疊在舊圖形之上
  • destination-over:新圖形畫在舊圖形之下
  • source-in:只顯示新圖形與舊圖形重疊的部分
  • destination-in:只保留舊圖形與新圖形重疊的部分
  • source-out:只顯示新圖形中「沒有」與舊圖形重疊的部分
  • destination-out:只保留舊圖形中沒有與新圖形重疊的部分
  • source-atop:新圖形的重疊區 + 未被覆蓋的舊圖形
  • destination-atop:舊圖形被新圖形覆蓋的區 + 新圖形的其餘部分
  • lighter:兩者都畫,重疊處做加色處理
  • xor:只畫不重疊的部分,重疊處變透明
  • copy:只畫新圖形

陰影(21)

context.shadowOffsetX = 10;                    // 水平位移
context.shadowOffsetY = 10;                    // 垂直位移
context.shadowColor = 'rgba(100,100,100,0.5)'; // 陰影顏色(含透明度)
context.shadowBlur = 7;                        // 模糊範圍

圖像的繪製、平鋪與裁剪(22)

context.drawImage(image, x, y);
context.drawImage(image, x, y, w, h);
context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh);
// sx, sy, sw, sh:來源圖被複製區域的起始座標與尺寸
// dx, dy, dw, dh:貼到畫布上的目標座標與尺寸

context.createPattern(image, type); // 平鋪,type 可為 no-repeat / repeat-x / repeat-y / repeat
context.clip();                     // 依目前路徑設定裁剪範圍

完整範例,示範平鋪、局部放大與裁剪:

var image = new Image();          // 建立 Image 物件
image.src = "../images/wukong.gif";

var test = context.createPattern(image, 'repeat-y');
context.fillStyle = test;
context.fillRect(0, 0, 400, 400);

// 用 onload 確保圖片載入完成後才繪製。
// 對於較大的網路圖檔,這樣可以邊載入邊畫,避免第一次渲染時什麼都看不到。
image.onload = function () {
  drawImg(context, image);
};

function drawImg(context, image) {
  context.drawImage(image, 10, 10, 125, 125);              // 原始尺寸
  context.drawImage(image, 20, 0, 90, 100, 150, 10, 125, 125); // 局部放大
  context.rect(20, 20, 80, 80);                            // 設定裁剪路徑
  context.clip();
  context.drawImage(image, 0, 0, 200, 200);                // 只在裁剪範圍內作畫
}

狀態保存與還原(23)

context.save() 會把目前的狀態堆進堆疊,context.restore() 則把上一個狀態取出來。要注意的是:保存的只是「繪圖設定與變換矩陣」,並不包含已經畫上去的像素內容。

常被保存的屬性包含:fillStyle、font、globalAlpha、globalCompositeOperation、lineCap、lineJoin、lineWidth、miterLimit、shadowBlur、shadowColor、shadowOffsetX、shadowOffsetY、strokeStyle、textAlign、textBaseline。

典型使用時機:

  1. 圖形或圖像需要變形,畫完想回到原本的座標系統
  2. 圖像裁剪,裁剪完要馬上復原
  3. 只是暫時改一下樣式,不想污染後續的繪圖設定

漸層(24)

var g  = context.createLinearGradient(xStart, yStart, xEnd, yEnd);   // 線性漸層
var g1 = context.createRadialGradient(xStart, yStart, rStart, xEnd, yEnd, rEnd); // 放射狀漸層

g.addColorStop(0, 'red');
g.addColorStop(1, 'green');

context.fillStyle = g;
context.fillRect(0, 0, 200, 200);

addColorStop(offset, color) 的 offset 是 0 到 1 之間的數字,代表顏色在漸層中的位置。

把這 24 個要點記熟,基本上一般的 Canvas 繪圖需求都能應付;剩下的進階話題(像素操作、WebGL、離屏畫布)都是在此之上的延伸。

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