基于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 相关文章推荐
html5 Canvas画图教程(2)—画直线与设置线条的样式如颜色/端点/交汇点
Jan 09 HTML / CSS
html5的canvas实现3d雪花飘舞效果
Dec 27 HTML / CSS
HTML5中新标签和常用标签详解
Mar 07 HTML / CSS
5 个强大的HTML5 API 函数推荐
Nov 19 HTML / CSS
HTML5新特性之用SVG绘制微信logo
Feb 03 HTML / CSS
canvas像素点操作之视频绿幕抠图
Sep 11 HTML / CSS
html5给汉字加拼音加进度条的实现代码
Apr 07 HTML / CSS
html5 横向滑动导航栏的方法示例
May 08 HTML / CSS
HTML5开发动态音频图的实现
Jul 02 HTML / CSS
清除canvas画布内容(点擦除+线擦除)
Aug 12 HTML / CSS
CSS极坐标的实例代码
Jun 03 HTML / CSS
sass 常用备忘案例详解
Sep 15 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新手上路(四)
2006/10/09 PHP
解析在apache里面给php写虚拟目录的详细方法
2013/06/24 PHP
PHP 反射(Reflection)使用实例
2015/05/12 PHP
万能的php分页类
2017/07/06 PHP
用javascript实现的图片马赛克后显示并切换加文字功能
2007/04/21 Javascript
js 颜色选择器(兼容firefox)
2009/03/05 Javascript
计算世界完全对称日的js代码,粗糙版
2011/11/04 Javascript
关于JQuery($.load)事件的用法和分析
2013/04/09 Javascript
JQuery教学之性能优化
2014/05/14 Javascript
JS基于Ajax实现的网页Loading效果代码
2015/10/27 Javascript
JS基于onclick事件实现单个按钮的编辑与保存功能示例
2017/02/13 Javascript
详解Angular 4.x NgIf 的用法
2017/05/22 Javascript
vue2.0全局组件之pdf详解
2017/06/26 Javascript
详解bootstrap导航栏.nav与.navbar区别
2017/11/23 Javascript
JavaScript实现左侧菜单效果
2017/12/14 Javascript
angularJs select绑定的model取不到值的解决方法
2018/10/08 Javascript
深入浅析vue中cross-env的使用
2019/09/12 Javascript
JS实现容器模块左右拖动效果
2020/01/14 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
JS制作简易计算器的实例代码
2020/07/04 Javascript
[01:00:44]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第三局
2016/03/04 DOTA
python检测是文件还是目录的方法
2015/07/03 Python
Python实现大文件排序的方法
2015/07/10 Python
python 全局变量的import机制介绍
2017/09/07 Python
用python编写第一个IDA插件的实例
2018/05/29 Python
Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例
2018/07/27 Python
python遍历小写英文字母的方法
2019/01/02 Python
Python编程快速上手——选择性拷贝操作案例分析
2020/02/28 Python
python实现与redis交互操作详解
2020/04/21 Python
Django 解决model 反向引用中的related_name问题
2020/05/19 Python
详解python命令提示符窗口下如何运行python脚本
2020/09/11 Python
python推导式的使用方法实例
2021/02/28 Python
聊城大学毕业生自荐书
2014/02/01 职场文书
初中新生军训方案
2014/05/13 职场文书
应届生求职信范文
2014/06/30 职场文书
学校安全责任书范本
2014/07/23 职场文书