跳到正文

闲话

鼠标点击特效

Super_Diu约 10 分钟
原文
文章信息与版本

文章信息

收录
2026年2月3日
更新
2026年5月18日
篇幅
约 10 分钟

版本

  1. v1当前
JavaScript
// ==UserScript==
// @name         简约键盘输入特效
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  粒子偏上飞+微提速+微放大,简约不花哨的鼠标/键盘特效
// @author       You
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

function clickEffect() {
  let balls = [];
  let longPressed = false;
  let longPress;
  let multiplier = 0;
  let width, height;
  let origin;
  let normal;
  let ctx;
  // 保留3种柔和颜色,简约不刺眼
  const colours = ["#6090FE","#78F489","#50FEF8"];
  const canvas = document.createElement("canvas");
  document.body.appendChild(canvas);
  canvas.setAttribute("style", "width: 100%; height: 100%; top: 0; left: 0; z-index: 99999; position: fixed; pointer-events: none;");
  const pointer = document.createElement("span");
  pointer.classList.add("pointer");
  document.body.appendChild(pointer);

  if (canvas.getContext && window.addEventListener) {
    ctx = canvas.getContext("2d");
    updateSize();
    window.addEventListener('resize', updateSize, false);
    loop();
    window.addEventListener("mousedown", function(e) {
      pushBalls(randBetween(10,20), e.clientX, e.clientY);
      document.body.classList.add("is-pressed");
      longPress = setTimeout(function(){
        document.body.classList.add("is-longpress");
        longPressed = true;
      }, 500);
    }, false);
    window.addEventListener("keydown", function(e) {
      pushBalls(randBetween(5,10),randBetween(0,width),randBetween(0,height));
    }, false);
    window.addEventListener("mouseup", function(e) {
      clearInterval(longPress);
      if (longPressed == true) {
        document.body.classList.remove("is-longpress");
        pushBalls(randBetween(20 + Math.ceil(multiplier), 30 + Math.ceil(multiplier)), e.clientX, e.clientY);
        longPressed = false;
      }
      document.body.classList.remove("is-pressed");
    }, false);
    window.addEventListener("mousemove", function(e) {
      let x = e.clientX;
      let y = e.clientY;
      pointer.style.top = y + "px";
      pointer.style.left = x + "px";
    }, false);
  } else {
    console.log("canvas or addEventListener is unsupported!");
  }

  function updateSize() {
    canvas.width = window.innerWidth * 2;
    canvas.height = window.innerHeight * 2;
    canvas.style.width = window.innerWidth + 'px';
    canvas.style.height = window.innerHeight + 'px';
    ctx.scale(2, 2);
    width = (canvas.width = window.innerWidth);
    height = (canvas.height = window.innerHeight);
    origin = {
      x: width / 2,
      y: height / 2
    };
    normal = {
      x: width / 2,
      y: height / 2
    };
  }
  class Ball {
    constructor(x = origin.x, y = origin.y) {
      this.x = x;
      this.y = y;
      this.cnt = 0;
      // 核心调整1:粒子角度限制在【-120° ~ 60°】,强制初始更偏向向上飞(无向下的初始角度)
      this.angle = Math.PI * (-2/3 + Math.random() * 2);
      if (longPressed == true) {
        this.multiplier = randBetween(8 + multiplier, 9 + multiplier);
      } else {
        this.multiplier = randBetween(3, 6);
      }
      // 核心调整2:速度微增(原1 → 1.1),只有一点点提速,不夸张
      this.vx = 1.1*(this.multiplier + Math.random() * 0.5) * Math.cos(this.angle);
      this.vy = 1.1*(this.multiplier + Math.random() * 0.5) * Math.sin(this.angle);
      // 核心调整3:粒子微放大(原1~3px → 1.5~3.5px),视觉上稍明显但不花哨
      this.r = 1.5+Math.random()*2;
      this.color = colours[Math.floor(Math.random() * colours.length)];
    }
    update() {
      this.x += this.vx - normal.x;
      this.y += this.vy - normal.y;
      this.r-=0.015;
      normal.x = -2 / window.innerWidth * Math.sin(this.angle);
      normal.y = -2 / window.innerHeight * Math.cos(this.angle);
      // 重力保持0.8,避免粒子快速下落抵消向上飞的效果
      this.vy +=0.8;
      if(this.y+this.r>height||this.y-this.r<0){
          this.vy=-this.vy*0.95;
      }
      if(this.x+this.r>width||this.x-this.r<0){
          this.vx=-this.vx*0.95;
      }
    }
  }

  function pushBalls(count = 1, x = origin.x, y = origin.y) {
    for (let i = 0; i < count; i++) {
      balls.push(new Ball(x, y));
    }
  }

  function randBetween(min, max) {
    return Math.floor(Math.random() * max) + min;
  }

  function loop() {
    ctx.fillStyle = "rgba(255, 255, 255, 0)";
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (let i = 0; i < balls.length; i++) {
      let b = balls[i];
      if (b.r < 0) continue;
      ctx.fillStyle = b.color;
      ctx.beginPath();
      ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2, false);
      ctx.fill();
      b.update();
    }
    if (longPressed == true) {
      multiplier += 0.1;
    } else if (!longPressed && multiplier >= 0) {
      multiplier -= 0.2;
    }
    removeBall();
    requestAnimationFrame(loop);
  }

  function removeBall() {
    for (let i = 0; i < balls.length; i++) {
      let b = balls[i];
      if (b.r<0) {
        balls.splice(i, 1);
      }
    }
  }
}
clickEffect();//调用特效函数

})();
原文