SOURCE

console 命令行工具 X clear

                    
>
console
var H = window.innerHeight,W = window.innerWidth,sin = Math.sin,cos =Math.cos,PI = Math.PI;
    var _canvas = new Canvas("canvas",W,H);
    var canvas = _canvas.canvas,ctx = _canvas.ctx;
    var obj = {balls:[]},num = 10;
    //  小球
    function Ball(x0,y0,r,vx){
        this.x0 = x0;
        this.y0 = y0;
        this.x = x0;
        this.y = y0;
        this.r = r;
        this.vx = vx; // x初速度
        this.vy = 0; // y速度
        this.g = 1000; // 重力加速度
        this.f = 50;  // 摩擦系数
        this.st = +new Date - 20;
        this.lt = +new Date - 20;
        this.runX = 1;
        this.runY = 1;
        this.vxarr = [];
        this.rgb = [random(100,255,1),random(100,255,1),random(100,255,1)] 
    }
    // 小球,地面初始化
    function init(){
        while(num--){
            obj.balls.push(new Ball(random(100,W-100),random(100,H-100),random(10,30,1),random(400,800,1,1)));
        }
        run();
    }
    
    
    function run(){
        for(var  x in obj.balls){
            var v = obj.balls[x];
            // 由初速度,加速度,时间算出单位时间的距离
            var now = +new Date;
            if(v.runX){
                if(v.vx<0) v.f = Math.abs(v.f)*(-1);
                else v.f = Math.abs(v.f);
                var sx = v.vx*(now-v.lt)/1000-v.f*(now-v.lt)*(now-v.st)/1000000-v.f/2*(now-v.lt)*(now-v.lt)/1000000;
                
                v.x+=sx;
                v.sx = sx;
                //v.vxarr.push([now-v.st,v.vx-v.f*(now-v.st)/1000])
                if(Math.abs(sx)<0.01 || Math.abs(v.vx)<Math.abs(v.f)*(now-v.st)/1000){
                    v.runX = 0;
                }
            }
            if(v.runY){
                // 在y轴方向上,因为在上下方向上加速度不一样,可用瞬时速度来计算单位距离
                if(v.vy>=0) v.f = -Math.abs(v.f);
                else v.f = Math.abs(v.f);
                v.vy += (v.f*5+v.g)*(now-v.lt)/1000;
                var ds = v.vy*(now-v.lt)/1000;
                v.y+=ds;
                
                if(v.y > H-v.r ){
                    v.y = H-v.r;
                    if(Math.abs(v.vy)<10) v.runY = 0;
                    
                }
            }
            if(v.runX || v.runY) v.lt = now;
        }
        colliding();
        render();
        requestAnimationFrame(run)
    }
    //  碰撞
    function colliding(){
        var l = obj.balls.length
        if(l){
            for(var i=0;i<l;i++){
                var b0 = obj.balls[i];
                if(b0.x<b0.r || b0.x>W-b0.r) b0.vx = -b0.vx;
                if(b0.y<b0.r || b0.y>=H-b0.r) b0.vy = -b0.vy;
                for(var j = i+1;j<l;j++){
                    
                }
            }
        }
    }
    // 绘制
    function render(){
        ctx.clearRect(0,0,W,H);
        for(var x in obj.balls){
            var v = obj.balls[x];
            ctx.beginPath();
            ctx.fillStyle="rgb("+v.rgb[0]+","+v.rgb[1]+","+v.rgb[2]+")";
            ctx.arc(v.x,v.y,v.r,0,2*Math.PI);
            ctx.fill();
            ctx.closePath();
        }
    }
    function distans(x1,y1,x2,y2){
        return Math.sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)));
    }

    function random(min,max,intval,minus){
        var res = Math.random()*(max-min)+min;
        res =  intval ?  parseInt(res) : res;
        return minus ? (Math.random()>0.5 ?  res : -res) : res;
    }

    function constantAcceleration(v0,a,st,dt){
        // -c *(t/=d)*(t-2) + b;
        var s = v0*t
    }


    init();

<script src="http://raw.githack.com/BiyugWv/codess/master/codes.js"></script>