這篇文章主要介紹了js+html5實(shí)現(xiàn)canvas繪制橢圓形圖案的方法,涉及html5圖形繪制的基礎(chǔ)技巧,感興趣的朋友可以參考一下
本文實(shí)例講述了js+html5實(shí)現(xiàn)canvas繪制橢圓形圖案的方法,HTML5 canvas 沒(méi)有畫(huà)橢圓的方法,以下代碼可以畫(huà)出橢圓,分享給大家供大家參考,具體實(shí)現(xiàn)方法如下:
1、在一個(gè)隱式的畫(huà)布 (將 其 CSS 定義成:display:none; ) 上畫(huà)園。
2、將隱式畫(huà)布的影像,以不同的寬高比值,畫(huà)在另一個(gè)顯式的畫(huà)布,以使園變成橢圓。
3、進(jìn)而,加進(jìn)動(dòng)畫(huà)功能。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>測(cè)試顏色背景</title>
<script>
var ticker=0;
var col = new Array("#000000","#A52A2A","#B8860B","pink","green","yellow","red","orange","#BB008B","#8B0000");
function drawBackground(){
var canvasHide=document.getElementById("hide"); //隱藏的畫(huà)布
var g=canvasHide.getContext("2d"); //找出隱藏畫(huà)布 hide 的畫(huà)筆 g
g.clearRect(0,0,1200,800); //清理隱藏畫(huà)布
var i=0;
do { //畫(huà) 不同顏色 依次同心發(fā)散的園
g.beginPath();
var grd=g.createRadialGradient(300,300,300-i*25, 300,300,265-i*25);
grd.addColorStop(0,col[(0+i+ticker)%col.length]);
grd.addColorStop(1,col[(1+i+ticker)%col.length]);
g.fillStyle=grd;
g.arc(300,300,300-i*25,0,2*Math.PI);
g.fill();
i++;
} while(i<11);
//找出顯式畫(huà)布 myCanvas 的畫(huà)筆 gg
var gg=document.getElementById("myCanvas").getContext("2d");
gg.clearRect(0,0,myCanvas.width,myCanvas.height); //清理顯式畫(huà)布
/* 將隱式畫(huà)布 hide 的園形圖像,
* 以 寬 600, 高 300 的比例,
* 畫(huà)到顯式畫(huà)布 myCanvas,
* 結(jié)果,隱式畫(huà)布 hide 的園形圖像,在顯式畫(huà)布 myCanvas 上 成了橢圓
*/
gg.drawImage(canvasHide,0,0,600,300);
ticker++;
}
function preperation(){
setInterval('drawBackground()',1000);
}
</script>
<style>
#myCanvas{
position:absolute;
left:0px;
top:0px;
}
#hide{
display:none;
}
</style>
</head>
<body onLoad="preperation()">
<canvas id="myCanvas" width="600" height="400" ></canvas>
<canvas id="hide" width="600" height="600" ></canvas>
</body>
</html>
希望本文所述對(duì)大家的web程序設(shè)計(jì)有所幫助。