基于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的列表toggle特效实例教程
Jan 04 HTML / CSS
使用CSS3和Checkbox实现JQuery的一些效果
Aug 03 HTML / CSS
利用纯CSS3实现动态的自行车特效源码
Jan 20 HTML / CSS
CSS3关于z-index不生效问题的解决
Feb 19 HTML / CSS
用css3实现转换过渡和动画效果
Mar 13 HTML / CSS
canvas实现图片马赛克的示例代码
Mar 26 HTML / CSS
基于IE10/HTML5 开发
Apr 22 HTML / CSS
简单html5代码获取地理位置
Mar 31 HTML / CSS
Html5大文件断点续传实现方法
Dec 05 HTML / CSS
深入了解canvas在移动端绘制模糊的问题解决
Apr 30 HTML / CSS
css背景和边框标签实例详解
May 21 HTML / CSS
在HTML中引入CSS的几种方式介绍
Dec 06 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连接Oracle数据库
2006/10/09 PHP
php生成WAP页面
2006/10/09 PHP
php 运行效率总结(提示程序速度)
2009/11/26 PHP
php统计时间和内存使用情况示例分享
2014/03/13 PHP
php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法
2015/11/30 PHP
PHP+shell脚本操作Memcached和Apache Status的实例分享
2016/03/11 PHP
magento后台无法登录解决办法的两种方法
2016/12/09 PHP
ThinkPHP中Widget扩展的两种写法及调用方法详解
2017/05/04 PHP
PHP利用百度ai实现文本和图片审核
2019/05/08 PHP
php使用curl模拟多线程实现批处理功能示例
2019/07/25 PHP
禁止刷新,回退的JS
2006/11/25 Javascript
jQuery图片轮播的具体实现
2013/09/11 Javascript
js函数与php函数的区别实例浅析
2015/01/12 Javascript
浅谈JavaScript中的apply/call/bind和this的使用
2017/02/26 Javascript
Javascript 关于基本类型和引用类型的个人理解
2019/11/01 Javascript
Nodejs使用archiver-zip-encrypted库加密压缩文件时报错(解决方案)
2019/11/18 NodeJs
详解如何在JS代码中消灭for循环
2019/12/11 Javascript
Vue发布订阅模式实现过程图解
2020/04/30 Javascript
Python爬虫常用库的安装及其环境配置
2018/09/19 Python
python实现简单聊天室功能 可以私聊
2019/07/12 Python
超全Python图像处理讲解(多模块实现)
2020/04/13 Python
Python实现自动打开电脑应用的示例代码
2020/04/17 Python
利用python对excel中一列的时间数据更改格式操作
2020/07/14 Python
基于Python pyecharts实现多种图例代码解析
2020/08/10 Python
英国最大的宠物食品和宠物用品网上零售商: Zooplus
2016/08/01 全球购物
idealfit英国:世界领先的女性健身用品和运动衣物品牌
2017/11/25 全球购物
中国京东和泰国中央集团合资的网站:JD CENTRAL
2020/08/22 全球购物
中西医结合临床医学专业大学生自荐信
2013/09/28 职场文书
开业庆典策划方案
2014/02/18 职场文书
检查机关党的群众路线个人整改措施
2014/10/04 职场文书
2014年社区民政工作总结
2014/12/02 职场文书
河童之夏观后感
2015/06/11 职场文书
圣诞晚会主持词
2015/07/01 职场文书
新学期开学标语2015
2015/07/16 职场文书
2016教师节感恩话语
2015/12/09 职场文书
机关干部正风肃纪心得体会
2016/01/15 职场文书