HTML5火的正熱,最近有個(gè)想法也是要用到HTML的相關(guān)功能,所以也要好好學(xué)習(xí)一把。
好好看了一下Canvas的功能,感覺(jué)HTML5在客戶(hù)端交互的功能性越來(lái)越強(qiáng)了,今天看了一下Canvas繪圖,下邊是幾個(gè)實(shí)例,記下以備后用。
1、使用Canvas繪制直線(xiàn):
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = can.getContext('2d');
- cans.moveTo(20,30);//第一個(gè)起點(diǎn)
- cans.lineTo(120,90);//第二個(gè)點(diǎn)
- cans.lineTo(220,60);//第三個(gè)點(diǎn)(以第二個(gè)點(diǎn)為起點(diǎn))
- cans.lineWidth=3;
- cans.strokeStyle = 'red';
- cans.stroke();
- }
- </script>
- <body onload="pageLoad();">
- <canvas id="can" width="400px" height="300px">4</canvas>
- </body>
- </html>
這里用到的兩個(gè)API方法,moveTo和lineTo分別是線(xiàn)段的起點(diǎn)和終點(diǎn)坐標(biāo),變量為(X坐標(biāo),Y坐標(biāo)),strokeStyle、stroke分別路徑繪制樣式和繪制路徑。
2、繪制漸變線(xiàn)條
漸變線(xiàn)條就是顏色有漸變的效果,當(dāng)然漸變的樣式可以遵循路徑的方向也可以不遵循路徑的方向:
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = can.getContext('2d');
- cans.moveTo(0,0);
- cans.lineTo(400,300);
- var gnt1 = cans.createLinearGradient(0,0,400,300);//線(xiàn)性漸變的起止坐標(biāo)
- gnt1.addColorStop(0,'red');//創(chuàng)建漸變的開(kāi)始顏色,0表示偏移量,個(gè)人理解為直線(xiàn)上的相對(duì)位置,最大為1,一個(gè)漸變中可以寫(xiě)任意個(gè)漸變顏色
- gnt1.addColorStop(1,'yellow');
- cans.lineWidth=3;
- cans.strokeStyle = gnt1;
- cans.stroke();
- }
- </script>
- <body onload="pageLoad();">
- <canvas id="can" width="400px" height="300px">4</canvas>
- </body>
- </html>
3、繪制矩形或正方形:
這種矩形框如果使用HTML4只能使用后臺(tái)代碼才能生成了,現(xiàn)在HTML5提供的Canvas功能卻很容易就能繪制,所以說(shuō)HTML5的優(yōu)越性是相當(dāng)高的。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = 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>
這里使用了一個(gè)方法——fillRect()從字面意思也能知道這個(gè)就是填充一個(gè)矩形,這里的參數(shù)值得說(shuō)明一下fillRect(X,Y,Width,Height),這個(gè)和數(shù)學(xué)里的坐標(biāo)是不一樣的,具體請(qǐng)看
這里的X,Y是相對(duì)Canvas左上角的起點(diǎn)開(kāi)始的,謹(jǐn)記!!
4、繪制一個(gè)簡(jiǎn)單的矩形框
上例中講到要繪制一個(gè)矩形塊,填充了顏色,這個(gè)例子只是簡(jiǎn)單地繪制一個(gè)矩形而不實(shí)現(xiàn)填充效果。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = can.getContext('2d');
- cans.strokeStyle = 'red';
- cans.strokeRect(30,30,340,240);
- }
- </script>
- <body onload="pageLoad();">
- <canvas id="can" width="400px" height="300px">4</canvas>
- </body>
- </html>
-
這個(gè)很簡(jiǎn)單,和上例一樣,就是將fill替換成了stroke,具體詳見(jiàn)上例。
5、繪制一個(gè)線(xiàn)性漸變的矩形
漸變是填充的一種相當(dāng)不錯(cuò)的效果,結(jié)合實(shí)例2和實(shí)例3,我們可以創(chuàng)建一個(gè)漸變的矩形
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = 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>
-
不解釋了,記住fillRect(X,Y,Width,Height)就行了。
6、填充一個(gè)圓形
圓形的用途很廣,當(dāng)然也包含了橢圓。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = can.getContext('2d');
- cans.beginPath();
- cans.arc(200,150,100,0,Math.PI*2,true);
- cans.closePath();
- cans.fillStyle = 'green';//本來(lái)這里最初使用的是red,截圖一看,傻眼了,怕上街被愛(ài)國(guó)者打啊,其實(shí)你懂的~~
- cans.fill();
- }
- </script>
- <body onload="pageLoad();">
- <canvas id="can" width="400px" height="300px">4</canvas>
- </body>
- </html>
-
這里的arc方法的用法是 arc(X,Y,Radius,startAngle,endAngle,anticlockwise),意思是(圓心X坐標(biāo),圓心Y坐標(biāo),半徑,開(kāi)始角度(弧度),結(jié)束角度弧度,是否按照順時(shí)針畫(huà));
arc中各參數(shù)比較:
a、cans.arc(200,150,100,0,Math.PI,true);
c、cans.arc(200,150,100,0,Math.PI/2,true);
c、cans.arc(200,150,100,0,Math.PI/2,true);
d、cans.arc(200,150,100,0,Math.PI/2,false);
7、圓形區(qū)塊
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = 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>
這里不解釋了,和上邊的例子相同,lineWidth是控制線(xiàn)條的寬度的。
8、圓形漸變
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!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 cancans = can.getContext('2d');
- var gnt = cans.createRadialGradient(200,300,50,200,200,200);
- gnt.addColorStop(1,'red');
- gnt.addColorStop(0,'green');
- cans.fillStyle = gnt;
- cans.fillRect(0,0,800,600);
- }
- </script>
- <body onload="pageLoad();">
- <canvas id="can" width="800px" height="600px">4</canvas>
- </body>
- </html>
這里需要說(shuō)明的是createRadialGradient方法,參數(shù)有(Xstart,Ystart,radiusStart,XEnd,YEnd,radiusEnd),也就是說(shuō),它在實(shí)行漸變時(shí),使用了兩個(gè)圓,一個(gè)是原始的圓,一個(gè)是漸變式圓,其實(shí),這種通過(guò)坐標(biāo)與半徑控制的方式可以實(shí)現(xiàn)很多樣式,比如
立體圓
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- var gnt = cans.createRadialGradient(200,150,0,200,50,250);
- gnt.addColorStop(0,'red');
- gnt.addColorStop(1,'#333');