Canvas2D基礎

Canvas2D基礎

Canvas2D基礎

什麼是Canvas

<canvas>是H5中最受歡迎的元素,在頁面上劃出一片區域,利用JS在上面畫出圖形,起初由Apple公司提出,各大瀏覽器廠商也對其做了不同程度上的實現。canvas中規定了了2D context和3D context(WebGL),目前絕大部分瀏覽器支持2D context。WebGL的發展還比較緩慢。

基本使用

1、toDataURL() 將畫好的圖像輸出爲圖片

//get data URI of the image
var imgURI = drawing.toDataURL("image/png");
//display the image
var image = document.createElement("img");
image.src = imgURI;
document.body.appendChild(image);

2、原點是基於canvas元素左上角
3、2D Context的兩個基本繪畫操作 fill and stroke

Rectangles(矩形)

1、fillRect()

context.fillRect(10, 10, 50, 50);
//draw a blue rectangle that’s semi-transparent
context.fillStyle = "rgba(0,0,255,0.5)";
context.fillRect(30, 30, 50, 50);

2、strokeRect()

//draw a red outlined rectangle
context.strokeStyle = "#ff0000";
context.strokeRect(10, 10, 50, 50);
//draw a blue outlined rectangle that’s semi-transparent
context.strokeStyle = "rgba(0,0,255,0.5)";
context.strokeRect(30, 30, 50, 50);

3、clearRect()

//draw a red rectangle
context.fillStyle = "#ff0000";
context.fillRect(10, 10, 50, 50);
//draw a blue rectangle that’s semi-transparent
context.fillStyle = "rgba(0,0,255,0.5)";
context.fillRect(30, 30, 50, 50);
//clear a rectangle that overlaps both of the previous rectangles
context.clearRect(40, 40, 10, 10);

Drawing Paths

1、如何畫一個錶盤

var drawing = document.getElementById("drawing");
//make sure <canvas> is completely supported
if(drawing.getContext) {
    var context = drawing.getContext("2d");
    //start the path
    context.beginPath();
    //draw outer circle
    context.arc(100, 100, 99, 0, 2 * Math.PI, false);
    //draw inner circle
    context.moveTo(194, 100); //將光標移動這個內圓繪製的起點上
    context.arc(100, 100, 94, 0, 2 * Math.PI, false);
    //draw minute hand
    context.moveTo(100, 100);
    context.lineTo(100, 15); // 從最後繪製點到(100,15)繪製一條線
    //draw hour hand
    context.moveTo(100, 100);
    context.lineTo(35, 100);
    //stroke the path
    context.stroke();
}

2、判斷一個座標點是否在繪製路線上
context.isPointInPath(100, 100) // true

Drawing Text

1、fillText() 、  strokeText() 後者很少用
2、3個屬性font、 textAlign、 textBaseline
3、Demo

context.font = "bold 14px Arial";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText("12", 100, 20);
//start-aligned
context.textAlign = "start";
context.fillText("12", 100, 40);
//end-aligned
context.textAlign = "end";
context.fillText("12", 100, 60);

4、measureText()的使用 可以度量文字大小

// 如果文字的長度大於140,就將字體縮小
var fontSize = 100;
context.font = fontSize + "px Arial";
while(context.measureText("Hello world!").width > 140) {
    fontSize--;
    context.font = fontSize + "px Arial";
}
context.fillText("Hello world!", 10, 10);
context.fillText("Font size is " + fontSize + "px", 10, 50);

Transformations

  1. 圖像變換方法
    rotate(angle)
    scale(scaleX, scaleY)
    translate(x, y)
    transform(m1_1, m1_2, m2_1, m2_2, dx, dy)
    setTransform(m1_1, m1_2, m2_1, m2_2, dx, dy)

  2. 可用translate重新定義原點的位置,
    在畫圓的時候,可將原點位置移動圓心位置,這樣在畫圓定位過程中比較方便

  3. context.rotate 注意是將座標軸進行旋轉

Drawing Images 圖片繪製

  1. 從當前canvasd的座標(10,10)的位置將圖片插入進來(注意,如果圖片在canvas中裝不下,那麼圖片是插入不進來的)
var image = document.images[0];
 context.drawImage(image, 10, 10);
  1. 指定區域內插入 -- 執行在從(10,10)開始,寬90,高90的區域內繪製圖像
context.drawImage(image, 10, 10,90,90)
  1. 將圖像上指定區域的部分 插入 canvas中的指定區域(相當於剪切原圖像上的一部分到canvas中)
//從圖像上(0,10)開始,寬150,高385的區域插入到 canvas中從(0,100)開始,寬40,高60的區域內
context.drawImage(image, 0, 10, 150, 385, 0, 100, 40, 60);

  1. 注意,如果插入進來的圖片是在別的域名下的,會報錯

shadows 加陰影

DEMO:

var drawing = document.getElementById("drawing");
var context = drawing.getContext("2d");
context.shadowOffsetX = 5;
context.shadowOffsetY = 5;
context.shadowBlur = 4;
context.shadowColor = "rgba(0, 0, 0, 0.5)";
//draw a red rectangle
context.fillStyle = "#ff0000";
context.fillRect(10, 10, 50, 50);
//draw a blue rectangle
context.fillStyle = "rgba(0,0,255,1)";
context.fillRect(30, 30, 50, 50);

Gradients 漸變色

  1. 線性漸變
var gradient = context.createLinearGradient(30, 30, 80, 80);
gradient.addColorStop(0, "white");
gradient.addColorStop(1, "black");
context.fillStyle = gradient;
context.fillRect(30, 30, 50, 50);
  1. 徑項漸變
var gradient = context.createRadialGradient(55, 55, 10, 55, 55, 30);
gradient.addColorStop(0, "white");
gradient.addColorStop(1, "black");
context.fillStyle = gradient;
context.fillRect(30, 30, 50, 50);

Patterns 模式

  1. 即圖像的重複模式 ,跟css一樣 “repeat”, “repeat-x”, “repeat-y”, no-repeat”
var context = drawing.getContext("2d");
var image = document.images[0],
    pattern = context.createPattern(image, "repeat");
//draw a rectangle
context.fillStyle = pattern;  // 圖像repeat模式仍然是從(0,0)開始的
context.fillRect(30, 0, 150, 150); // 這裏的意思是繪製矩形,並讓圖像repeat模式在該矩形區域顯示,並不是說 圖片repeat是從繪製該矩形的起點開始的,漸變也是如此

Working with Image Data 圖像原始數據

可以用來做濾鏡效果,詳細可看  www.html5rocks.com/en/tutorials/canvas/imagefilters/

Compositing 合成

兩個圖像之間如何糾纏的,這裏舉一個例子,其他雷同

var drawing = document.getElementById("drawing");
var context = drawing.getContext("2d");
//draw a red rectangle
context.fillStyle = "#ff0000";
context.fillRect(10, 10, 50, 50);
//change the global alpha
context.globalCompositeOperation = "xor";
//draw a blue rectangle
context.fillStyle = "rgba(0,0,255,1)";
context.fillRect(30, 30, 50, 50);

總結

  1. canvas內容就是上面這些了,就好比設計師能用的顏色就那麼多,但是將顏色,圖形組合起來,創意真的是無限的,canvas也是如此。
  2. webGL是一個 對OpenGL ES 2.0瀏覽器實現接口,用於3D繪畫,生產階段最好不要用,可用於實驗階段。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章