JS 实现万花筒特效 JS 原生代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            background: #190117;
            overflow: hidden;
            margin: 0;
            width: 100%;
        }
    </style>
</head>
<body>
<script>
    window.requestAnimFrame = (function() {
        return window.requestAnimationFrame ||
                window.webkitRequestAnimationFrame ||
                window.mozRequestAnimationFrame ||
                window.oRequestAnimationFrame ||
                window.msRequestAnimationFrame ||
                function(callback) {
                    window.setTimeout(callback, 1000 / 60);
                };
    })();
    var obj = {
        rad: {
            base: 200,
            vary: 25,
            step: Math.PI / 360
        },
        c: {
            w: window.innerWidth,
            h: window.innerHeight
        },
        lay: {
            num: 28,
            dist: 80,
            diff: Math.PI / 66
        },
        run: {
            int: 20
        }
    };
    function Circle(x, y, step) {
        this.x = x;
        this.y = y;
        this.step = step
    }
    Circle.prototype.draw = function($) {
        this.step += obj.rad.step;
        $.beginPath();
        var r = obj.rad.base + Math.sin(this.step) * obj.rad.vary;
        $.arc(this.x, this.y, r, 0, 2 * Math.PI, true);
        $.fill();
    }
    function init() {
        var d = obj.lay.dist,
                t = Math.PI / 3,
                x = obj.c.w / 2,
                y = obj.c.h / 2,
                clay = [
                    [new Circle(x, y, 0)]
                ],
                circ, lay, s, pt, ptx, pty, dx, dy;
        for (lay = 1; lay < obj.lay.num; lay++) {
            circ = [];
            for (s = 0; s < 6; s++) {
                ptx = x + d * lay * Math.cos(t * s);
                pty = y + d * lay * Math.sin(t * s);
                dx = d * Math.cos(t * s + t * 2);
                dy = d * Math.sin(t * s + t * 2);
                for (pt = 0; pt < lay; pt++) {
                    circ.push(new Circle(ptx + dx * pt, pty + dy * pt, -1 * lay * obj.lay.diff));
                }
            }
            clay.push(circ);
        }
        return clay;
    }
    var c = document.createElement('canvas');
    c.width = obj.c.w;
    c.height = obj.c.h;
    document.body.appendChild(c);
    var $ = c.getContext('2d');
    $.fillRect(0, 0, c.width, c.height);
    $.globalCompositeOperation = 'xor';
    $.fillStyle = 'hsla(304, 95%, 75%, 1)';
    c.addEventListener('load', resize);
    c.addEventListener('resize', resize, false);
    function resize() {
        c.width = w = window.innerWidth;
        c.height = h = window.innerHeight;
        c.style.position = 'absolute';
        c.style.left = (window.innerWidth - w) *
                .01 + 'px';
        c.style.top = (window.innerHeight - h) *
                .01 + 'px';
    }
    var clay = init();
    function run() {
        var i, j, ilen, jlen;
        $.clearRect(0, 0, obj.c.w, obj.c.h);
        for (i = 0, ilen = clay.length; i < ilen; i++) {
            for (j = 0, jlen = clay[i].length; j < jlen; j++) {
                clay[i][j].draw($);
            }
        }
    }
    function go() {
        run();
        window.requestAnimFrame(go);
    }
    go();
</script>
</body>
</html>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值