基于html5 canvas实现漫天飞雪效果实例


Posted in HTML / CSS onSeptember 10, 2014

本文实例讲述了基于html5 canvas实现漫天飞雪效果的方法,运行该实例可以看到很棒的下雪效果。如下图所示:

基于html5 canvas实现漫天飞雪效果实例

主要代码如下:

复制代码
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</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><div style=" background:#6b92b9; width:100%; height:2000px;" ></div>
<canvas id="canvas" style="position:fixed; top:0px;left:0px;z-index:80;pointer-events:none;"></canvas></p> <p><script>
window.onload = function(){
//canvas init
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

//canvas dimensions
var W = window.innerWidth;
var H = window.innerHeight;
canvas.width = W;
canvas.height = H;

//snowflake particles
var mp = 3000; //max particles
var particles = [];
for(var i = 0; i < mp; i++)
{
particles.push({
x: Math.random()*W, //x-coordinate
y: Math.random()*H, //y-coordinate
r: Math.random()*3+1, //radius
d: Math.random()*mp //density
})
}

//Lets draw the flakes
function draw()
{
ctx.clearRect(0, 0, W, H);

ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
/* ctx.fillStyle = "#FF0000";*/
ctx.beginPath();
for(var i = 0; i < mp; i++)
{
var p = particles[i];
ctx.moveTo(p.x, p.y);
ctx.arc(p.x, p.y, p.r, 0, Math.PI*2, true);
}
ctx.fill();
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 += Math.cos(angle+p.d) + 1 + p.r/2;
p.x += Math.sin(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: Math.random()*W, y: -10, r: p.r, d: p.d};
}
else
{
//If the flake is exitting from the right
if(Math.sin(angle) > 0)
{
//Enter fromth
particles[i] = {x: -5, y: Math.random()*H, r: p.r, d: p.d};
}
else
{
//Enter from the right
particles[i] = {x: W+5, y: Math.random()*H, r: p.r, d: p.d};
}
}
}
}
}

//animation loop
setInterval(draw, 15);
}
</script>
</body>
</html>

代码分析如下:

这行代码改变雪花半径大小:

复制代码
代码如下:
r: Math.random()*3+1, //radius

这行代码改变雪花下落速度:

复制代码
代码如下:
setInterval(draw, 15);

这行值改变雪花密度:

复制代码
代码如下:
var mp = 3000; //max particles

相信本文所述对大家的html5 WEB程序设计有一定的借鉴价值。

HTML / CSS 相关文章推荐
使用CSS3的rem属性制作响应式页面布局的要点解析
May 24 HTML / CSS
使用CSS3的::selection改变选中文本颜色的方法
Sep 29 HTML / CSS
利用CSS3把图片变成灰色模式的实例代码
Sep 06 HTML / CSS
利用CSS3实现单选框动画特效示例代码
Sep 26 HTML / CSS
使用CSS3实现SVG路径描边动画效果入门教程
Oct 21 HTML / CSS
CSS3 真的会替代 SCSS 吗
Mar 09 HTML / CSS
使用Html5、CSS实现文字阴影效果
Jan 17 HTML / CSS
html5 自定义播放器核心代码
Dec 20 HTML / CSS
简单介绍HTML5中的文件导入
May 08 HTML / CSS
HTML5拖放API实现自动生成相框功能
Apr 07 HTML / CSS
html5调用摄像头实例代码
Jun 28 HTML / CSS
css常用字体属性与背景属性介绍
Feb 28 HTML / CSS
html5中的input新属性range使用记录
Sep 05 #HTML / CSS
让IE下支持Html5的placeholder属性的插件
Sep 02 #HTML / CSS
html5摇一摇代码优化包括DeviceMotionEvent等等
Sep 01 #HTML / CSS
Html5 FileReader实现即时上传图片功能实例代码
Sep 01 #HTML / CSS
html5定位获取当前位置并在百度地图上显示
Aug 22 #HTML / CSS
HTML5 transform三维立方体实现360无死角三维旋转效果
Aug 22 #HTML / CSS
html5 更新图片颜色示例代码
Jul 29 #HTML / CSS
You might like
apache+php+mysql安装配置方法小结
2010/08/01 PHP
php数组编码转换示例详解
2014/03/11 PHP
PHP获取youku视频真实flv文件地址的方法
2014/12/23 PHP
PHP pthreads v3下同步处理synchronized用法示例
2020/02/21 PHP
Aliyun Linux 编译安装 php7.3 tengine2.3.2 mysql8.0 redis5的过程详解
2020/10/20 PHP
js自执行函数的几种不同写法的比较
2012/08/16 Javascript
jQuery Animation实现CSS3动画示例介绍
2013/08/14 Javascript
禁止iframe页面的所有js脚本如alert及弹出窗口等
2014/09/03 Javascript
JS原型链怎么理解
2016/06/27 Javascript
Input文本框随着输入内容多少自动延伸的实现
2017/02/15 Javascript
详解Node项目部署到云服务器上
2017/07/12 Javascript
JavaScript创建对象方法实例小结
2018/09/03 Javascript
微信小程序实现时间预约功能
2018/11/27 Javascript
微信小程序HTTP接口请求封装代码实例
2019/09/05 Javascript
laydate只显示时分 不显示秒的功能实现方法
2019/09/28 Javascript
jQuery实现移动端图片上传预览组件的方法分析
2020/05/01 jQuery
js cavans实现静态滚动弹幕
2020/05/21 Javascript
微信小程序绘制半圆(弧形)进度条
2020/11/18 Javascript
在Python3中使用asyncio库进行快速数据抓取的教程
2015/04/02 Python
python使用递归的方式建立二叉树
2019/07/03 Python
详解Python Matplotlib解决绘图X轴值不按数组排序问题
2019/08/05 Python
Python pickle模块实现对象序列化
2019/11/22 Python
NumPy排序的实现
2020/01/21 Python
Python对象的属性访问过程详解
2020/03/05 Python
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧装饰器信号与槽详细使用方法与实例
2020/03/06 Python
python随机模块random的22种函数(小结)
2020/05/15 Python
使用Python绘制台风轨迹图的示例代码
2020/09/21 Python
详解python中的三种命令行模块(sys.argv,argparse,click)
2020/12/15 Python
澳大利亚制造的蜡烛和扩散器:Glasshouse Fragrances
2018/05/20 全球购物
三年级科学教学反思
2014/01/29 职场文书
期末评语大全
2014/05/04 职场文书
安全口号大全
2014/06/21 职场文书
优秀三好学生事迹材料
2014/08/31 职场文书
领导班子作风建设年个人整改措施
2014/09/29 职场文书
交通事故协议书范本
2014/11/18 职场文书
贷款工作证明模板
2015/06/12 职场文书