


付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、本文实例讲述了基于 html5 canvas 实现漫天飞雪效果的方法, 运行该实例可以看到很棒的下雪效果。如下图所示:主要代码如下:>"><html xmlns="<a href=">"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>
2、;漫天飞雪 </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&am
3、p;gt;<body></p><p><divstyle="background:#6b92b9; width:100%; height:2000px;" ></div><canvasid="canvas"style="position:fixed;top:0px;left:0px;z-index:80;pointer-events:none;"></can
4、vas></p> <p><script>window.onload = function()/canvas initvar canvas = document.getElementById("canvas");var ctx = canvas.getContext("2d");/canvas dimensionsvar W = window.innerWidth;var H = window.innerHeight;canvas.width = W;canv
5、as.height = H;/snowflake particlesvar mp = 3000; /max particlesvar particles = ;for(var i = 0; i < mp; i+)particles.push(x: Math.random()*W, /x-coordinatey: Math.random()*H, /y-coordinater: Math.random()*3+1, /radiusd: Math.random()*mp /density)/Lets draw the flakesfunction draw()ctx.clearRec
6、t(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 = particlesi;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 wil
7、l be an ongoing incremental flag. Sin and Cos functions will be applied to it to create vertical and horizontal movements of the flakesvar angle = 0;function update()angle += 0.01;for(var i = 0; i < mp; i+)var p = particlesi;/Updating X and Y coordinates/We will add 1 to the cos function to p
8、revent 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 radiusp.y += Math.cos(angle+p.d) + 1 + p.r/2;p.x += Math.sin(angle) * 2;/Sending flakes
9、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 flakesparticlesi = x: Math.random()*W, y: -10, r: p.r, d: p.d;else/If the flake is exitting from the
10、 rightif(Math.sin(angle) > 0)/Enter fromthparticlesi = x: -5, y: Math.random()*H, r: p.r, d: p.d;else/Enter from the rightparticlesi = x: W+5, y: Math.random()*H, r: p.r, d: p.d;/animation loopsetInterval(draw, 15);</script></body></html>代码分析如下:这行代码改变雪花半径大小:复制代码代码如下:r: Math.r
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年点火系统:点火开关项目合作计划书
- 2025年红外线汽车尾气分析仪项目合作计划书
- 教育技术与OER的深度融合研究
- 2025年年中国电子政务合作协议书
- 脑波技术在学习障碍诊断中的应用前景
- 2025年广东省深圳市龙文一对一物理高一下期末质量检测试题含解析
- 湖北省汉川二中2025年物理高二下期末检测模拟试题含解析
- 商业洞察生物机制驱动的可持续发展战略
- 技术引领未来中医传承在智能教育领域的发展
- 2025年湖南省东安一中高一物理第二学期期末质量检测模拟试题含解析
- 包席合同协议
- 资产评估风险管理制度
- 眼视光创业计划书
- 大学英语四六级词汇表
- 呼吸训练康复课件
- 2025既有建筑消防改造设计指南
- 血液透析医院感染预防与控制
- 胃镜室年度安全管理总结
- 企业数字化转型服务协议
- 人工智能算法的伦理规制研究
- 变电所设备更换申请报告
评论
0/150
提交评论