canvas画布数组绘画函数效果:源码:------------------------------
当前位置:Gxlcms > html代码 > HTML5实现下雪效果的实例代码分享

HTML5实现下雪效果的实例代码分享

时间:2021-07-01 10:21:17 帮助过:129人阅读

下雪实例

知识点:

http://www.gxlcms.com/code/7892.html" target="_blank">canvas画布

数组

绘画函数

效果:

1349.jpg

源码:

------------------------------

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="Generator" content="">
  6. <meta name="Author" content="">
  7. <meta name="Keywords" content="">
  8. <meta name="Description" content="">
  9. <title>下雪</title>
  10. <style>
  11. *{padding:0;margin:0}
  12. html{overflow:hidden}
  13. </style>
  14. </head>
  15. <body>
  16. <canvas id="canvas" style="background:#111"></canvas>
  17. <audio src="http://dx.sc.chinaz.com/Files/DownLoad/sound1/201210/2178.mp3" autoplay loop/>
  18. <audio src="http://dx.sc.chinaz.com/Files/DownLoad/sound1/201205/1430.mp3" autoplay loop/>
  19. <script type="text/javascript">
  20. window.onload = function(){
  21. //获取画布对象
  22. var canvas = document.getElementById("canvas");
  23. //获取画布的上下文
  24. var context =canvas.getContext("2d");
  25. //获取浏览器屏幕的宽度和高度
  26. var w = window.innerWidth;
  27. var h = window.innerHeight;
  28. //设置canvas的宽度和高度
  29. canvas.width = w;
  30. canvas.height = h;
  31. //1:如何产生雪花,一个圆 ,arc(x,y,r,start,end)
  32. //初始化雪花数量
  33. var num = 200;
  34. //雪花数组
  35. var snows = [];
  36. for(var i=0;i<num;i++){
  37. //x,y圆心掉的坐标位置,r代表圆的半径,d每个圆的每个圆之间的间距,c代表的颜色
  38. var r = randColor();
  39. snows.push({
  40. x:Math.random()*w,
  41. y:Math.random()*h,
  42. r:Math.random()*10,
  43. d:Math.random()*num
  44. });
  45. };
  46. //绘画的函数
  47. function draw(){
  48. context.clearRect(0,0,w,h);
  49. context.beginPath();
  50. for(var i=0;i<num;i++){
  51. var snow = snows[i];
  52. context.fillStyle = "rgba(255,255,255,0.9)";
  53. context.moveTo(snow.x,snow.y);
  54. context.arc(snow.x,snow.y,snow.r,0,Math.PI*2);
  55. }
  56. context.fill();
  57. //掉落
  58. drop();
  59. };
  60. var angle = 0;
  61. function drop()
  62. {
  63. angle += 0.01;
  64. for(var i = 0; i < num; i++){
  65. var p = snows[i];
  66. //记住两个公式:Math.sin(弧度)返回是一个0 1 -1 的数字
  67. //math.cos(1 0 -1 ) 自由体,
  68. p.y += Math.cos(angle+p.d) + 1 + p.r*0.625;
  69. p.x += Math.sin(angle) * 8 ;
  70. //如果雪花到了边界,进行边界处理
  71. if(p.x > w+5 || p.x < -5 || p.y > h){
  72. if(i%4 > 0) {
  73. snows[i] = {x: Math.random()*w, y: -10, r: p.r, d: p.d};
  74. }else{
  75. //控制方向
  76. if(Math.sin(angle) > 0){
  77. snows[i] = {x: -5, y: Math.random()*h, r: p.r, d: p.d};
  78. }else{
  79. snows[i] = {x: w+5, y: Math.random()*h, r: p.r, d: p.d};
  80. }
  81. }
  82. }
  83. }
  84. };
  85. //执行和调用函数
  86. draw();
  87. setInterval(draw,10);
  88. //随机颜色
  89. function randColor(){
  90. var r = Math.floor(Math.random() * 256);
  91. var g = Math.floor(Math.random() * 256);
  92. var b = Math.floor(Math.random() * 256);
  93. return "rgb("+r+","+g+","+b+")";
  94. };
  95. };
  96. </script>
  97. </body>
  98. </html>

以上就是 HTML5实现下雪效果的实例代码分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行