TypechoJoeTheme

Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)

统计

为你的网站添加点击散开特效

子沫博主
2021-04-11
/
0 评论
/
803 阅读
/
0 个字
/
百度已收录
04/11
本文最后更新于2021年04月11日,已超过1335天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

<canvas id="fireworks" style="position: fixed; left: 0px; top: 0px; pointer-events: none; z-index: 2147483647; width: 1920px; height: 151px;" width="3840" height="302"></canvas>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/animejs@3.0.1/lib/anime.min.js"></script>
<script type="text/javascript">
    function updateCoords(e) {
        pointerX = (e.clientX || e.touches[0].clientX) - canvasEl.getBoundingClientRect().left,
            pointerY = e.clientY || e.touches[0].clientY - canvasEl.getBoundingClientRect().top
    }

    function setParticuleDirection(e) {
        var t = anime.random(0, 360) * Math.PI / 180,
            a = anime.random(50, 180),
            n = [-1, 1][anime.random(0, 1)] * a;
        return {
            x: e.x + n * Math.cos(t),
            y: e.y + n * Math.sin(t)
        }
    }

    function createParticule(e, t) {
        var a = {};
        return a.x = e,
            a.y = t,
            a.color = colors[anime.random(0, colors.length - 1)],
            a.radius = anime.random(16, 32),
            a.endPos = setParticuleDirection(a),
            a.draw = function() {
                ctx.beginPath(),
                    ctx.arc(a.x, a.y, a.radius, 0, 2 * Math.PI, !0),
                    ctx.fillStyle = a.color,
                    ctx.fill()
            },
            a
    }

    function createCircle(e, t) {
        var a = {};
        return a.x = e,
            a.y = t,
            a.color = "#F00",
            a.radius = .1,
            a.alpha = .5,
            a.lineWidth = 6,
            a.draw = function() {
                ctx.globalAlpha = a.alpha,
                    ctx.beginPath(),
                    ctx.arc(a.x, a.y, a.radius, 0, 2 * Math.PI, !0),
                    ctx.lineWidth = a.lineWidth,
                    ctx.strokeStyle = a.color,
                    ctx.stroke(),
                    ctx.globalAlpha = 1
            },
            a
    }

    function renderParticule(e) {
        for (var t = 0; t < e.animatables.length; t++)
            e.animatables[t].target.draw()
    }

    function animateParticules(e, t) {
        for (var a = createCircle(e, t), n = [], i = 0; i < numberOfParticules; i++)
            n.push(createParticule(e, t));
        anime.timeline().add({
            targets: n,
            x: function(e) {
                return e.endPos.x
            },
            y: function(e) {
                return e.endPos.y
            },
            radius: .1,
            duration: anime.random(1200, 1800),
            easing: "easeOutExpo",
            update: renderParticule
        }).add({
            targets: a,
            radius: anime.random(80, 160),
            lineWidth: 0,
            alpha: {
                value: 0,
                easing: "linear",
                duration: anime.random(600, 800)
            },
            duration: anime.random(1200, 1800),
            easing: "easeOutExpo",
            update: renderParticule,
            offset: 0
        })
    }

    function debounce(fn, delay) {
        var timer
        return function() {
            var context = this
            var args = arguments
            clearTimeout(timer)
            timer = setTimeout(function() {
                fn.apply(context, args)
            }, delay)
        }
    }

    var canvasEl = document.querySelector("#fireworks");
    if (canvasEl) {
        var ctx = canvasEl.getContext("2d"),
            numberOfParticules = 30,
            pointerX = 0,
            pointerY = 0,
            tap = "mousedown",
            colors = ["#FF1461", "#18FF92", "#5A87FF", "#FBF38C"],
            setCanvasSize = debounce(function() {
                canvasEl.width = 2 * window.innerWidth,
                    canvasEl.height = 2 * window.innerHeight,
                    canvasEl.style.width = window.innerWidth + "px",
                    canvasEl.style.height = window.innerHeight + "px",
                    canvasEl.getContext("2d").scale(2, 2)
            }, 500),
            render = anime({
                duration: 1 / 0,
                update: function() {
                    ctx.clearRect(0, 0, canvasEl.width, canvasEl.height)
                }
            });
        document.addEventListener(tap, function(e) {
                "sidebar" !== e.target.id && "toggle-sidebar" !== e.target.id && "A" !== e.target.nodeName && "IMG" !== e.target.nodeName && (render.play(),
                    updateCoords(e),
                    animateParticules(pointerX, pointerY))
            }, !1),
            setCanvasSize(),
            window.addEventListener("resize", setCanvasSize, !1)
    }
</script>
朗读
赞(1)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)

本文链接:

https://jpgke.com/lg/310.html(转载时请注明本文出处及文章链接)

评论 (0)

互动读者

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

360实时热点

  • 获取失败!

标签云

最新回复

  1. 试试看闲逛
    2021-12-14
  2. 555闲逛
    2021-12-07
  3. def闲逛
    2021-11-28
  4. 紫夜闲逛
    2021-11-28
  5. 666闲逛
    2021-10-05