html5 canvas 一个漫天飞雪的效果
很棒的下雪效果
代码奉上
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>漫天飞雪</title>
<style type="text/css">
* {margin: 0; padding: 0;} body {
/*You can use any kind of background here.*/
background: #6b92b9;
}
canvas {
display: block;
}
</style>
</head> <body> <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> <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 canvas 一个漫天飞雪的效果的更多相关文章
- 经典!HTML5 Canvas 模拟可撕裂布料效果
这是一个模拟可撕裂布料效果的 HTML5 Canvas 应用演示,效果逼真.你会看到,借助 Canvas 的强大绘图和动画功能,只需很少的代码就能实现让您屏息凝神的效果. 温馨提示:为保证最佳的效果, ...
- 用HTML5 Canvas 做擦除及扩散效果
2013年的时候曾经使用canvas实现了一个擦除效果的需求,即模拟用户在模糊的玻璃上擦除水雾看到清晰景色的交互效果.好在2012年的时候学习HTML5的时候研究过canvas了,所以在比较短的时间内 ...
- Html5 Canvas一个简单的画笔例子
相比了下Qt quick的canvas和HTML5的canvas,发现HTML5 Canvas在同样绘制绘制操作下性能比Qt的canvas强很多,附上一个HTML5 canvas画笔一例子 var D ...
- HTML5 Canvas实战之刮奖效果
近年来由于移动设备对HTML5的较好支持,经常有活动用刮奖的效果,最近也在看H5方面的内容,就自己实现了一个,现分享出来跟大家交流. 1.效果 2.原理 原理很简单,就是在刮奖区添加两个canvas, ...
- HTML5 Canvas实现黑客帝国文字掉落效果
效果: 原理: 用canvas逐行输出文字,然后让背景颜色逐渐加深,再随机中断某些列. 代码: HTML: <canvas id="c"></canvas> ...
- <html5 canvas>一个简单的矩形
Html5: <!doctype html> <html> <head> <meta charset="UTF-8"> <ti ...
- 基于 HTML5 Canvas 的 3D 热力云图效果
前言 数据蕴藏价值,但数据的价值需要用 IT 技术去发现.探索,可视化可以帮助人更好的去分析数据,信息的质量很大程度上依赖于其呈现方式.在数据分析上,热力图无疑是一种很好的方式.在很多行业中都有着广泛 ...
- HTML5 Canvas实现刮刮卡效果实例
HTML: <style> #canvas { border: 1px solid blue; position: absolute; left: 10px; top: 10px; bac ...
- HTML5 Canvas实战之刮奖效果【转】
开源项目地址:https://github.com/artwl/Lottery 作者博客地址:http://www.cnblogs.com/jscode/p/3580878.html 谢谢浏览!
随机推荐
- 利用glibc中锁结构的信息解决死锁问题
首先非常感谢老丁和老李同学的帮助,没有他们这个问题估计又得搞很久.遇见这个问题,真是头疼.不熟悉代码.不熟悉流程,但是领导还是把活给排下来了(实在不解),只能硬着头皮找了. 问题是这样的,cac ...
- phpmyadmin上传较大sql文件
1.找到phpmyadmin目录,新建文件夹import 2.打开import文件夹,将要导入的sql文件放进去 3.打开config.inc.php文件,修改$cfg['UploadDir']等于i ...
- Firebug介绍及使用技巧
一.介绍 Firebug是网页浏览器Firefox下的一款开发调试工具.安装firebug后在浏览器的插件工具栏中(上方)会有一个小甲虫的图标. F12打开和关闭Firebug窗口. 二.FireBu ...
- 龙邱STM32单片机用J-LINK下载无法被识别的解决方法
问题如下: 按照正常步骤使用keil5给龙邱的stm32下载程序,SWD下载方式提示no cortex-m sw device found,JTAG方式提示no cortex-m device fou ...
- Frogger(最短路)
Frogger(poj2253) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31854 Accepted: 1026 ...
- 宽带连接工具[bat]
功能概述: 本工具使用批处理编写,提供自动判断网络状态以决定断开或是连上网络,本月已用宽带时长,到月初自动清零.提供联网日志功能,可以记录下所有的连接或断开网络记录.如果连接失败,自动提示输入密码,特 ...
- 1007 Numerical Summation of a Series
简单入门题.按照题目给的指导编程,算多少数要理解题意. #include <stdio.h> int main(){ int k,ssx; double x,psix; ;ssx<= ...
- python模范发送邮件的时候,才smtp.connect的时候总是抛出错误
python发送邮件的时候,总是出现:[Errno 10060] 错误码 根据debug得到在connect的时候出错. 认真检查了下host,没有错呀~应该就是服务器的host. 查看了下网上的一些 ...
- Ubuntu14.0.4 64位 ADT 连接手机调试问题
1:使用 lsusb 命令查看USB 设备 y@y:~$ lsusbBus 001 Device 002: ID 8087:8000 Intel Corp. Bus 001 Device 001: I ...
- GUI为什么不设计为多线程(用户事件和底层事件的流程是相反的,每层都加锁效率太低,共用一把锁那就是单线程)
在我们这批新人转正评审的时候,我师父问了我的小伙伴一个问题:为什么一些更新界面的方法只能在主线程中调用?师父没有问我这个问题,让知其然但不知其所以然的我有种侥幸逃过一难的心情.我想如果回答那是因为An ...