当前位置:   article > 正文

HTML烟花代码

HTML烟花代码

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>2022烟花</title>
  6. <style>
  7. html, body{
  8. padding:0px;
  9. margin:0px;
  10. background:#222;
  11. font-family: 'Karla', sans-serif;
  12. color:#FFF;
  13. height:100%;
  14. overflow:hidden;
  15. }
  16. h1{
  17. z-index: 1000;
  18. position:fixed;
  19. top:50%;
  20. left:50%;
  21. transform:translateX(-50%) translateY(-100%);
  22. font-size:58px;
  23. border:2px solid #FFF;
  24. padding:7.5px 15px;
  25. background:rgba(0, 0, 0, 0.5);
  26. border-radius:3px;
  27. overflow:hidden;
  28. }
  29. span{
  30. position:relative;
  31. display:inline-block;
  32. animation: drop 0.75s;
  33. }
  34. canvas {
  35. width:100%;
  36. height:100%;
  37. }
  38. @keyframes drop {
  39. 0% {
  40. transform: translateY(-100px);
  41. opacity: 0;
  42. }
  43. 90% {
  44. opacity: 1;
  45. transform:translateY(10px);
  46. }
  47. 100% {
  48. transform:translateY(0px;)
  49. }
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <canvas></canvas>
  55. <h1>202<span>2</span></h1>
  56. <script type="text/javascript">
  57. var ctx = document.querySelector('canvas').getContext('2d')
  58. ctx.canvas.width = window.innerWidth
  59. ctx.canvas.height = window.innerHeight
  60. var sparks = []
  61. var fireworks = []
  62. var i = 20; while(i--) {
  63. fireworks.push(
  64. new Firework(Math.random()*window.innerWidth, window.innerHeight*Math.random())
  65. )
  66. }
  67. render()
  68. function render() {
  69. setTimeout(render, 1000/60)
  70. ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
  71. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
  72. for(var firework of fireworks) {
  73. if(firework.dead) continue
  74. firework.move()
  75. firework.draw()
  76. }
  77. for(var spark of sparks) {
  78. if(spark.dead) continue
  79. spark.move()
  80. spark.draw()
  81. }
  82. if(Math.random() < 0.05) {
  83. fireworks.push(new Firework())
  84. }
  85. }
  86. function Spark(x, y, color) {
  87. this.x = x
  88. this.y = y
  89. this.dir = Math.random() * (Math.PI*2)
  90. this.dead = false
  91. this.color = color
  92. this.speed = Math.random() * 3 + 3;
  93. this.walker = new Walker({ radius: 20, speed: 0.25 })
  94. this.gravity = 0.25
  95. this.dur = this.speed / 0.1
  96. this.move = function() {
  97. this.dur--
  98. if(this.dur < 0) this.dead = true
  99. if(this.speed < 0) return
  100. if(this.speed > 0) this.speed -= 0.1
  101. var walk = this.walker.step()
  102. this.x += Math.cos(this.dir + walk) * this.speed
  103. this.y += Math.sin(this.dir + walk) * this.speed
  104. this.y += this.gravity
  105. this.gravity += 0.05
  106. }
  107. this.draw = function() {
  108. drawCircle(this.x, this.y, 3, this.color)
  109. }
  110. }
  111. function Firework(x, y) {
  112. this.xmove = new Walker({radius: 10, speed: 0.5})
  113. this.x = x || Math.random() * ctx.canvas.width
  114. this.y = y || ctx.canvas.height
  115. this.height = Math.random()*ctx.canvas.height/2
  116. this.dead = false
  117. this.color = randomColor()
  118. this.move = function() {
  119. this.x += this.xmove.step()
  120. if(this.y > this.height) this.y -= 1;
  121. else this.burst()
  122. }
  123. this.draw = function() {
  124. drawCircle(this.x, this.y, 1, this.color)
  125. }
  126. this.burst = function() {
  127. this.dead = true
  128. var i = 100; while(i--) sparks.push(new Spark(this.x, this.y, this.color))
  129. }
  130. }
  131. function drawCircle(x, y, radius, color) {
  132. color = color || '#FFF'
  133. ctx.fillStyle = color
  134. ctx.fillRect(x-radius/2, y-radius/2, radius, radius)
  135. }
  136. function randomColor(){
  137. return ['#6ae5ab','#88e3b2','#36b89b','#7bd7ec','#66cbe1'][Math.floor(Math.random() * 5)];
  138. }
  139. function Walker(options){
  140. this.step = function(){
  141. this.direction = Math.sign(this.target) * this.speed
  142. this.value += this.direction
  143. this.target
  144. ? this.target -= this.direction
  145. : (this.value)
  146. ? (this.wander)
  147. ? this.target = this.newTarget()
  148. : this.target = -this.value
  149. : this.target = this.newTarget()
  150. return this.direction
  151. }
  152. this.newTarget = function() {
  153. return Math.round(Math.random()*(this.radius*2)-this.radius)
  154. }
  155. this.start = 0
  156. this.value = 0
  157. this.radius = options.radius
  158. this.target = this.newTarget()
  159. this.direction = Math.sign(this.target)
  160. this.wander = options.wander
  161. this.speed = options.speed || 1
  162. }
  163. </script>
  164. <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
  165. <p>适用浏览器:360、FireFox、Chrome、Opera、傲游、搜狗、世界之窗. 不支持Safari、IE8及以下浏览器。</p>
  166. </div>
  167. <script>document.write('<script src="//' + (location.host || 'localhost').split(':')[0] + ':35929/livereload.js?snipver=1"></' + 'script>')</script><script>document.addEventListener('LiveReloadDisconnect', function() { setTimeout(function() { window.location.reload(); }, 500); })</script></body>
  168. </html>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号