400-650-7353

精品课程

html流星雨代码 html流星雨代码详解

发布: Java培训问答 发布时间:2021-08-24 16:14:25

推荐答案
品牌型号:联想小新Pro13/系统版本:windows10

基于HTML+CSS+JS实现流星雨特效实现,可用于移动开发以及网站背景图,具体代码如下:

  1. <!doctype html> 
  2. <html> 
  3.     <head> 
  4.         <meta charset="utf-8" /> 
  5.         <title>流星雨</title> 
  6.         <meta name="keywords" content="关键词,关键字"
  7.         <meta name="description" content="描述信息"
  8.         <style> 
  9.             body { 
  10.                 margin: 0; 
  11.                 overflow: hidden; 
  12.             } 
  13.         </style> 
  14.     </head> 
  15.   
  16.     <body> 
  17.   
  18.         <!-- 
  19.             <canvas>画布 画板 画画的本子 
  20.         --> 
  21.         <canvas width=400 height=400 style="background:#000000;" id="canvas"></canvas> 
  22.   
  23.         <!-- 
  24.             javascript 
  25.             画笔 
  26.         -->  
  27.         <script> 
  28.                      
  29.             //获取画板 
  30.             //doccument 当前文档 
  31.             //getElement 获取一个标签 
  32.             //ById 通过Id名称的方式 
  33.             //var 声明一片空间 
  34.             //var canvas 声明一片空间的名字叫做canvas 
  35.             var canvas = document.getElementById("canvas"); 
  36.             //获取画板权限 上下文 
  37.             var ctx = canvas.getContext("2d"); 
  38.             //让画板的大小等于屏幕的大小 
  39.             /* 
  40.                 思路: 
  41.                     1.获取屏幕对象 
  42.                     2.获取屏幕的尺寸 
  43.                     3.屏幕的尺寸赋值给画板 
  44.             */ 
  45.             //获取屏幕对象 
  46.             var s = window.screen; 
  47.             //获取屏幕的宽度和高度 
  48.             var w = s.width; 
  49.             var h = s.height; 
  50.             //设置画板的大小 
  51.             canvas.width = w; 
  52.             canvas.height = h; 
  53.   
  54.             //设置文字大小  
  55.             var fontSize = 14; 
  56.             //计算一行有多少个文字 取整数 向下取整 
  57.             var clos = Math.floor(w/fontSize); 
  58.             //思考每一个字的坐标 
  59.             //创建数组把clos 个 0 (y坐标存储起来) 
  60.             var drops = []; 
  61.             var str = "qwertyuiopasdfghjklzxcvbnm"
  62.             //往数组里面添加 clos 个 0 
  63.             for(var i = 0;i<clos;i++) { 
  64.                 drops.push(0); 
  65.             } 
  66.   
  67.             //绘制文字 
  68.             function drawString() { 
  69.                 //给矩形设置填充色 
  70.                 ctx.fillStyle="rgba(0,0,0,0.05)" 
  71.                 //绘制一个矩形 
  72.                 ctx.fillRect(0,0,w,h); 
  73.   
  74.                 //添加文字样式 
  75.                 ctx.font = "600 "+fontSize+"px 微软雅黑"
  76.                 //设置文字颜色 
  77.                 ctx.fillStyle = "#00ff00"
  78.   
  79.                 for(var i = 0;i<clos;i++) { 
  80.                     //x坐标 
  81.                     var x = i*fontSize; 
  82.                     //y坐标 
  83.                     var y = drops[i]*fontSize; 
  84.                     //设置绘制文字 
  85.                     ctx.fillText(str[Math.floor(Math.random()*str.length)],x,y); 
  86.                     if(y>h&&Math.random()>0.99){ 
  87.                         drops[i] = 0; 
  88.                     } 
  89.                     drops[i]++; 
  90.                 } 
  91.                      
  92.             } 
  93.             //定义一个定时器,每隔30毫秒执行一次 
  94.             setInterval(drawString,30); 
  95.         </script> 
  96.     </body> 
  97. </html> 
