鼠标点击爆炸

拼搏现实的明天。 2024-04-07 12:52 267阅读 0赞

直接引入以下js文件

  1. function clickEffect() {
  2. let balls = [];
  3. let longPressed = false;
  4. let longPress;
  5. let multiplier = 0;
  6. let width, height;
  7. let origin;
  8. let normal;
  9. let ctx;
  10. const colours = ["#F73859", "#14FFEC", "#00E0FF", "#FF99FE", "#FAF15D"];
  11. const canvas = document.createElement("canvas");
  12. document.body.appendChild(canvas);
  13. canvas.setAttribute("style", "width: 100%; height: 100%; top: 0; left: 0; z-index: 99999; position: fixed; pointer-events: none;");
  14. const pointer = document.createElement("span");
  15. pointer.classList.add("pointer");
  16. document.body.appendChild(pointer);
  17. if (canvas.getContext && window.addEventListener) {
  18. ctx = canvas.getContext("2d");
  19. updateSize();
  20. window.addEventListener('resize', updateSize, false);
  21. loop();
  22. window.addEventListener("mousedown", function(e) {
  23. pushBalls(randBetween(10, 20), e.clientX, e.clientY);
  24. document.body.classList.add("is-pressed");
  25. longPress = setTimeout(function(){
  26. document.body.classList.add("is-longpress");
  27. longPressed = true;
  28. }, 500);
  29. }, false);
  30. window.addEventListener("mouseup", function(e) {
  31. clearInterval(longPress);
  32. if (longPressed == true) {
  33. document.body.classList.remove("is-longpress");
  34. pushBalls(randBetween(50 + Math.ceil(multiplier), 100 + Math.ceil(multiplier)), e.clientX, e.clientY);
  35. longPressed = false;
  36. }
  37. document.body.classList.remove("is-pressed");
  38. }, false);
  39. window.addEventListener("mousemove", function(e) {
  40. let x = e.clientX;
  41. let y = e.clientY;
  42. pointer.style.top = y + "px";
  43. pointer.style.left = x + "px";
  44. }, false);
  45. } else {
  46. console.log("canvas or addEventListener is unsupported!");
  47. }
  48. function updateSize() {
  49. canvas.width = window.innerWidth * 2;
  50. canvas.height = window.innerHeight * 2;
  51. canvas.style.width = window.innerWidth + 'px';
  52. canvas.style.height = window.innerHeight + 'px';
  53. ctx.scale(2, 2);
  54. width = (canvas.width = window.innerWidth);
  55. height = (canvas.height = window.innerHeight);
  56. origin = {
  57. x: width / 2,
  58. y: height / 2
  59. };
  60. normal = {
  61. x: width / 2,
  62. y: height / 2
  63. };
  64. }
  65. class Ball {
  66. constructor(x = origin.x, y = origin.y) {
  67. this.x = x;
  68. this.y = y;
  69. this.angle = Math.PI * 2 * Math.random();
  70. if (longPressed == true) {
  71. this.multiplier = randBetween(14 + multiplier, 15 + multiplier);
  72. } else {
  73. this.multiplier = randBetween(6, 12);
  74. }
  75. this.vx = (this.multiplier + Math.random() * 0.5) * Math.cos(this.angle);
  76. this.vy = (this.multiplier + Math.random() * 0.5) * Math.sin(this.angle);
  77. this.r = randBetween(8, 12) + 3 * Math.random();
  78. this.color = colours[Math.floor(Math.random() * colours.length)];
  79. }
  80. update() {
  81. this.x += this.vx - normal.x;
  82. this.y += this.vy - normal.y;
  83. normal.x = -2 / window.innerWidth * Math.sin(this.angle);
  84. normal.y = -2 / window.innerHeight * Math.cos(this.angle);
  85. this.r -= 0.3;
  86. this.vx *= 0.9;
  87. this.vy *= 0.9;
  88. }
  89. }
  90. function pushBalls(count = 1, x = origin.x, y = origin.y) {
  91. for (let i = 0; i < count; i++) {
  92. balls.push(new Ball(x, y));
  93. }
  94. }
  95. function randBetween(min, max) {
  96. return Math.floor(Math.random() * max) + min;
  97. }
  98. function loop() {
  99. ctx.fillStyle = "rgba(255, 255, 255, 0)";
  100. ctx.clearRect(0, 0, canvas.width, canvas.height);
  101. for (let i = 0; i < balls.length; i++) {
  102. let b = balls[i];
  103. if (b.r < 0) continue;
  104. ctx.fillStyle = b.color;
  105. ctx.beginPath();
  106. ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2, false);
  107. ctx.fill();
  108. b.update();
  109. }
  110. if (longPressed == true) {
  111. multiplier += 0.2;
  112. } else if (!longPressed && multiplier >= 0) {
  113. multiplier -= 0.4;
  114. }
  115. removeBall();
  116. requestAnimationFrame(loop);
  117. }
  118. function removeBall() {
  119. for (let i = 0; i < balls.length; i++) {
  120. let b = balls[i];
  121. if (b.x + b.r < 0 || b.x - b.r > width || b.y + b.r < 0 || b.y - b.r > height || b.r < 0) {
  122. balls.splice(i, 1);
  123. }
  124. }
  125. }
  126. }
  127. clickEffect();

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读

    相关 jQuery鼠标事件

    概念 > 个人理解: > > jQuery鼠标点击事件是最常用的事件之一,当用户使用鼠标在浏览器窗口或元素上进行点击交互时触发的事件,都属于鼠标点击事件的范围。常见的鼠