当前位置:   article > 正文

JS实现新年倒计时动画特效+音乐案例_javascript动画demo音乐

javascript动画demo音乐

效果图:

 源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <style>
  6. body{
  7. overflow: hidden;
  8. margin: 0;
  9. }
  10. h1{
  11. position: fixed;
  12. top: 30%;
  13. left: 0;
  14. width: 100%;
  15. text-align: center;
  16. transform:translateY(-50%);
  17. font-family: 'Love Ya Like A Sister', cursive;
  18. font-size: 60px;
  19. color: #c70012;
  20. padding: 0 20px;
  21. }
  22. h1 span{
  23. position: fixed;
  24. left: 0;
  25. width: 100%;
  26. text-align: center;
  27. margin-top:30px;
  28. font-size:40px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <h1 id="h1"></h1>
  34. <canvas></canvas> <!--canvas 画布-->
  35. <script>
  36. var canvas = document.querySelector("canvas"),
  37. ctx = canvas.getContext("2d");
  38. var ww,wh;
  39. function onResize(){
  40. ww = canvas.width = window.innerWidth;
  41. wh = canvas.height = window.innerHeight;
  42. }
  43. ctx.strokeStyle = "red";
  44. ctx.shadowBlur = 25;
  45. ctx.shadowColor = "hsla(0, 100%, 60%,0.5)";
  46. var precision = 100;
  47. var hearts = [];
  48. var mouseMoved = false;
  49. function onMove(e){
  50. mouseMoved = true;
  51. if(e.type === "touchmove"){
  52. hearts.push(new Heart(e.touches[0].clientX, e.touches[0].clientY));
  53. hearts.push(new Heart(e.touches[0].clientX, e.touches[0].clientY));
  54. }
  55. else{
  56. hearts.push(new Heart(e.clientX, e.clientY));
  57. hearts.push(new Heart(e.clientX, e.clientY));
  58. }
  59. }
  60. var Heart = function(x,y){
  61. this.x = x || Math.random()*ww;
  62. this.y = y || Math.random()*wh;
  63. this.size = Math.random()*2 + 1;
  64. this.shadowBlur = Math.random() * 10;
  65. this.speedX = (Math.random()+0.2-0.6) * 8;
  66. this.speedY = (Math.random()+0.2-0.6) * 8;
  67. this.speedSize = Math.random()*0.05 + 0.01;
  68. this.opacity = 1;
  69. this.vertices = [];
  70. for (var i = 0; i < precision; i++) {
  71. var step = (i / precision - 0.5) * (Math.PI * 2);
  72. var vector = {
  73. x : (15 * Math.pow(Math.sin(step), 3)),
  74. y : -(13 * Math.cos(step) - 5 * Math.cos(2 * step) - 2 * Math.cos(3 * step) - Math.cos(4 * step))
  75. }
  76. this.vertices.push(vector);
  77. }
  78. }
  79. Heart.prototype.draw = function(){
  80. this.size -= this.speedSize;
  81. this.x += this.speedX;
  82. this.y += this.speedY;
  83. ctx.save();
  84. ctx.translate(-1000,this.y);
  85. ctx.scale(this.size, this.size);
  86. ctx.beginPath();
  87. for (var i = 0; i < precision; i++) {
  88. var vector = this.vertices[i];
  89. ctx.lineTo(vector.x, vector.y);
  90. }
  91. ctx.globalAlpha = this.size;
  92. ctx.shadowBlur = Math.round((3 - this.size) * 10);
  93. ctx.shadowColor = "hsla(0, 100%, 60%,0.5)";
  94. ctx.shadowOffsetX = this.x + 1000;
  95. ctx.globalCompositeOperation = "screen"
  96. ctx.closePath();
  97. ctx.fill()
  98. ctx.restore();
  99. };
  100. function render(a){
  101. requestAnimationFrame(render);
  102. hearts.push(new Heart())
  103. ctx.clearRect(0,0,ww,wh);
  104. for (var i = 0; i < hearts.length; i++) {
  105. hearts[i].draw();
  106. if(hearts[i].size <= 0){
  107. hearts.splice(i,1);
  108. i--;
  109. }
  110. }
  111. }
  112. onResize();
  113. window.addEventListener("mousemove", onMove);
  114. window.addEventListener("touchmove", onMove);
  115. window.addEventListener("resize", onResize);
  116. requestAnimationFrame(render);
  117. window.onload=function starttime(){
  118. time(h1,'2023/2/1'); // 2021年春节时间
  119. ptimer = setTimeout(starttime,1000); // 添加计时器
  120. }
  121. function time(obj,futimg){
  122. var nowtime = new Date().getTime(); // 现在时间转换为时间戳
  123. var futruetime = new Date(futimg).getTime(); // 未来时间转换为时间戳
  124. var msec = futruetime-nowtime; // 毫秒 未来时间-现在时间
  125. var time = (msec/1000); // 毫秒/1000
  126. var day = parseInt(time/86400); // 天 24*60*60*1000
  127. var hour = parseInt(time/3600)-24*day; // 小时 60*60 总小时数-过去的小时数=现在的小时数
  128. var minute = parseInt(time%3600/60); // 分 -(day*24) 以60秒为一整份 取余 剩下秒数 秒数/60 就是分钟数
  129. var second = parseInt(time%60); // 以60秒为一整份 取余 剩下秒数
  130. obj.innerHTML="<br>距离春节还有:<br>"+day+"天"+hour+"小时"+minute+"分"+second+"秒"+"<br><span>愿我所念的人平安喜乐,<br>愿我所想的事顺心如意。<br>May the people I think of be safe and happy, <br>and may the things I think of be all right.</span>"
  131. return true;
  132. }
  133. </script>
  134. <audio id="bgmusic" src="http://music.163.com/song/media/outer/url?id=1851244378.mp3" autoplay="autoplay" loop="loop" style="display: block; width: 3%; height:3%;"></audio>
  135. <script type="text/javascript">
  136. document.addEventListener('DOMContentLoaded', function () {
  137. function audioAutoPlay() {
  138. var audio = document.getElementById('bgmusic');
  139. audio.play();
  140. document.addEventListener("WeixinJSBridgeReady", function () {
  141. audio.play();
  142. }, false);
  143. }
  144. audioAutoPlay();
  145. });
  146. </script>
  147. </body>
  148. </html>

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

闽ICP备14008679号