基于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 background-image颜色渐变的实现代码
Sep 13 HTML / CSS
css3.0 图形构成实例练习一
Mar 19 HTML / CSS
利用CSS3的特性改变文本选中时的颜色
Sep 11 HTML / CSS
详解CSS3中border-image的使用
Jul 18 HTML / CSS
浅析CSS3中鲜为人知的属性:-webkit-tap-highlight-color
Jan 12 HTML / CSS
利用HTML5+CSS3实现3D转换效果实例详解
May 02 HTML / CSS
HTML5 本地存储 LocalStorage详解
Jun 24 HTML / CSS
HTML5 与 XHTML2
Oct 17 HTML / CSS
HTML5+lufylegend实现游戏中的卷轴
Feb 29 HTML / CSS
HTML5+CSS3:3D展示商品信息示例
Jan 03 HTML / CSS
html5超简单的localStorage实现记住密码的功能实现
Sep 07 HTML / CSS
教你使用Canvas处理图片的方法
Nov 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
PHP的foreach中使用引用时需要注意的一个问题和解决方法
2014/05/29 PHP
php动态函数调用方法
2015/05/21 PHP
JavaScript 以对象为索引的关联数组
2010/05/19 Javascript
JavaScript删除数组元素的方法
2015/03/20 Javascript
js组件SlotMachine实现图片切换效果制作抽奖系统
2016/04/17 Javascript
JS时间控制实现动态效果的实例讲解
2017/07/31 Javascript
JavaScript中使用参数个数实现重载功能
2017/09/01 Javascript
ES6中Array.includes()函数的用法
2017/09/20 Javascript
记一次webpack3升级webpack4的踩坑经历
2018/06/12 Javascript
JavaScript常见事件处理程序实例总结
2019/01/05 Javascript
如何在vue里面优雅的解决跨域(路由冲突问题)
2019/01/20 Javascript
微信小程序搭建自己的Https服务器
2019/05/02 Javascript
Android 自定义view仿微信相机单击拍照长按录视频按钮
2019/07/19 Javascript
微信小程序之 catalog 切换实现解析
2019/09/12 Javascript
Vue可自定义tab组件用法实例
2019/10/24 Javascript
一看就会的vuex实现登录验证(附案例)
2020/01/09 Javascript
Vue基本指令实例图文讲解
2021/02/25 Vue.js
Python2.x版本中maketrans()方法的使用介绍
2015/05/19 Python
windows7 32、64位下python爬虫框架scrapy环境的搭建方法
2018/11/29 Python
Django urls.py重构及参数传递详解
2019/07/23 Python
matplotlib实现显示伪彩色图像及色度条
2019/12/07 Python
在pycharm中实现删除bookmark
2020/02/14 Python
PyQt5中QTableWidget如何弹出菜单的示例代码
2020/02/23 Python
Django Admin后台添加数据库视图过程解析
2020/04/01 Python
python根据完整路径获得盘名/路径名/文件名/文件扩展名的方法
2020/04/22 Python
pandas.DataFrame.drop_duplicates 用法介绍
2020/07/06 Python
Boden美国官网:英伦原创时装品牌
2017/07/03 全球购物
Intersport西班牙:在线体育商店
2019/11/06 全球购物
大专生自荐信
2013/10/04 职场文书
《王二小》教学反思
2014/02/27 职场文书
学校庆元旦歌咏比赛主持词
2014/03/18 职场文书
无财产离婚协议书范本
2014/10/28 职场文书
Python基础之教你怎么在M1系统上使用pandas
2021/05/08 Python
Mysql中有关Datetime和Timestamp的使用总结
2021/12/06 MySQL
mysql 8.0.27 绿色解压版安装教程及配置方法
2022/04/20 MySQL
SQL Server删除表中的重复数据
2022/05/25 SQL Server