基于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教程:新增加的结构伪类
Apr 02 HTML / CSS
详解CSS3中Media Queries的相关使用
Jul 17 HTML / CSS
利用CSS3实现进度条的两种姿势详解
Mar 21 HTML / CSS
CSS3中伪元素::before和::after的用法示例
Sep 18 HTML / CSS
HTML5的Geolocation地理位置定位API使用教程
May 12 HTML / CSS
HTML5离线缓存Manifest是什么
Mar 09 HTML / CSS
HTML5的文档结构和新增标签完全解析
Apr 21 HTML / CSS
HTML5手机端弹出遮罩菜单特效代码
Jan 27 HTML / CSS
html5简介_动力节点Java学院整理
Jul 07 HTML / CSS
Canvas高级路径操作之拖拽对象的实现
Aug 05 HTML / CSS
使用数据结构给女朋友写个Html5走迷宫游戏
Nov 26 HTML / CSS
canvas实现手机的手势解锁的步骤详细
Mar 16 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
一个改进的UBB类
2006/10/09 PHP
浅析PHP中Collection 类的设计
2013/06/21 PHP
php强制运行广告的方法
2014/12/01 PHP
jquery插件 cluetip 关键词注释
2010/01/12 Javascript
判断某个字符在一个字符串中是否存在的js代码
2014/02/28 Javascript
JS获取鼠标选中的文字
2016/08/10 Javascript
简单模拟node.js中require的加载机制
2016/10/27 Javascript
使用 bootstrap modal遇到的问题小结
2016/11/09 Javascript
JS搜狐面试题分析
2016/12/16 Javascript
JavaScript设计模式之享元模式实例详解
2019/01/17 Javascript
如何基于jQuery实现五角星评分
2020/09/02 jQuery
Ant design vue中的联动选择取消操作
2020/10/31 Javascript
Vue实现小购物车功能
2020/12/21 Vue.js
Python中输出ASCII大文字、艺术字、字符字小技巧
2015/04/28 Python
python搭建微信公众平台
2016/02/09 Python
python实现随机森林random forest的原理及方法
2017/12/21 Python
Windows环境下python环境安装使用图文教程
2018/03/13 Python
Python中的TCP socket写法示例
2018/05/11 Python
Django基础知识与基本应用入门教程
2018/07/20 Python
浅谈python多进程共享变量Value的使用tips
2019/07/16 Python
python3.6+selenium实现操作Frame中的页面元素
2019/07/16 Python
用Python爬取QQ音乐评论并制成词云图的实例
2019/08/24 Python
Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例
2020/02/05 Python
在Keras中CNN联合LSTM进行分类实例
2020/06/29 Python
python从PDF中提取数据的示例
2020/10/30 Python
css3气泡 css3关键帧动画创建的动态通知气泡
2013/02/26 HTML / CSS
五个2015 年最佳HTML5 框架
2015/11/11 HTML / CSS
学校卫生检查制度
2014/02/03 职场文书
公司总经理岗位职责
2014/03/15 职场文书
幼儿园毕业寄语
2014/04/03 职场文书
供应链金融服务方案
2014/05/25 职场文书
毕业酒会致辞
2015/07/29 职场文书
口袋妖怪冰系十大最强精灵,几何雪花排第七,第六类似北极熊
2022/03/18 日漫
聊聊基于pytorch实现Resnet对本地数据集的训练问题
2022/03/25 Python
iSCSI服务器CHAP双向认证配置
2022/04/01 Servers
VUE解决跨域问题Access to XMLHttpRequest at
2022/05/06 Vue.js