感谢v2ex用户zerision提供的鼠标点击特效源码 原帖:https://www.v2ex.com/t/656062

function clickEffect() {
    let balls = [];
    let longPressed = false;
    let longPress;
    let multiplier = 0;
    let width, height;
    let origin;
    let normal;
    let ctx;
    const colours = ["#F73859", "#14FFEC", "#00E0FF", "#FF99FE", "#FAF15D"];
    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("mouseup", function(e) {
            clearInterval(longPress);
            if (longPressed == true) {
                document.body.classList.remove("is-longpress");
                pushBalls(randBetween(50 + Math.ceil(multiplier), 100 + 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.angle = Math.PI * 2 * Math.random();
            if (longPressed == true) {
                this.multiplier = randBetween(14 + multiplier, 15 + multiplier);
            } else {
                this.multiplier = randBetween(6, 12);
            }
            this.vx = (this.multiplier + Math.random() * 0.5) * Math.cos(this.angle);
            this.vy = (this.multiplier + Math.random() * 0.5) * Math.sin(this.angle);
            this.r = randBetween(8, 12) + 3 * Math.random();
            this.color = colours[Math.floor(Math.random() * colours.length)];
        }
        update() {
            this.x += this.vx - normal.x;
            this.y += this.vy - normal.y;
            normal.x = -2 / window.innerWidth * Math.sin(this.angle);
            normal.y = -2 / window.innerHeight * Math.cos(this.angle);
            this.r -= 0.3;
            this.vx *= 0.9;
            this.vy *= 0.9;
        }
    }

    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.2;
        } else if (!longPressed &amp;&amp; multiplier >= 0) {
            multiplier -= 0.4;
        }
        removeBall();
        requestAnimationFrame(loop);
    }

    function removeBall() {
        for (let i = 0; i < balls.length; i++) {
            let b = balls[i];
            if (b.x + b.r < 0 || b.x - b.r > width || b.y + b.r < 0 || b.y - b.r > height || b.r < 0) {
                balls.splice(i, 1);
            }
        }
    }
}

2 thoughts on “新增了鼠标点击特效

  1. 人类历史上有过各种各样的预言和传说,认为到了一定时期会有劫难出现,甚至走向毁灭,为什么?因为人失去了对神的信仰,道德失控了,没了底线,变得不是人了,瘟疫等危险自然就来了。
    善良的朋友:
    法 轮 功是佛 家上乘修 炼大 法,原则是真 善 忍。大法可归正人类道德,救度众生于水火。但中共江 氏集团出于嫉恨,火烧活人,自导自演了“天安 门 自 焚”,从而迫害法 轮 功,妄图阻碍众生得救。

    Reply
  2. 珍惜这时间吧!

    中华民族在历史上,走过了秦、汉、三国、晋 、南北朝、隋、唐、宋、元、明、清、民国等朝代,历朝历代都是这样更迭表演着;每个朝代都想永远统治江山,但每个朝代最后都逃脱不了被历史淘汰的命运;一个朝代结束,另一个朝代又来;朝朝如此,代代相续;中华民族就这样沧桑而又茁壮的走过了五千年 。

    今天的中国红朝政权也到了大清朝末期的末期的最后一刻。国库已空,无官不贪,无民不刁,无处不拆,民怨极大,军民思变,动乱频传,贫富巨差,官民对立,种种丑态,种种乱象。红朝崩溃、政权灭亡的时间到来了。

    然上天有好生之德,不忍心生灵涂炭,想尽量避免历朝历代所出现的血腥场面,意让这个崩溃的政权从内部清洗、自我过渡到新朝、新代、新政权。

    民间舆论,海内外精英包括体制内有识之士,已经发出惊呼:改革(改良的希望)已死,革命必来。

    清醒吧,不要对邪恶的流氓集团再抱有任何幻想。

    中华民族的淘汰旧王朝,进入新朝代的历史轮回时刻又一次来到了。

    顺天意者得天下,方有未来。如此,众生喜之,天地欣之。

    自救方式:从退出共党及其附属组织开始。珍惜这时间吧!

    Reply

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据