基於html5 canvas實現漫天飛雪效果的方法
導語:基於html5 canvas實現漫天飛雪效果的方法,你懂嗎?下面的`是本站小編為大家蒐集的基於html5 canvas實現漫天飛雪效果的方法,運行該實例可以看到很棒的下雪效果。如下圖所示:
主要代碼如下:
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href=""></a>">
<html xmlns="<a href=""></a>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>漫天飛雪</title>
<style type="text/css">
* {margin: 0; padding: 0;}</p> <p>body {
/*You can use any kind of background here.*/
background: #6b92b9;
}
canvas {
display: block;
}
</style>
</head></p> <p><body></p> <p><p style=" background:#6b92b9; width:100%; height:2000px;" ></p>
<canvas id="canvas" style="position:fixed; top:0px;left:0px;z-index:80;pointer-events:none;"></canvas></p> <p><script>
ad = function(){
//canvas init
var canvas = lementById("canvas");
var ctx = ontext("2d");
//canvas dimensions
var W = rWidth;
var H = rHeight;
h = W;
ht = H;
//snowflake particles
var mp = 3000; //max particles
var particles = [];
for(var i = 0; i < mp; i++)
{
({
x: om()*W, //x-coordinate
y: om()*H, //y-coordinate
r: om()*3+1, //radius
d: om()*mp //density
})
}
//Lets draw the flakes
function draw()
{
rRect(0, 0, W, H);
Style = "rgba(255, 255, 255, 0.8)";
/* Style = "#FF0000";*/
nPath();
for(var i = 0; i < mp; i++)
{
var p = particles[i];
To(p.x, p.y);
(p.x, p.y, p.r, 0, *2, true);
}
();
update();
}
//Function to move the snowflakes
//angle will be an ongoing incremental flag. Sin and Cos functions will be applied to it to create vertical and horizontal movements of the flakes
var angle = 0;
function update()
{
angle += 0.01;
for(var i = 0; i < mp; i++)
{
var p = particles[i];
//Updating X and Y coordinates
//We will add 1 to the cos function to prevent negative values which will lead flakes to move upwards
//Every particle has its own density which can be used to make the downward movement different for each flake
//Lets make it more random by adding in the radius
p.y += (angle+p.d) + 1 + p.r/2;
p.x += (angle) * 2;
//Sending flakes back from the top when it exits
//Lets make it a bit more organic and let flakes enter from the left and right also.
if(p.x > W || p.x < 0 || p.y > H)
{
if(i%3 > 0) //66.67% of the flakes
{
particles[i] = {x: om()*W, y: -10, r: p.r, d: p.d};
}
else
{
//If the flake is exitting from the right
if((angle) > 0)
{
//Enter fromth
particles[i] = {x: -5, y: om()*H, r: p.r, d: p.d};
}
else
{
//Enter from the right
particles[i] = {x: W+5, y: om()*H, r: p.r, d: p.d};
}
}
}
}
}
//animation loop
setInterval(draw, 15);
}
</script>
</body>
</html>
代碼分析如下:
這行代碼改變雪花半徑大小:
代碼如下:
r: om()*3+1, //radius
這行代碼改變雪花下落速度:
代碼如下:
setInterval(draw, 15);
這行值改變雪花密度:
代碼如下:
var mp = 3000; //max particles
-
如何理解Javascript的caller,callee,call,apply區別
在提到上述的概念之前,首先想説説javascript中函數的隱含參數:argumentsarguments該對象代表正在執行的函數和調用它的函數的參數。[function.]arguments[n]參數function:選項。當前正在執行的Function對象的名字。n:選項。要傳遞給Function對象的從0開始的參數值...
-
javascript操作select元素案例分析
本文實例講述了javascript操作select元素的.用法。分享給大家供大家參考。具體分析如下:這裏熟悉一下js對select元素的操作,html頁面中建立一個form,其中包含一個select元素和submit按鈕。當選擇select中某一項時改變其文字,當select中所有項的文字都改變後,重新恢...
-
php程序員的簡歷模板
日子如同白駒過隙,新一輪的招聘又在朝我們招手,這時一份好的簡歷可以起到很好的.作用哦。那麼如何寫簡歷才簡練、明確呢?以下是小編收集整理的php程序員的簡歷模板,希望能夠幫助到大家。基本簡歷姓名:黃先生國籍:中國現在所在地:廣州民族:漢族户口所在地:潮州身材:170cm...
-
如何理解Javascript的caller,callee,call,apply區別
在提到上述的概念之前,首先想説説javascript中函數的隱含參數:argumentsarguments該對象代表正在執行的函數和調用它的函數的參數。[function.]arguments[n]參數function:選項。當前正在執行的Function對象的名字。n:選項。要傳遞給Function對象的從0開始的參數值...
相關文章
- HTML5 Canvas基本繪製線條教程
- java使用ConcurrentHashMap和計數器實現鎖的方法
- 英語閲讀:African Union Leaders Talk Mali, Chinese Investmen
- 頂級的HTML5 Canvas賞析
- HTML5教程:畫布Canvas基礎知識講解
- Lantern Festival,The 15th day of the 1st lunar month英語作
- 關於HTML5組件Canvas實現圖像灰度化教程
- HTML5中canvas標籤實現刮刮卡效果
- 國中英語作文:Jim Lambert’s vacation plans
- html5的canvas方法使用