基于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中的prototype.bind()方法介绍
以前,你可能会直接设置self=this或者that=this等等,这样做当然也能起作用,但是使用()会更好,看上去也更专业。下面举个简单的例子:复制代码代码如下:varmyObj={specialFunction:function(){},anotherSpecialFunction:function(){},getAsyncData:function(cb){cb();}...
-
如何理解Javascript的caller,callee,call,apply区别
在提到上述的概念之前,首先想说说javascript中函数的隐含参数:argumentsarguments该对象代表正在执行的函数和调用它的函数的参数。[function.]arguments[n]参数function:选项。当前正在执行的Function对象的名字。n:选项。要传递给Function对象的从0开始的参数值...
-
JavaScript基本语法分析
一、JavaScript基本语法。(一)数据类型与变量类型。整数,小数,布局,字符串,日期时间,数组强制转换:parseInt()parseFloat()isNaN()(二)数组var数组名=newArray([长度]);//“假冒”数组th-长度a[下标]=值。a[下标](三)函数复制代码代码如下:function函数名(形参){}function...
-
网页设计很糟糕的10个原因
个好的网页设计,不仅吸引眼球,也是实用的、直观的、层级简单却足够复杂到保持用户的兴趣。但是不好的网页设计是由很多原因造成的,下面是关于网页设计很糟糕的10个原因,欢迎阅读了解!没有足够的空白空白可以说是设计中最重要的一部分。它有助于防止用户在浏览网站时...
相关文章
- HTML5教程:画布Canvas基础知识讲解
- 英语阅读:African Union Leaders Talk Mali, Chinese Investmen
- 关于HTML5组件Canvas实现图像灰度化教程
- html5的canvas方法使用
- 顶级的HTML5 Canvas赏析
- HTML5 Canvas基本绘制线条教程
- 初中英语作文:Jim Lambert’s vacation plans
- java使用ConcurrentHashMap和计数器实现锁的方法
- HTML5中canvas标签实现刮刮卡效果
- Lantern Festival,The 15th day of the 1st lunar month英语作