You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改代码实现目标鼠标拖尾动画效果?

如何修改代码实现目标鼠标拖尾动画效果?

看起来你想要的是那种离散渐变的霓虹光斑拖尾效果,而不是当前代码里的连续线条拖尾对吧?我帮你调整一下代码,让它更贴近你想要的效果,直接看修改后的完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Neon Cursor Trail</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        body {
            background: #0a0a0a; /* 深色背景突出霓虹效果 */
            overflow: hidden;
            margin: 0;
        }
        canvas {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: 50;
        }
    </style>
</head>
<body>
    <h1 class="text-white text-4xl text-center pt-20">Neon Cursor Trail ⚡</h1>
    <canvas id="cursorSlice"></canvas>
    <script>
        const canvas = document.getElementById("cursorSlice");
        const ctx = canvas.getContext("2d");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        let mouse = { x: 0, y: 0 };
        let trail = [];
        const TRAIL_DURATION = 800; // 拖尾消失时长(毫秒)
        const MAX_SIZE = 12; // 光斑最大尺寸

        window.addEventListener("resize", () => {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
        });

        document.addEventListener("mousemove", (e) => {
            mouse.x = e.clientX;
            mouse.y = e.clientY;
            // 每个拖尾点存储位置、创建时间,以及初始尺寸
            trail.push({
                x: mouse.x,
                y: mouse.y,
                time: Date.now(),
                initialSize: MAX_SIZE * (0.5 + Math.random() * 0.5) // 随机大小增加自然感
            });
        });

        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            const now = Date.now();

            // 遍历所有拖尾点,逐个绘制光斑
            for (let i = trail.length - 1; i >= 0; i--) {
                const point = trail[i];
                const age = now - point.time;

                // 如果超过生命周期,移除该点
                if (age > TRAIL_DURATION) {
                    trail.splice(i, 1);
                    continue;
                }

                // 计算当前点的透明度和尺寸(随时间衰减)
                const progress = age / TRAIL_DURATION;
                const opacity = 1 - progress;
                const size = point.initialSize * (1 - progress);

                // 创建径向渐变,模拟霓虹光斑的中心亮边缘暗效果
                const gradient = ctx.createRadialGradient(
                    point.x, point.y, 0,
                    point.x, point.y, size
                );
                gradient.addColorStop(0, `rgba(0, 255, 255, ${opacity})`);
                gradient.addColorStop(0.5, `rgba(255, 0, 255, ${opacity * 0.8})`);
                gradient.addColorStop(1, `rgba(255, 255, 0, ${opacity * 0.3})`);

                // 设置绘制样式
                ctx.fillStyle = gradient;
                ctx.shadowColor = "#00ffff";
                ctx.shadowBlur = 15 * opacity; // 发光效果随透明度衰减

                // 绘制圆形光斑
                ctx.beginPath();
                ctx.arc(point.x, point.y, size, 0, Math.PI * 2);
                ctx.fill();
            }

            requestAnimationFrame(animate);
        }

        animate();
    </script>
</body>
</html>

关键修改点说明:

  • 从连线改为离散光斑:原来的代码是把所有鼠标点连成线条,现在改成逐个绘制圆形光斑,更符合常见的“拖尾”视觉效果
  • 动态衰减效果:每个光斑会随着时间推移逐渐缩小、透明,直到完全消失,模拟自然的拖尾淡出
  • 径向渐变优化:用径向渐变替代线性渐变,让每个光斑有中心亮、边缘过渡的霓虹质感,增加层次感
  • 随机化初始尺寸:给每个拖尾点随机一个初始大小,让拖尾看起来更自然,不会过于规整
  • 反向遍历移除元素:从后往前遍历拖尾数组,避免删除元素时打乱遍历顺序

你可以根据自己的需求调整TRAIL_DURATION(拖尾时长)和MAX_SIZE(光斑最大尺寸)这两个常量,来适配你想要的效果~

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 08:14:35