其它答案
牛仔很忙2020-06-22 18:56:36
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4.     <meta charset="utf-8"
  5.     <title>流星雨</title> 
  6.     <script> 
  7.         var context; 
  8.         var arr = new Array(); 
  9.         var starCount = 800; 
  10.         var rains = new Array(); 
  11.         var rainCount = 20; 
  12.  
  13.         function init() { 
  14.             var stars = document.getElementById("stars"); 
  15.             windowWidth = window.innerWidth; //当前的窗口的高度 
  16.             stars.width = windowWidth; 
  17.             stars.height = window.innerHeight; 
  18.             context = stars.getContext("2d"); 
  19.         } 
  20.  
  21.         //创建一个星星对象 
  22.         var Star = function () { 
  23.             this.x = windowWidth * Math.random();//横坐标 
  24.             this.y = 5000 * Math.random();//纵坐标 
  25.             this.text = ".";//文本 
  26.             this.color = "white";//颜色 
  27.             this.getColor = function () { 
  28.                 var _r = Math.random(); 
  29.                 if (_r < 0.5) { 
  30.                     this.color = "#333"
  31.                 } else { 
  32.                     this.color = "white"
  33.                 } 
  34.             } 
  35. //初始化 
  36.             this.init = function () { 
  37.                 this.getColor(); 
  38.             } 
  39. //绘制 
  40.             this.draw = function () { 
  41.                 context.fillStyle = this.color; 
  42.                 context.fillText(this.text, this.x, this.y); 
  43.             } 
  44.         } 
  45.  
  46.         //画月亮 
  47.         function drawMoon() { 
  48.             var moon = new Image(); 
  49.             moon.src = "./images/moon.jpg" 
  50.             context.drawImage(moon, -5, -10); 
  51.         } 
  52.  
  53.         //页面加载的时候 
  54.         window.onload = function () { 
  55.             init(); 
  56. //画星星 
  57.             for (var i = 0; i < starCount; i++) { 
  58.                 var star = new Star(); 
  59.                 star.init(); 
  60.                 star.draw(); 
  61.                 arr.push(star); 
  62.             } 
  63. //画流星 
  64.             for (var i = 0; i < rainCount; i++) { 
  65.                 var rain = new MeteorRain(); 
  66.                 rain.init(); 
  67.                 rain.draw(); 
  68.                 rains.push(rain); 
  69.             } 
  70.             drawMoon();//绘制月亮 
  71.             playStars();//绘制闪动的星星 
  72.             playRains();//绘制流星 
  73.  
  74.         } 
  75.  
  76.         //星星闪起来 
  77.         function playStars() { 
  78.             for (var n = 0; n < starCount; n++) { 
  79.                 arr[n].getColor(); 
  80.                 arr[n].draw(); 
  81.             } 
  82.  
  83.             setTimeout("playStars()", 100); 
  84.         } 
  85.  
  86.         /*流星雨开始*/ 
  87.         var MeteorRain = function () { 
  88.             this.x = -1; 
  89.             this.y = -1; 
  90.             this.length = -1;//长度 
  91.             this.angle = 30; //倾斜角度 
  92.             this.width = -1;//宽度 
  93.             this.height = -1;//高度 
  94.             this.speed = 1;//速度 
  95.             this.offset_x = -1;//横轴移动偏移量 
  96.             this.offset_y = -1;//纵轴移动偏移量 
  97.             this.alpha = 1; //透明度 
  98.             this.color1 = "";//流星的色彩 
  99.             this.color2 = ""//流星的色彩 
  100.             /****************初始化函数********************/ 
  101.             this.init = function () //初始化 
  102.             { 
  103.                 this.getPos(); 
  104.                 this.alpha = 1;//透明度 
  105.                 this.getRandomColor(); 
  106. //最小长度,最大长度 
  107.                 var x = Math.random() * 80 + 150; 
  108.                 this.length = Math.ceil(x); 
  109. // x = Math.random()*10+30; 
  110.                 this.angle = 30; //流星倾斜角 
  111.                 x = Math.random() + 0.5; 
  112.                 this.speed = Math.ceil(x); //流星的速度 
  113.                 var cos = Math.cos(this.angle * 3.14 / 180); 
  114.                 var sin = Math.sin(this.angle * 3.14 / 180); 
  115.                 this.width = this.length * cos; //流星所占宽度 
  116.                 this.height = this.length * sin;//流星所占高度 
  117.                 this.offset_x = this.speed * cos; 
  118.                 this.offset_y = this.speed * sin; 
  119.             } 
  120.             /**************获取随机颜色函数*****************/ 
  121.             this.getRandomColor = function () { 
  122.                 var a = Math.ceil(255 - 240 * Math.random()); 
  123. //中段颜色 
  124.                 this.color1 = "rgba(" + a + "," + a + "," + a + ",1)"
  125. //结束颜色 
  126.                 this.color2 = "black"
  127.             } 
  128.             /***************重新计算流星坐标的函数******************/ 
  129.             this.countPos = function ()// 
  130.             { 
  131. //往左下移动,x减少,y增加 
  132.                 this.x = this.x - this.offset_x; 
  133.                 this.y = this.y + this.offset_y; 
  134.             } 
  135.             /*****************获取随机坐标的函数*****************/ 
  136.             this.getPos = function () // 
  137.             { 
  138. //横坐标200--1200 
  139.                 this.x = Math.random() * window.innerWidth; //窗口高度 
  140. //纵坐标小于600 
  141.                 this.y = Math.random() * window.innerHeight; //窗口宽度 
  142.             } 
  143.             /****绘制流星***************************/ 
  144.             this.draw = function () //绘制一个流星的函数 
  145.             { 
  146.                 context.save(); 
  147.                 context.beginPath(); 
  148.                 context.lineWidth = 1; //宽度 
  149.                 context.globalAlpha = this.alpha; //设置透明度 
  150. //创建横向渐变颜色,起点坐标至终点坐标 
  151.                 var line = context.createLinearGradient(this.x, this.y, 
  152.                     this.x + this.width, 
  153.                     this.y - this.height); 
  154. //分段设置颜色 
  155.                 line.addColorStop(0, "white"); 
  156.                 line.addColorStop(0.3, this.color1); 
  157.                 line.addColorStop(0.6, this.color2); 
  158.                 context.strokeStyle = line; 
  159. //起点 
  160.                 context.moveTo(this.x, this.y); 
  161. //终点 
  162.                 context.lineTo(this.x + this.width, this.y - this.height); 
  163.                 context.closePath(); 
  164.                 context.stroke(); 
  165.                 context.restore(); 
  166.             } 
  167.             this.move = function () { 
  168. //清空流星像素 
  169.                 var x = this.x + this.width - this.offset_x; 
  170.                 var y = this.y - this.height; 
  171.                 context.clearRect(x - 3, y - 3, this.offset_x + 5, this.offset_y + 5); 
  172. // context.strokeStyle="red"; 
  173. // context.strokeRect(x,y-1,this.offset_x+1,this.offset_y+1); 
  174. //重新计算位置,往左下移动 
  175.                 this.countPos(); 
  176. //透明度增加 
  177.                 this.alpha -= 0.002; 
  178. //重绘 
  179.                 this.draw(); 
  180.             } 
  181.         } 
  182.  
  183.         //绘制流星 
  184.         function playRains() { 
  185.  
  186.             for (var n = 0; n < rainCount; n++) { 
  187.                 var rain = rains[n]; 
  188.                 rain.move();//移动 
  189.                 if (rain.y > window.innerHeight) {//超出界限后重来 
  190.                     context.clearRect(rain.x, rain.y - rain.height, rain.width, rain.height); 
  191.                     rains[n] = new MeteorRain(); 
  192.                     rains[n].init(); 
  193.                 } 
  194.             } 
  195.             setTimeout("playRains()", 2); 
  196.         } 
  197.  
  198.         /*流星雨结束*/ 
  199.     </script> 
  200.     <style type="text/css"
  201.         body { 
  202.             background-color: black; 
  203.         } 
  204.  
  205.         body, html { 
  206.             width: 100%; 
  207.             height: 100%; 
  208.             overflow: hidden; 
  209.         } 
  210.     </style> 
  211. </head> 
  212. <body> 
  213. <canvas id="stars"></canvas> 
  214. </body> 
  215. </html> 

中公旗下IT培训品牌

  • 中公教育品牌

     中公教育是一家中国领先的全品类职业教育机构,提供超过100个品类的综合职业就业培训服务。公司在全国超过1000个直营网点展开经营,深度覆盖300多个地级市,并正在快速向数千个县城和高校扩张。

  • 完善就业体系

    通过阶段性授课机制,和每阶段的定期考核,先让学员能够学会所学内容,才能找打合适工作。最后一个阶段为就业课程,从技术和面试两个方面加深就业能力,并且还有不定期的双选会供大家选择。

  • 全程面授+实战技术

    线下课程全程是师资面对面教学,不会存在上课只对着大屏幕上课的情况,有问题都可以在课上得到解答。并且优就业通过自主研发大纲和学习路线,并且定期更新课程所学技术,让大家所学技术不落伍。

中公优就业专业职业规划老师

为您详细答疑解惑,更能领取免费课程

相关问题

更多课程

专业课程老师将第一时间为您解答

立即答疑
修改
优就业:ujiuye

关注中公优就业官方微信

  • 关注微信回复关键词“大礼包”,领80G学习资料