基于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 相关文章推荐
10个很棒的 CSS3 开发工具 推荐
May 16 HTML / CSS
css3强大的动画效果animate使用说明及浏览器兼容介绍
Jan 09 HTML / CSS
CSS3 text shadow字体阴影效果
Jan 08 HTML / CSS
CSS3+JavaScript实现炫酷呼吸效果的示例代码
Jun 15 HTML / CSS
CSS3 对过渡(transition)进行调速以及延时
Oct 21 HTML / CSS
html5播放视频且动态截图实现步骤与代码(支持safari其他未测试)
Jan 06 HTML / CSS
如何在Canvas中添加事件的方法示例
May 21 HTML / CSS
HTML5手指下滑弹出负一屏阻止移动端浏览器内置下拉刷新功能的实现代码
Apr 10 HTML / CSS
AmazeUI框架搭建的方法步骤(图文)
Aug 17 HTML / CSS
CSS3 制作的图片滚动效果
Apr 14 HTML / CSS
CSS3新特性详解(五):多列columns column-count和flex布局
Apr 30 HTML / CSS
HTML+css盒子模型案例(圆,半圆等)“border-radius” 简单易上手
May 10 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
简单实现限定phpmyadmin访问ip的方法
2013/03/05 PHP
深入PHP中慎用双等于(==)的详解
2013/06/06 PHP
php ajax实现文件上传进度条
2016/03/29 PHP
php实现的redis缓存类定义与使用方法示例
2017/08/09 PHP
JavaScript 封装Ajax传递的数据代码
2009/06/05 Javascript
jQuery写的日历(包括日历的样式及功能)
2013/04/23 Javascript
yepnope.js使用详解及示例分享
2014/06/23 Javascript
原生JavaScript实现合并多个数组示例
2014/09/21 Javascript
JQuery给网页更换皮肤的方法
2015/05/30 Javascript
Javascript基于对象三大特性(封装性、继承性、多态性)
2016/01/04 Javascript
AngularJS基础 ng-open 指令简单实例
2016/08/02 Javascript
JS模拟实现方法重载示例
2016/08/03 Javascript
JavaScript仿微博发布信息案例
2016/11/16 Javascript
Bootstrap学习笔记之进度条、媒体对象实例详解
2017/03/09 Javascript
Angular利用trackBy提升性能的方法
2018/01/26 Javascript
深入理解JavaScript 中的匿名函数((function() {})();)与变量的作用域
2018/08/28 Javascript
微信小程序文章详情页面实现代码
2018/09/10 Javascript
微信运维交互机器人的示例代码
2018/11/12 Javascript
微信小程序使用map组件实现获取定位城市天气或者指定城市天气数据功能
2019/01/22 Javascript
inquirer.js一个用户与命令行交互的工具详解
2019/05/18 Javascript
python分析网页上所有超链接的方法
2015/05/08 Python
深入学习Python中的装饰器使用
2016/06/20 Python
python hbase读取数据发送kafka的方法
2018/12/27 Python
python cv2在验证码识别中应用实例解析
2019/12/25 Python
tensorflow模型的save与restore,及checkpoint中读取变量方式
2020/05/26 Python
ghd澳大利亚官方网站:英国最受欢迎的美发工具品牌
2018/05/21 全球购物
火山咖啡:Volcanica Coffee
2019/10/29 全球购物
英国比较机场停车场网站:Airport Parking Essentials
2019/12/01 全球购物
教师对学生的评语
2014/04/28 职场文书
承诺函范文
2015/01/21 职场文书
2015年见习期个人工作总结
2015/05/28 职场文书
2015初一年级组工作总结
2015/07/24 职场文书
公司食堂管理制度
2015/08/05 职场文书
如何把新闻人物写得立体、鲜活?
2019/08/14 职场文书
微信小程序 WeUI扩展组件库的入门教程
2022/04/21 Javascript