html星空特效代码

html星空特效代码怎么写

发布 : web前端培训   发布时间:2021-10-21 09:37:35

品牌型号:联想 YOGA 14c/系统版本:windows7
  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>星空特效</title>
  6. <style>
  7. *{
  8. margin: 0;
  9. padding: 0;
  10. }
  11. body{
  12. overflow: hidden;
  13. background-color: #000;
  14. }
  15. h1{
  16. position: absolute;
  17. line-height: 50px;
  18. letter-spacing: 5px;
  19. color: #fff;
  20. width: 300px;
  21. top: 40%;
  22. left: 50%;
  23. margin-left: -200px;
  24. font-size: 30px;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <h1>
  30. </h1>
  31. <canvas></canvas>
  32. <script>
  33. var canvas=document.querySelector("canvas");
  34. ctx=canvas.getContext("2d");
  35. w=canvas.width=window.innerWidth;
  36. h=canvas.height=window.innerHeight;
  37. var canvas2=document.createElement("canvas");
  38. ctx2=canvas2.getContext("2d");
  39. canvas2.width=100;
  40. canvas2.height=100;
  41. var a=canvas2.width/2;
  42. var grandient=ctx.createRadialGradient(a,a,0,a,a,a);
  43. grandient.addColorStop(0.025,&#39;#fff&#39;);
  44. grandient.addColorStop(0.1, &#39;hsl(220,59%,18%)&#39;);
  45. grandient.addColorStop(0.025, &#39;hsl(220,60%,33%)&#39;);
  46. grandient.addColorStop(1,"transparent");
  47. ctx2.fillStyle=grandient;
  48. ctx2.beginPath();
  49. ctx2.arc(a,a,a,0,Math.PI*2);
  50. ctx2.fill();
  51. ctx2.closePath();
  52. var stars=[];
  53. var count=0;
  54. function Star(){
  55. this.pos=Math.floor(Math.random()* w/2-100);
  56. this.r=Math.floor(Math.random()*100);
  57. this.dx=w/2;
  58. this.dy=h/2;
  59. this.rand=Math.floor(Math.random()*360);
  60. this.speed=this.pos/100000;
  61. stars[count]=this;
  62. count ++;
  63. }
  64. Star.prototype.draw=function(){
  65. var x=Math.sin(this.rand+1)* this.pos+this.dx;
  66. y=Math.cos(this.rand)*this.pos/2+this.dy;
  67. ctx.drawImage(canvas2,x-this.r/2,y-this.r/2,this.r,this.r);
  68. this.rand=this.rand+this.speed;
  69. }
  70. for(var i=0;i<1000;i++){
  71. new Star();
  72. }
  73. function anmit(){
  74. ctx.clearRect(0,0,w,h);
  75. for(var i=0;i<stars.length;i++){
  76. stars[i].draw();
  77. }
  78. requestAnimationFrame(anmit);
  79. }
  80. anmit();
  81. var oH=document.getElementsByTagName("h1")[0];
  82. var arr=["浩瀚星辰"],
  83. index=0,
  84. arrLen=arr.length,
  85. strLen=arr[0].length;
  86. pos=0,
  87. row=0,
  88. str="",
  89. timer=null;
  90. function print() {
  91. while(row<index){
  92. str=str+arr[row]+"<br>";
  93. row++;
  94. }
  95. oH.innerHTML=str+arr[index].substring(0,pos);
  1. if(pos==strLen){
  2. index++;
  3. pos =0;
  4. if(index<arr.length){
  5. strLen=arr[index].length;
  6. timer=setTimeout(print,250);
  7. }
  8. }else{
  9. pos++;
  10. timer=setTimeout(print,250);
  11. }
  12. }
  13. setTimeout(print,250);
  14. </script>
  15. </body>
  16. </html>


其它答案
冰阔落2020-06-22 18:56:36
  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>星空特效</title>
  5. <link rel="stylesheet" href="css/index.css" />
  6. </head>
  7. <body>
  8. <div id="background" class="wall"></div>
  9. <div id="midground" class="wall"></div>
  10. <div id="foreground" class="wall"></div>
  11. <div id="top" class="wall"></div>
  12. <div id="ship" class="wall"></div>
  13. </body>
  14. </html>


 相关推荐