您现在的位置是:首页 > 编程语言学习 > 前端编程语言 > 文章正文 前端编程语言

JavaScript Canvas实现兼容IE的兔子发射爆破动图特效

2023-01-12 16:06:28 前端编程语言

简介Hello,同学们好!又是一年新春之际,祝福大家兔年快乐!给大家介绍一个有趣的动效(兼容 IE),页面右下角有一只搞怪的兔子,鼠标在页面...

Hello,同学们好!又是一年新春之际,祝福大家兔年快乐!给大家介绍一个有趣的动效(兼容 IE),页面右下角有一只搞怪的兔子,鼠标在页面中悬停时,兔子会跟着做出不同的动作和表情。然后可以在页面中任意位置(离兔子太近不能发射,会伤到兔子😆)点击鼠标,将从兔子眼睛👀里发射炮弹,随之击中爆破的是你的霉 运、压 力、贫 穷、疾 病😮‍💨。

实现

  • 定义一个随机文本块。
  1. <p id="p1"></p> 
  • 定义兔子的构造函数。
  1. function HoverRabbit() { 
  2.   this.explodeImage = new Image(); 
  3.   this.explodeImage.src = "img/explode.png"
  4.   for (var i = 1; i &lt;= 6; i++) { 
  5. this.images[i] = new Image(); 
  6. this.images[i].src = "img/s" + i + ".png"
  7.   } 
  8.   if (typeof(CanvasGradient) != 'undefined') { 
  9. this.canvas = document.createElement("canvas"); 
  10. this.canvas.width = screen.width + 100; 
  11. this.canvas.height = screen.height; 
  12. this.canvas.style.position = 'absolute'
  13. this.canvas.style.left = '0px'
  14. this.canvas.style.top = '0px'
  15. this.canvas.style.display = 'none'
  16. document.body.appendChild(this.canvas); 
  17. this.canvas.style.position = 'fixed'
  18.   } 
  • 定义兔子原型的属性和方法。
  1. HoverRabbit.prototype = { 
  2.   images: [], 
  3.   explodeImage: null
  4.   eyePositions: [], 
  5.   current: 1, 
  6.   frame: 1, 
  7.   canvas: null
  8.   interval: null
  9.   start: function() { 
  10. var me = this
  11. this.eyePositions[1] = { 
  12.   eye1x: 123, 
  13.   eye1y: 47, 
  14.   eye2x: 104, 
  15.   eye2y: 53 
  16. }; 
  17. this.eyePositions[2] = { 
  18.   eye1x: 120, 
  19.   eye1y: 50, 
  20.   eye2x: 101, 
  21.   eye2y: 54 
  22. }; 
  23. this.eyePositions[3] = { 
  24.   eye1x: 119, 
  25.   eye1y: 54, 
  26.   eye2x: 97, 
  27.   eye2y: 56 
  28. }; 
  29. this.eyePositions[4] = { 
  30.   eye1x: 112, 
  31.   eye1y: 61, 
  32.   eye2x: 90, 
  33.   eye2y: 61 
  34. }; 
  35. this.eyePositions[5] = { 
  36.   eye1x: 105, 
  37.   eye1y: 64, 
  38.   eye2x: 85, 
  39.   eye2y: 61 
  40. }; 
  41. this.eyePositions[6] = { 
  42.   eye1x: 98, 
  43.   eye1y: 68, 
  44.   eye2x: 79, 
  45.   eye2y: 56 
  46. }; 
  47. document.onmousemove = function(e) { 
  48.   me.onmousemove(e); 
  49. if (this.canvas) { 
  50.   document.addEventListener("click"function(e) { 
  51. me.ondblclick(e); 
  52.   }); 
  53.   }, 
  54.   onmousemove: function(e) { 
  55. var event = e || window.event; 
  56. var deg = Math.abs(screen.height - event.screenY) / (Math.abs(screen.width - event.screenX) + 1); 
  57. var n = 1; 
  58. if (deg > 2) n = 6; 
  59. else if (deg > 1.4) n = 5; 
  60. else if (deg > 0.7) n = 4; 
  61. else if (deg > 0.45) n = 3; 
  62. else if (deg > 0.2) n = 2; 
  63. this.deg = n; 
  64. if (this.current != n) { 
  65.   document.body.style.backgroundImage = "url(" + this.images[n].src + ")"
  66.   this.current = n; 
  67.   }, 
  68.   drawBomb: function(ctxt, n, x, y) { 
  69. var sx = 64 * (n % 4); 
  70. var sy = 64 * (Math.floor(n / 4)); 
  71. ctxt.drawImage(this.explodeImage, sx, sy, 64, 64, x - 32, y - 32, 64, 64); 
  72.   }, 
  73.   ondblclick: function(e) { 
  74. if (this.canvas.style.display != 'none'return
  75. var me = this
  76. if (e.clientX > window.innerWidth - 200 && e.clientY > window.innerHeight - 200) return
  77. var ctxt = this.canvas.getContext("2d"); 
  78. this.frame = 1; 
  79. this.interval = setInterval(function(e2) { 
  80.   ctxt.clearRect(0, 0, me.canvas.width, me.canvas.height); 
  81.   switch (me.frame) { 
  82. case 1: 
  83.   ctxt.strokeStyle = 'rgba(247,166,71,1)'
  84.   me.canvas.style.display = 'block'
  85. case 2: 
  86.   if (me.frame == 2) { 
  87. ctxt.strokeStyle = 'rgba(247,166,71,0.5)'
  88. me.drawBomb(ctxt, 0, e.clientX, e.clientY); 
  89.   } 
  90.   case 3: 
  91. if (me.frame == 3) { 
  92.   ctxt.strokeStyle = 'rgba(247,166,71,0.1)'
  93.   me.drawBomb(ctxt, 1, e.clientX, e.clientY); 
  94. var eye1x = window.innerWidth - me.eyePositions[me.current].eye1x; 
  95. var eye1y = window.innerHeight - me.eyePositions[me.current].eye1y; 
  96. ctxt.lineWidth = 3; 
  97. ctxt.beginPath(); 
  98. ctxt.moveTo(eye1x, eye1y); 
  99. ctxt.lineTo(e.clientX, e.clientY); 
  100. ctxt.stroke(); 
  101. var eye2x = window.innerWidth - me.eyePositions[me.current].eye2x; 
  102. var eye2y = window.innerHeight - me.eyePositions[me.current].eye2y; 
  103. ctxt.beginPath(); 
  104. ctxt.moveTo(eye2x, eye2y); 
  105. ctxt.lineTo(e.clientX, e.clientY); 
  106. p1.textContent = ['霉 运''压 力''贫 穷''疾 病'][Math.floor(Math.random() * 4)]; 
  107. p1.style.display = 'block'
  108. p1.style.transform = 'rotate(' + (-150 + me.deg * 30) + 'deg)'
  109. p1.style.left = e.clientX - 30 + 'px'
  110. p1.style.top = e.clientY - 30 + 'px'
  111. fade(p1); 
  112. ctxt.stroke(); 
  113. break
  114.   case 4: 
  115. me.drawBomb(ctxt, 2, e.clientX, e.clientY); 
  116. break
  117.   case 14: 
  118. me.canvas.style.display = 'none'
  119. window.clearInterval(me.interval); 
  120. break
  121.   default
  122. me.drawBomb(ctxt, me.frame - 2, e.clientX, e.clientY); 
  123.   } 
  124.   me.frame++; 
  125. }, 50); 
  126.   } 
  127. }; 

各个属性和方法说明:

  • images - 兔子不同的动作的图片数组。
  • explodeImage - 炮弹图片元素。
  • eyePositions - 兔子眼睛位置的数组。
  • current - 整型数字,兔子当前动作的指针。
  • frame - 整型数字,发射炮弹动画的帧数指针。
  • canvas - 画布元素。
  • interval - 发射炮弹动画时间间隔定时器的 interval ID。
  • start - 启动页面交互的方法,在这里初始化了兔子眼睛位置的数组数据,绑定页面鼠标移动事件、点击事件。
  • onmousemove - 定义页面鼠标移动的实现方法。
  • ondblclick - 定义页面鼠标点击的实现方法。
  • drawBomb - 定义绘制和更新炮弹动画的方法。
  • 定义文字淡出的动画。
  1. function fade(e) { 
  2.   var s = e.style; 
  3.   s.opacity = 1; 
  4.   (function hide() { 
  5. (s.opacity -= .01) < 0 ? s.display = "none" : requestAnimationFrame(hide); 
  6.   })(); 
  • 创建兔子对象,调用启动交互方法。
  1. var s = new HoverRabbit(); 
  2. s.start(); 

 

相关文章

站点信息