基于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中:nth-child和:nth-of-type的区别深入理解
Mar 10 HTML / CSS
css3制作彩色边线3d立体按钮的示例(css3按钮)
May 06 HTML / CSS
CSS3实现多背景展示效果通过CSS3定位多张背景
Aug 10 HTML / CSS
HTML5实现表单自动验证功能实例代码
Jan 11 HTML / CSS
webapp字号大小跟随系统字号大小缩放的示例代码
Dec 26 HTML / CSS
详解rem 适配布局
Oct 31 HTML / CSS
html5时钟实现代码
Oct 22 HTML / CSS
HTML5验证以及日期显示的实现详解
Jul 05 HTML / CSS
html5通过postMessage进行跨域通信的方法
Dec 04 HTML / CSS
canvas因为图片资源不在同一域名下而导致的跨域污染画布的解决办法
Jan 18 HTML / CSS
html5 移动端视频video的android兼容(去除播放控件、全屏)
Mar 26 HTML / CSS
VW、VH适配移动端的解决方案与常见问题
May 21 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 将字符串按大写字母分隔成字符串数组
2010/04/30 PHP
基于在生产环境中使用php性能测试工具xhprof的详解
2013/06/03 PHP
hadoop中一些常用的命令介绍
2013/06/19 PHP
php计算两个日期相差天数的方法
2015/03/14 PHP
PHP弱类型的安全问题详细总结
2016/09/25 PHP
javascript编程起步(第三课)
2007/02/27 Javascript
alixixi runcode.asp的代码不错的应用
2007/08/08 Javascript
js判断一个元素是否为另一个元素的子元素的代码
2012/03/21 Javascript
jQuery实现鼠标滑向当前图片高亮显示并且其它图片变灰的方法
2015/07/27 Javascript
利用jsonp跨域调用百度js实现搜索框智能提示
2016/08/24 Javascript
自定义require函数让浏览器按需加载Js文件
2016/11/24 Javascript
JavaScript中匿名函数的递归调用
2017/01/22 Javascript
ExtJs异步无法向外传值和赋值的完美解决办法
2017/06/14 Javascript
JavaScript中三个等号和两个等号你了解多少
2017/07/04 Javascript
vuejs 单文件组件.vue 文件的使用
2017/07/28 Javascript
Vue+axios+WebApi+NPOI导出Excel文件实例方法
2019/06/05 Javascript
jQuery实现带进度条的轮播图
2020/09/13 jQuery
python使用pycharm环境调用opencv库
2018/02/11 Python
Django 接收Post请求数据,并保存到数据库的实现方法
2019/07/12 Python
python判断正负数方式
2020/06/03 Python
Pycharm配置autopep8实现流程解析
2020/11/28 Python
Matplotlib animation模块实现动态图
2021/02/25 Python
美国娱乐和流行文化商品店:FYE
2017/09/14 全球购物
全球性的众包图形设计市场:DesignCrowd
2021/02/02 全球购物
高三历史教学反思
2014/01/09 职场文书
网络事业创业计划书范文
2014/01/09 职场文书
档案接收函范文
2014/01/10 职场文书
旅游网创业计划书
2014/01/31 职场文书
大学生开西餐厅创业计划书
2014/02/01 职场文书
教师培训简讯
2015/07/20 职场文书
关于艺术节的开幕致辞
2016/03/04 职场文书
python爬取豆瓣电影TOP250数据
2021/05/23 Python
mysql外连接与内连接查询的不同之处
2021/06/03 MySQL
Java elasticsearch安装以及部署教程
2021/06/28 Java/Android
spring cloud 配置中心native配置方式
2021/09/25 Java/Android
Redis如何实现验证码发送 以及限制每日发送次数
2022/04/18 Redis