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. <body>
  16. <!--
  17. <canvas>画布 画板 画画的本子
  18. -->
  19. <canvas width=400 height=400 style="background:#000000;" id="canvas"></canvas>
  20. <!--
  21. javascript
  22. 画笔
  23. -->
  24. <script>
  25. //获取画板
  26. //doccument 当前文档
  27. //getElement 获取一个标签
  28. //ById 通过Id名称的方式
  29. //var 声明一片空间
  30. //var canvas 声明一片空间的名字叫做canvas
  31. var canvas = document.getElementById("canvas");
  32. //获取画板权限 上下文
  33. var ctx = canvas.getContext("2d");
  34. //让画板的大小等于屏幕的大小
  35. /*
  36. 思路:
  37. 1.获取屏幕对象
  38. 2.获取屏幕的尺寸
  39. 3.屏幕的尺寸赋值给画板
  40. */
  41. //获取屏幕对象
  42. var s = window.screen;
  43. //获取屏幕的宽度和高度
  44. var w = s.width;
  45. var h = s.height;
  46. //设置画板的大小
  47. canvas.width = w;
  48. canvas.height = h;
  49. //设置文字大小
  50. var fontSize = 14;
  51. //计算一行有多少个文字 取整数 向下取整
  52. var clos = Math.floor(w/fontSize);
  53. //思考每一个字的坐标
  54. //创建数组把clos 个 0 (y坐标存储起来)
  55. var drops = [];
  56. var str = "qwertyuiopasdfghjklzxcvbnm";
  57. //往数组里面添加 clos 个 0
  58. for(var i = 0;i<clos;i++) {
  59. drops.push(0);
  60. }
  61. //绘制文字
  62. function drawString() {
  63. //给矩形设置填充色
  64. ctx.fillStyle="rgba(0,0,0,0.05)"
  65. //绘制一个矩形
  66. ctx.fillRect(0,0,w,h);
  67. //添加文字样式
  68. ctx.font = "600 "+fontSize+"px 微软雅黑";
  69. //设置文字颜色
  70. ctx.fillStyle = "#00ff00";
  71. for(var i = 0;i<clos;i++) {
  72. //x坐标
  73. var x = i*fontSize;
  74. //y坐标
  75. var y = drops[i]*fontSize;
  76. //设置绘制文字
  77. ctx.fillText(str[Math.floor(Math.random()*str.length)],x,y);
  78. if(y>h&&Math.random()>0.99){
  79. drops[i] = 0;
  80. }
  81. drops[i]++;
  82. }
  83. }
  84. //定义一个定时器,每隔30毫秒执行一次
  85. setInterval(drawString,30);
  1. </script>
  2. </body>
  3. </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. function init() {
  13. var stars = document.getElementById("stars");
  14. windowWidth = window.innerWidth; //当前的窗口的高度
  15. stars.width = windowWidth;
  16. stars.height = window.innerHeight;
  17. context = stars.getContext("2d");
  18. }
  19. //创建一个星星对象
  20. var Star = 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. function drawMoon() {
  45. var moon = new Image();
  46. moon.src = "./images/moon.jpg"
  47. context.drawImage(moon, -5, -10);
  48. }
  49. //页面加载的时候
  50. window.onload = function () {
  51. init();
  52. //画星星
  53. for (var i = 0; i < starCount; i++) {
  54. var star = new Star();
  55. star.init();
  56. star.draw();
  57. arr.push(star);
  58. }
  59. //画流星
  60. for (var i = 0; i < rainCount; i++) {
  61. var rain = new MeteorRain();
  62. rain.init();
  63. rain.draw();
  64. rains.push(rain);
  65. }
  66. drawMoon();//绘制月亮
  67. playStars();//绘制闪动的星星
  68. playRains();//绘制流星
  69. }
  70. //星星闪起来
  71. function playStars() {
  72. for (var n = 0; n < starCount; n++) {
  73. arr[n].getColor();
  74. arr[n].draw();
  75. }
  76. setTimeout("playStars()", 100);
  77. }
  78. /*流星雨开始*/
  79. var MeteorRain = function () {
  80. this.x = -1;
  81. this.y = -1;
  82. this.length = -1;//长度
  83. this.angle = 30; //倾斜角度
  84. this.width = -1;//宽度
  85. this.height = -1;//高度
  86. this.speed = 1;//速度
  87. this.offset_x = -1;//横轴移动偏移量
  88. this.offset_y = -1;//纵轴移动偏移量
  89. this.alpha = 1; //透明度
  90. this.color1 = "";//流星的色彩
  91. this.color2 = ""; //流星的色彩
  92. /****************初始化函数********************/
  93. this.init = function () //初始化
  1. {
  2. this.getPos();
  3. this.alpha = 1;//透明度
  4. this.getRandomColor();
  5. //最小长度,最大长度
  6. var x = Math.random() * 80 + 150;
  7. this.length = Math.ceil(x);
  8. // x = Math.random()*10+30;
  9. this.angle = 30; //流星倾斜角
  10. x = Math.random() + 0.5;
  11. this.speed = Math.ceil(x); //流星的速度
  12. var cos = Math.cos(this.angle * 3.14 / 180);
  13. var sin = Math.sin(this.angle * 3.14 / 180);
  14. this.width = this.length * cos; //流星所占宽度
  15. this.height = this.length * sin;//流星所占高度
  16. this.offset_x = this.speed * cos;
  17. this.offset_y = this.speed * sin;
  18. }
  19. /**************获取随机颜色函数*****************/
  20. this.getRandomColor = function () {
  21. var a = Math.ceil(255 - 240 * Math.random());
  22. //中段颜色
  23. this.color1 = "rgba(" + a + "," + a + "," + a + ",1)";
  24. //结束颜色
  25. this.color2 = "black";
  26. }
  27. /***************重新计算流星坐标的函数******************/
  28. this.countPos = function ()//
  29. {
  30. //往左下移动,x减少,y增加
  31. this.x = this.x - this.offset_x;
  32. this.y = this.y + this.offset_y;
  33. }
  34. /*****************获取随机坐标的函数*****************/
  35. this.getPos = function () //
  36. {
  37. //横坐标200--1200
  38. this.x = Math.random() * window.innerWidth; //窗口高度
  39. //纵坐标小于600
  40. this.y = Math.random() * window.innerHeight; //窗口宽度
  41. }
  42. /****绘制流星***************************/
  43. this.draw = function () //绘制一个流星的函数
  44. {
  45. context.save();
  46. context.beginPath();
  47. context.lineWidth = 1; //宽度
  48. context.globalAlpha = this.alpha; //设置透明度
  49. //创建横向渐变颜色,起点坐标至终点坐标
  50. var line = context.createLinearGradient(this.x, this.y,
  51. this.x + this.width,
  52. this.y - this.height);
  53. //分段设置颜色
  54. line.addColorStop(0, "white");
  55. line.addColorStop(0.3, this.color1);
  56. line.addColorStop(0.6, this.color2);
  57. context.strokeStyle = line;
  58. //起点
  59. context.moveTo(this.x, this.y);
  60. //终点
  61. context.lineTo(this.x + this.width, this.y - this.height);
  62. context.closePath();
  63. context.stroke();
  64. context.restore();
  65. }
  66. this.move = function () {
  67. //清空流星像素
  68. var x = this.x + this.width - this.offset_x;
  69. var y = this.y - this.height;
  70. context.clearRect(x - 3, y - 3, this.offset_x + 5, this.offset_y + 5);
  71. // context.strokeStyle="red";
  72. // context.strokeRect(x,y-1,this.offset_x+1,this.offset_y+1);
  73. //重新计算位置,往左下移动
  74. this.countPos();
  75. //透明度增加
  76. this.alpha -= 0.002;
  77. //重绘
  78. this.draw();
  79. }
  80. }
  81. //绘制流星
  82. function playRains() {
  83. for (var n = 0; n < rainCount; n++) {
  84. var rain = rains[n];
  85. rain.move();//移动
  86. if (rain.y > window.inner
  1. Height) {//超出界限后重来
  2. context.clearRect(rain.x, rain.y - rain.height, rain.width, rain.height);
  3. rains[n] = new MeteorRain();
  4. rains[n].init();
  5. }
  6. }
  7. setTimeout("playRains()", 2);
  8. }
  9. /*流星雨结束*/
  10. </script>
  11. <style type="text/css">
  12. body {
  13. background-color: black;
  14. }
  15. body, html {
  16. width: 100%;
  17. height: 100%;
  18. overflow: hidden;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <canvas id="stars"></canvas>
  24. </body>
  25. </html>