学分高考 web前端

html流星雨代码

发布时间: 2022-03-03 17:47:48

精选答案

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

  1. <!doctypehtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8"/>
  5. <title>流星雨</title>
  6. <metaname="keywords"content="关键词,关键字">
  7. <metaname="description"content="描述信息">
  8. <style>
  9. body{
  10. margin:0;
  11. overflow:hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!--
  17. <canvas>画布画板画画的本子
  18. -->
  19. <canvaswidth=400height=400style="background:#000000;"id="canvas"></canvas>
  20. <!--
  21. javascript
  22. 画笔
  23. -->
  24. <script>
  25. //获取画板
  26. //doccument当前文档
  27. //getElement获取一个标签
  28. //ById通过Id名称的方式
  29. //var声明一片空间
  30. //varcanvas声明一片空间的名字叫做canvas
  31. varcanvas=document.getElementById("canvas");
  32. //获取画板权限上下文
  33. varctx=canvas.getContext("2d");
  34. //让画板的大小等于屏幕的大小
  35. //获取屏幕对象
  36. vars=window.screen;
  37. //获取屏幕的宽度和高度
  38. varw=s.width;
  39. varh=s.height;
  40. //设置画板的大小
  41. canvas.width=w;
  42. canvas.height=h;
  43. //设置文字大小
  44. varfontSize=14;
  45. //计算一行有多少个文字取整数向下取整
  46. varclos=Math.floor(w/fontSize);
  47. //思考每一个字的坐标
  48. //创建数组把clos个0(y坐标存储起来)
  49. vardrops=[];
  50. varstr="qwertyuiopasdfghjklzxcvbnm";
  51. //往数组里面添加clos个0
  52. for(vari=0;i<clos;i++){
  53. drops.push(0);
  54. }
  55. //绘制文字
  56. functiondrawString(){
  57. //给矩形设置填充色
  58. ctx.fillStyle="rgba(0,0,0,0.05)"
  59. //绘制一个矩形
  60. ctx.fillRect(0,0,w,h);
  61. //添加文字样式
  62. ctx.font="600"+fontSize+"px微软雅黑";
  63. //设置文字颜色
  64. ctx.fillStyle="#00ff00";
  65. for(vari=0;i<clos;i++){
  66. //x坐标
  67. varx=i*fontSize;
  68. //y坐标
  69. vary=drops[i]*fontSize;
  70. //设置绘制文字
  71. ctx.fillText(str[Math.floor(Math.random()*str.length)],x,y);
  72. if(y>h&&Math.random()>0.99){
  73. drops[i]=0;
  74. }
  75. drops[i]++;
  76. }
  77. }
  78. //定义一个定时器,每隔30毫秒执行一次
  79. setInterval(drawString,30);
  80. </script>
  81. </body>
  82. </html>

其他答案

  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4. <metacharset="utf-8">
  5. <title>流星雨</title>
  6. <script>
  7. varcontext;
  8. vararr=newArray();
  9. varstarCount=800;
  10. varrains=newArray();
  11. varrainCount=20;
  12. functioninit(){
  13. varstars=document.getElementById("stars");
  14. windowWidth=window.innerWidth;//当前的窗口的高度
  15. stars.width=windowWidth;
  16. stars.height=window.innerHeight;
  17. context=stars.getContext("2d");
  18. }
  19. //创建一个星星对象
  20. varStar=function(){
  21. this.x=windowWidth*Math.random();//横坐标
  22. this.y=5000*Math.random();//纵坐标
  23. this.text=".";//文本
  24. this.color="white";//颜色
  25. this.getColor=function(){
  26. var_r=Math.random();
  27. if(_r<0.5){
  28. this.color="#333";
  29. }else{
  30. this.color="white";
  31. }
  32. }
  33. //初始化
  34. this.init=function(){
  35. this.getColor();
  36. }
  37. //绘制
  38. this.draw=function(){
  39. context.fillStyle=this.color;
  40. context.fillText(this.text,this.x,this.y);
  41. }
  42. }
  43. //画月亮
  44. functiondrawMoon(){
  45. varmoon=newImage();
  46. moon.src="./images/moon.jpg"
  47. context.drawImage(moon,-5,-10);
  48. }
  49. //页面加载的时候
  50. window.onload=function(){
  51. init();
  52. //画星星
  53. for(vari=0;i<starCount;i++){
  54. varstar=newStar();
  55. star.init();
  56. star.draw();
  57. arr.push(star);
  58. }
  59. //画流星
  60. for(vari=0;i<rainCount;i++){
  61. varrain=newMeteorRain();
  62. rain.init();
  63. rain.draw();
  64. rains.push(rain);
  65. }
  66. drawMoon();//绘制月亮
  67. playStars();//绘制闪动的星星
  68. playRains();//绘制流星
  69. }
  70. //星星闪起来
  71. functionplayStars(){
  72. for(varn=0;n<starCount;n++){
  73. arr[n].getColor();
  74. arr[n].draw();
  75. }
  76. setTimeout("playStars()",100);
  77. }
  78. varMeteorRain=function(){
  79. this.x=-1;
  80. this.y=-1;
  81. this.length=-1;//长度
  82. this.angle=30;//倾斜角度
  83. this.width=-1;//宽度
  84. this.height=-1;//高度
  85. this.speed=1;//速度
  86. this.offset_x=-1;//横轴移动偏移量
  87. this.offset_y=-1;//纵轴移动偏移量
  88. this.alpha=1;//透明度
  89. this.color1="";//流星的色彩
  90. this.color2="";//流星的色彩
  91. this.init=function()//初始化
  92. {
  93. this.getPos();
  94. this.alpha=1;//透明度
  95. this.getRandomColor();
  96. //最小长度,最大长度
  97. varx=Math.random()*80+150;
  98. this.length=Math.ceil(x);
  99. //x=Math.random()*10+30;
  100. this.angle=30;//流星倾斜角
  101. x=Math.random()+0.5;
  102. this.speed=Math.ceil(x);//流星的速度
  103. varcos=Math.cos(this.angle*3.14/180);
  104. varsin=Math.sin(this.angle*3.14/180);
  105. this.width=this.length*cos;//流星所占宽度
  106. this.height=this.length*sin;//流星所占高度
  107. this.offset_x=this.speed*cos;
  108. this.offset_y=this.speed*sin;
  109. }
  110. this.getRandomColor=function(){
  111. vara=Math.ceil(255-240*Math.random());
  112. //中段颜色
  113. this.color1="rgba("+a+","+a+","+a+",1)";
  114. //结束颜色
  115. this.color2="black";
  116. }
  117. this.countPos=function()//
  118. {
  119. //往左下移动,x减少,y增加
  120. this.x=this.x-this.offset_x;
  121. this.y=this.y+this.offset_y;
  122. }
  123. this.getPos=function()//
  124. {
  125. //横坐标200--1200
  126. this.x=Math.random()*window.innerWidth;//窗口高度
  127. //纵坐标小于600
  128. this.y=Math.random()*window.innerHeight;//窗口宽度
  129. }
  130. this.draw=function()//绘制一个流星的函数
  131. {
  132. context.save();
  133. context.beginPath();
  134. context.lineWidth=1;//宽度
  135. context.globalAlpha=this.alpha;//设置透明度
  136. //创建横向渐变颜色,起点坐标至终点坐标
  137. varline=context.createLinearGradient(this.x,this.y,
  138. this.x+this.width,
  139. this.y-this.height);
  140. //分段设置颜色
  141. line.addColorStop(0,"white");
  142. line.addColorStop(0.3,this.color1);
  143. line.addColorStop(0.6,this.color2);
  144. context.strokeStyle=line;
  145. //起点
  146. context.moveTo(this.x,this.y);
  147. //终点
  148. context.lineTo(this.x+this.width,this.y-this.height);
  149. context.closePath();
  150. context.stroke();
  151. context.restore();
  152. }
  153. this.move=function(){
  154. //清空流星像素
  155. varx=this.x+this.width-this.offset_x;
  156. vary=this.y-this.height;
  157. context.clearRect(x-3,y-3,this.offset_x+5,this.offset_y+5);
  158. //context.strokeStyle="red";
  159. //context.strokeRect(x,y-1,this.offset_x+1,this.offset_y+1);
  160. //重新计算位置,往左下移动
  161. this.countPos();
  162. //透明度增加
  163. this.alpha-=0.002;
  164. //重绘
  165. this.draw();
  166. }
  167. }
  168. //绘制流星
  169. functionplayRains(){
  170. for(varn=0;n<rainCount;n++){
  171. varrain=rains[n];
  172. rain.move();//移动
  173. if(rain.y>window.innerHeight){//超出界限后重来
  174. context.clearRect(rain.x,rain.y-rain.height,rain.width,rain.height);
  175. rains[n]=newMeteorRain();
  176. rains[n].init();
  177. }
  178. }
  179. setTimeout("playRains()",2);
  180. }
  181. </script>
  182. <styletype="text/css">
  183. body{
  184. background-color:black;
  185. }
  186. body,html{
  187. width:100%;
  188. height:100%;
  189. overflow:hidden;
  190. }
  191. </style>
  192. </head>
  193. <body>
  194. <canvasid="stars"></canvas>
  195. </body>
  196. </html>
温馨提示:
本文【html流星雨代码】由作者教培参考提供。该文观点仅代表作者本人,学分高考系信息发布平台,仅提供信息存储空间服务,若存在侵权问题,请及时联系管理员或作者进行删除。
我们采用的作品包括内容和图片部分来源于网络用户投稿,我们不确定投稿用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的权利,请联系我站将及时删除。
内容侵权、违法和不良信息举报
Copyright @ 2024 学分高考 All Rights Reserved 版权所有. 湘ICP备17021685号