JavaScript實(shí)現(xiàn)使用Canvas繪制圖形的基本教程
由于這兩年HTML5火的正熱,所以研究了一下,最近有個(gè)想法也是要用到HTML的相關(guān)功能,所以也要好好學(xué)習(xí)一把。
好好看了一下Canvas的功能,感覺(jué)HTML5在客戶端交互的功能性越來(lái)越強(qiáng)了,今天看了一下Canvas繪圖,下邊是幾個(gè)實(shí)例,記下以備后用。
1、使用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.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分別是線段的起點(diǎn)和終點(diǎn)坐標(biāo),變量為(X坐標(biāo),Y坐標(biāo)),strokeStyle、stroke分別路徑繪制樣式和繪制路徑。
2、繪制漸變線條

漸變線條就是顏色有漸變的效果,當(dāng)然漸變的樣式可以遵循路徑的方向也可以不遵循路徑的方向:
<!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.moveTo(0,0);
cans.lineTo(400,300);
var gnt1 = cans.createLinearGradient(0,0,400,300);//線性漸變的起止坐標(biāo)
gnt1.addColorStop(0,'red');//創(chuàng)建漸變的開始顏色,0表示偏移量,個(gè)人理解為直線上的相對(duì)位置,最大為1,一個(gè)漸變中可以寫任意個(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)高的。
<!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>
這里使用了一個(gè)方法——fillRect()從字面意思也能知道這個(gè)就是填充一個(gè)矩形,這里的參數(shù)值得說(shuō)明一下fillRect(X,Y,Width,Height),這個(gè)和數(shù)學(xué)里的坐標(biāo)是不一樣的,具體請(qǐng)看

這里的X,Y是相對(duì)Canvas左上角的起點(diǎn)開始的,謹(jǐn)記!!
4、繪制一個(gè)簡(jiǎn)單的矩形框
上例中講到要繪制一個(gè)矩形塊,填充了顏色,這個(gè)例子只是簡(jiǎn)單地繪制一個(gè)矩形而不實(shí)現(xiàn)填充效果。

<!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.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,具體詳見上例。
5、繪制一個(gè)線性漸變的矩形
漸變是填充的一種相當(dāng)不錯(cuò)的效果,結(jié)合實(shí)例2和實(shí)例3,我們可以創(chuàng)建一個(gè)漸變的矩形

<!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>
不解釋了,記住fillRect(X,Y,Width,Height)就行了。
6、填充一個(gè)圓形
圓形的用途很廣,當(dāng)然也包含了橢圓。

<!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,true);
cans.closePath();
cans.fillStyle = 'green';//本來(lái)這里最初使用的是red,截圖一看,傻眼了,怕上街被愛國(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),半徑,開始角度(弧度),結(jié)束角度弧度,是否按照順時(shí)針畫);
arc中各參數(shù)比較:
a、cans.arc(200,150,100,0,Math.PI,true);

c、cans.arc(200,150,100,0,Math.PI/2,true);[/code]

c、cans.arc(200,150,100,0,Math.PI/2,true);

d、cans.arc(200,150,100,0,Math.PI/2,false);

7、圓形區(qū)塊

<!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>
這里不解釋了,和上邊的例子相同,lineWidth是控制線條的寬度的。
8、圓形漸變

<!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 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)很多樣式,比如
立體圓

var gnt = cans.createRadialGradient(200,150,0,200,50,250); gnt.addColorStop(0,'red'); gnt.addColorStop(1,'#333');
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)網(wǎng)頁(yè)每隔3秒彈出一次對(duì)話框的方法
這篇文章主要介紹了JS實(shí)現(xiàn)網(wǎng)頁(yè)每隔3秒彈出一次對(duì)話框的方法,涉及JavaScript結(jié)合時(shí)間函數(shù)遞歸調(diào)用的相關(guān)技巧,非常簡(jiǎn)單,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
JavaScript設(shè)計(jì)模式之外觀模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之外觀模式介紹,外觀模式是用于由于子系統(tǒng)或程序組成較復(fù)雜而提供的一個(gè)高層界面接口,使用客戶端更容易訪問(wèn)底層的程序或系統(tǒng)接口,需要的朋友可以參考下2014-12-12
js以對(duì)象為索引的關(guān)聯(lián)數(shù)組
在代碼邏輯中更多的是用關(guān)聯(lián)數(shù)組的方式。但即使是這樣我們也很少使用對(duì)象類型作為鍵值對(duì)的鍵名。2010-07-07
JS中數(shù)組常用的循環(huán)遍歷你會(huì)幾種
JS 遍歷數(shù)組(循環(huán)數(shù)組)的方式有多種,但你都知道嗎?下面這篇文章主要給大家介紹了關(guān)于JS中數(shù)組常用循環(huán)遍歷的相關(guān)資料,需要的朋友可以參考下2021-06-06

