js实现canvas模拟点击生成多个黑洞重力影响光子粒子运行轨迹动画效果代码

代码语言:html

所属分类:粒子

代码描述:js实现canvas模拟点击生成多个黑洞重力影响光子粒子运行轨迹动画效果代码

代码标签: canvas 物理 重力 黑洞 光子 粒子

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">





    <style>
        body {
            font-family: Helvetica sans-serif;
            padding: 0;
            margin: 0;
            background-color: #222;
            overflow: hidden;
            -webkit-user-select: none;
               -moz-user-select: none;
                 -o-user-select: none;
                -ms-user-select: none;
                    user-select: none;
        }
        
        canvas {
            position: absolute;
            top: 0;
            left: 0;
        }
        
        .info {
            position: absolute;
            top: 0;
            left: 0;
            padding: 5px 15px;
            color: #eee;
            font-size: 13px;
            background-color: rgba(0, 0, 0, .5);
        }
    </style>



</head>

<body>
    <canvas id="c"></canvas>
    <div class="info">Click to add gravity point.</div>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script>
    <script>
        /**
     * requestAnimationFrame
     */
    window.requestAnimationFrame = (function(){
        return  window.requestAnimationFrame       ||
                window.webkitRequestAnimationFrame ||
                window.mozRequestAnimationFrame    ||
                window.oRequestAnimationFrame      ||
                window.msRequestAnimationFrame     ||
                function (callback) {
                    window.setTimeout(callback, 1000 / 60);
                };
    })();
    
    
    /**
     * Vector
     */
    function Vector(x, y) {
        this.x = x || 0;
        this.y = y || 0;
    }
    
    Vector.add = function(a, b) {
        return new Vector(a.x + b.x, a.y + b.y);
    };
    
    Vector.sub = function(a, b) {
        return new Vector(a.x - b.x, a.y - b.y);
    };
    
    Vector.scale = function(v, s) {
        return v.clone().scale(s);
    };
    
    Vector.random = function() {
        return new Vector(
            Math.random() * 2 - 1,
            Math.random() * 2 - 1
        );
    };
    
    Vector.prototype = {
        set: function(x, y) {
            if (typeof x === 'object') {
                y = x.y;
                x = x.x;
            }
            this.x = x || 0;
            this.y = y || 0;
            return this;
        },
    
        add: function(v) {
            this.x += v.x;
            this.y += v.y;
            return this;
        },
    
        sub: function(v) {
            this.x -= v.x;
            this.y -= v.y;
            return this;
        },
    
        scale: function(s) {
            this.x *= s;
            this.y *= s;
            return this;
        },
    
        length: function() {
    .........完整代码请登录后点击上方下载按钮下载查看

网友评论0