html--鼠标跟随特效

html 复制代码
<!DOCTYPE html > 
<head>
 <title></title>
 <script type="text/javascript">
window.onload = function () {
    C = Math.cos; // cache Math objects
    S = Math.sin;
    U = 0;
    w = window;
    j = document;
    d = j.getElementById("c");
    c = d.getContext("2d");
    W = d.width = w.innerWidth;
    H = d.height = w.innerHeight;
    c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default)
    c.globalCompositeOperation = "lighter"; // switch to additive color application
    c.lineWidth = 0.2;
    c.lineCap = "round";
    var bool = 0, 
        t = 0; // theta
    d.onmousemove = function (e) {
        if(window.T) {
            if(D==9) { D=Math.random()*15; f(1); }
            clearTimeout(T);
        }
        X = e.pageX; // grab mouse pixel coords
        Y = e.pageY;
        a=0; // previous coord.x
        b=0; // previous coord.y 
        A = X, // original coord.x
        B = Y; // original coord.y
        R=(e.pageX/W * 999>>0)/999;
        r=(e.pageY/H * 999>>0)/999;
        U=e.pageX/H * 360 >>0;
        D=9;
        g = 360 * Math.PI / 180;
        T = setInterval(f = function (e) { // start looping spectrum
            c.save();
            c.globalCompositeOperation = "source-over"; // switch to additive color application
            if(e!=1) {
                c.fillStyle = "rgba(0,0,0,0.02)";
                c.fillRect(0, 0, W, H); // resize <canvas> and draw black rect (default)
            }
            c.restore();
            i = 25; while(i --) {
                c.beginPath();
                if(D > 450 || bool) { // decrease diameter
                    if(!bool) { // has hit maximum
                        bool = 1;
                    }
                    if(D < 0.1) { // has hit minimum
                        bool = 0;
                    }
                    t -= g; // decrease theta
                    D -= 0.1; // decrease size
                }
                if(!bool) {
                    t += g; // increase theta
                    D += 0.1; // increase size
                }
                q = (R / r - 1) * t; // create hypotrochoid from current mouse position, and setup variables (see: http://en.wikipedia.org/wiki/Hypotrochoid)
                x = (R - r) * C(t) + D * C(q) + (A + (X - A) * (i / 25)) + (r - R); // center on xy coords
                y = (R - r) * S(t) - D * S(q) + (B + (Y - B) * (i / 25));
                if (a) { // draw once two points are set
                    c.moveTo(a, b);
                    c.lineTo(x, y)
                }
                c.strokeStyle = "hsla(" + (U % 360) + ",100%,50%,0.75)"; // draw rainbow hypotrochoid
                c.stroke();
                a = x; // set previous coord.x
                b = y; // set previous coord.y
            }
            U -= 0.5; // increment hue
            A = X; // set original coord.x
            B = Y; // set original coord.y
        }, 16);
    }
    j.onkeydown = function(e) { a=b=0; R += 0.05 }
    d.onmousemove({pageX:300, pageY:290})
}
 
</script>
</head>
<body style="margin:0px;">
<canvas id="c"></canvas>
</body>
</html>


相关推荐
妖精的羽翼9 分钟前
前端(Vue)→ 全栈 + AI 应用开发
前端
码路飞28 分钟前
玩了一圈 AI 编程工具,Background Agent 才是让我真正震撼的东西
前端·javascript
UCloud_TShare31 分钟前
优刻得发布云搜索服务CSS:面向AI时代的企业级搜索基础设施
前端·css·人工智能
木斯佳1 小时前
前端八股文面经大全:字节暑期前端一面(2026-04-21)·面经深度解析
前端·面试·校招·面经·实习
Jolyne_1 小时前
前端从0开始的LangChain学习(一)
前端·langchain
掘金一周1 小时前
掘友们,一人说一个你买过夯到爆的东西 | 沸点周刊 4.23
前端·人工智能·后端
Developer_Niuge1 小时前
告别翻不动的 1000+ 书签:开源 Chrome / Edge 浏览器书签管理插件 Smart Bookmark 0.2 发布
前端·后端
WebInfra1 小时前
Rsbuild 2.0 发布:即将支持 TanStack Start
前端·javascript·程序员
用户52709648744902 小时前
前端性能指标速查手册
前端
淹死在鱼塘的程序猿2 小时前
🚀 告别"一次性聊天":揭秘让 AI 智能体越用越聪明的秘密武器 —— Skills
前端·人工智能·agent