400-650-7353

精品課程

html流星雨代碼 html流星雨代碼詳解

發(fā)布: Java培訓(xùn)問答 發(fā)布時間:2021-08-24 16:14:25

推薦答案
品牌型號:聯(lián)想小新Pro13/系統(tǒng)版本:windows10

基于HTML+CSS+JS實現(xiàn)流星雨特效實現(xiàn),可用于移動開發(fā)以及網(wǎng)站背景圖,具體代碼如下:

  1. <!doctype html> 
  2. <html> 
  3.     <head> 
  4.         <meta charset="utf-8" /> 
  5.         <title>流星雨</title> 
  6.         <meta name="keywords" content="關(guān)鍵詞,關(guān)鍵字"
  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 當(dāng)前文檔 
  31.             //getElement 獲取一個標(biāo)簽 
  32.             //ById 通過Id名稱的方式 
  33.             //var 聲明一片空間 
  34.             //var canvas 聲明一片空間的名字叫做canvas 
  35.             var canvas = document.getElementById("canvas"); 
  36.             //獲取畫板權(quán)限 上下文 
  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.             //設(shè)置畫板的大小 
  51.             canvas.width = w; 
  52.             canvas.height = h; 
  53.   
  54.             //設(shè)置文字大小  
  55.             var fontSize = 14; 
  56.             //計算一行有多少個文字 取整數(shù) 向下取整 
  57.             var clos = Math.floor(w/fontSize); 
  58.             //思考每一個字的坐標(biāo) 
  59.             //創(chuàng)建數(shù)組把clos 個 0 (y坐標(biāo)存儲起來) 
  60.             var drops = []; 
  61.             var str = "qwertyuiopasdfghjklzxcvbnm"
  62.             //往數(shù)組里面添加 clos 個 0 
  63.             for(var i = 0;i<clos;i++) { 
  64.                 drops.push(0); 
  65.             } 
  66.   
  67.             //繪制文字 
  68.             function drawString() { 
  69.                 //給矩形設(shè)置填充色 
  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.                 //設(shè)置文字顏色 
  77.                 ctx.fillStyle = "#00ff00"
  78.   
  79.                 for(var i = 0;i<clos;i++) { 
  80.                     //x坐標(biāo) 
  81.                     var x = i*fontSize; 
  82.                     //y坐標(biāo) 
  83.                     var y = drops[i]*fontSize; 
  84.                     //設(shè)置繪制文字 
  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毫秒執(zhí)行一次 
  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; //當(dāng)前的窗口的高度 
  16.             stars.width = windowWidth; 
  17.             stars.height = window.innerHeight; 
  18.             context = stars.getContext("2d"); 
  19.         } 
  20.  
  21.         //創(chuàng)建一個星星對象 
  22.         var Star = function () { 
  23.             this.x = windowWidth * Math.random();//橫坐標(biāo) 
  24.             this.y = 5000 * Math.random();//縱坐標(biāo) 
  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.             /****************初始化函數(shù)********************/ 
  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.             /**************獲取隨機顏色函數(shù)*****************/ 
  121.             this.getRandomColor = function () { 
  122.                 var a = Math.ceil(255 - 240 * Math.random()); 
  123. //中段顏色 
  124.                 this.color1 = "rgba(" + a + "," + a + "," + a + ",1)"
  125. //結(jié)束顏色 
  126.                 this.color2 = "black"
  127.             } 
  128.             /***************重新計算流星坐標(biāo)的函數(shù)******************/ 
  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.             /*****************獲取隨機坐標(biāo)的函數(shù)*****************/ 
  136.             this.getPos = function () // 
  137.             { 
  138. //橫坐標(biāo)200--1200 
  139.                 this.x = Math.random() * window.innerWidth; //窗口高度 
  140. //縱坐標(biāo)小于600 
  141.                 this.y = Math.random() * window.innerHeight; //窗口寬度 
  142.             } 
  143.             /****繪制流星***************************/ 
  144.             this.draw = function () //繪制一個流星的函數(shù) 
  145.             { 
  146.                 context.save(); 
  147.                 context.beginPath(); 
  148.                 context.lineWidth = 1; //寬度 
  149.                 context.globalAlpha = this.alpha; //設(shè)置透明度 
  150. //創(chuàng)建橫向漸變顏色,起點坐標(biāo)至終點坐標(biāo) 
  151.                 var line = context.createLinearGradient(this.x, this.y, 
  152.                     this.x + this.width, 
  153.                     this.y - this.height); 
  154. //分段設(shè)置顏色 
  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.         /*流星雨結(jié)束*/ 
  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培訓(xùn)品牌

  • 中公教育品牌

     中公教育是一家中國領(lǐng)先的全品類職業(yè)教育機構(gòu),提供超過100個品類的綜合職業(yè)就業(yè)培訓(xùn)服務(wù)。公司在全國超過1000個直營網(wǎng)點展開經(jīng)營,深度覆蓋300多個地級市,并正在快速向數(shù)千個縣城和高校擴張。

  • 完善就業(yè)體系

    通過階段性授課機制,和每階段的定期考核,先讓學(xué)員能夠?qū)W會所學(xué)內(nèi)容,才能找打合適工作。最后一個階段為就業(yè)課程,從技術(shù)和面試兩個方面加深就業(yè)能力,并且還有不定期的雙選會供大家選擇。

  • 全程面授+實戰(zhàn)技術(shù)

    線下課程全程是師資面對面教學(xué),不會存在上課只對著大屏幕上課的情況,有問題都可以在課上得到解答。并且優(yōu)就業(yè)通過自主研發(fā)大綱和學(xué)習(xí)路線,并且定期更新課程所學(xué)技術(shù),讓大家所學(xué)技術(shù)不落伍。

中公優(yōu)就業(yè)專業(yè)職業(yè)規(guī)劃老師

為您詳細(xì)答疑解惑,更能領(lǐng)取免費課程

相關(guān)問題

更多課程

專業(yè)課程老師將第一時間為您解答

立即答疑
修改
優(yōu)就業(yè):ujiuye

關(guān)注中公優(yōu)就業(yè)官方微信

  • 關(guān)注微信回復(fù)關(guān)鍵詞“大禮包”,領(lǐng)80G學(xué)習(xí)資料