HTML5 畫布
? ?<canvas></canvas>是HTML5出現的新標簽,像所有的dom對象一樣它有自己本身的屬性、方法和事件,其中就有繪圖的方法,js能夠調用它來進行繪圖 。
什么是 Canvas?
HTML5 <canvas> 元素用于圖形的繪制,通過腳本 (通常是JavaScript)來完成.
<canvas> 標簽只是圖形容器,您必須使用腳本來繪制圖形。
你可以通過多種方法使用Canva繪制路徑,盒、圓、字符以及添加圖像。
基本所有的瀏覽器都支持<canvas> 元素除了IE8及以前的版本
canvas元素繪制圖像的時候有兩種方法,分別是
??????? context.fill()//填充
??????? context.stroke()//繪制邊框
style:在進行圖形繪制前,要設置好繪圖的樣式
??????? context.fillStyle//填充的樣式
??????? context.strokeStyle//邊框樣式
? ? ? ??context.lineWidth//圖形邊框寬度
顏色的表示方式:
???????? 直接用顏色名稱:"red" "green" "blue"
???????? 十六進制顏色值: "#EEEEFF"
?????????rgb(1-255,1-255,1-255)
?????????rgba(1-255,1-255,1-255,透明度)
使用Canvas繪制直線:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>php.cn</title> </head> <style type="text/css"> canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); cans.moveTo(20,30);//第一個起點 cans.lineTo(120,90);//第二個點 cans.lineTo(220,60);//第三個點(以第二個點為起點) cans.lineWidth=3; cans.strokeStyle = 'red'; cans.stroke(); } </script> <body onload="pageLoad();"> <canvas id="can" width="400px" height="300px">4</canvas> </body> </html>
Canvas 坐標
canvas 是一個二維網格。
canvas 的左上角坐標為 (0,0)
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <style type="text/css"> canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); cans.fillStyle = 'yellow'; cans.fillRect(30,30,340,240); } </script> <body onload="pageLoad();"> <canvas id="can" width="400px" height="300px">4</canvas> </body> </html>
注意:這里使用了一個方法——fillRect()從字面意思也能知道這個就是填充一個矩形,這里的參數值得說明一下fillRect(X,Y,Width,Height),這個和數學里的坐標是不一樣的,這里的X,Y是相對Canvas左上角的起點開始的,謹記??!
圓形
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <style type="text/css"> canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); cans.beginPath(); cans.arc(200,150,100,0,Math.PI*2,false); cans.closePath(); cans.lineWidth = 5; cans.strokeStyle = 'red'; cans.stroke(); } </script> <body onload="pageLoad();"> <canvas id="can" width="400px" height="300px">4</canvas> </body> </html>
注意:這里的arc方法的用法是 arc(X,Y,Radius,startAngle,endAngle,anticlockwise),意思是(圓心X坐標,圓心Y坐標,半徑,開始角度(弧度),結束角度弧度,是否按照順時針畫);
Canvas - 漸變
漸變可以填充在矩形, 圓形, 線條, 文本等等, 各種形狀可以自己定義不同的顏色。
以下有兩種不同的方式來設置Canvas漸變:
createLinearGradient(x,y,x1,y1) - 創(chuàng)建線條漸變
createRadialGradient(x,y,r,x1,y1,r1) - 創(chuàng)建一個徑向/圓漸變
當我們使用漸變對象,必須使用兩種或兩種以上的停止顏色。
addColorStop()方法指定顏色停止,參數使用坐標來描述,可以是0至1.
使用漸變,設置fillStyle或strokeStyle的值為 漸變,然后繪制形狀,如矩形,文本,或一條線。
<!doctype html> <html> <head> <meta charset="UTF-8"> </head> <style type="text/css"> canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); var gnt1 = cans.createLinearGradient(10,0,390,0); gnt1.addColorStop(0,'red'); gnt1.addColorStop(0.5,'green'); gnt1.addColorStop(1,'blue'); cans.fillStyle = gnt1; cans.fillRect(10,10,380,280); } </script> <body onload="pageLoad();"> <canvas id="can" width="400px" height="300px">4</canvas> </body> </html>
Canvas - 文本
? ?text:要繪制的文字
???? x:文字起點的x坐標軸
???? y:文字起點的y坐標軸
???? context.font:設置字體樣式
???? context.textAlign:水平對齊方式
? ? ? ? ? start、end、right、center
???? context.textBaseline:垂直對齊方式
????????? top、hanging、middle、alphabetic、ideographic、bottom
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Canvas</title> </head> <style type="text/css"> body{margin:20px auto; padding:0; width:800px; } canvas{border:dashed 2px #CCC} </style> <script type="text/javascript"> function $$(id){ return document.getElementById(id); } function pageLoad(){ var can = $$('can'); var cans = can.getContext('2d'); cans.font = 'bold 144px consolas'; cans.textAlign = 'left'; cans.textBaseline = 'top'; cans.strokeStyle = '#DF5326'; cans.strokeText('Hello', 100, 100); cans.font = 'bold 144px arial'; cans.fillStyle = 'red'; cans.fillText('World', 300,300); } function img_click(){ var can = $$('can'); var cans = can.getContext('2d'); cans.clearRect(0,0,800,600); } </script> <body onload="pageLoad();"> <canvas id="can" width="800px" height="600px" onclick="img_click(this);"></canvas> </body> </html>