Cleaning up
This commit is contained in:
		@ -3,6 +3,5 @@
 | 
				
			|||||||
<script src="index.js"></script>
 | 
					<script src="index.js"></script>
 | 
				
			||||||
</head>
 | 
					</head>
 | 
				
			||||||
<body>
 | 
					<body>
 | 
				
			||||||
<canvas id="canvas"></canvas>
 | 
					 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
							
								
								
									
										81
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										81
									
								
								index.js
									
									
									
									
									
								
							@ -1,26 +1,37 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
document.addEventListener('DOMContentLoaded', init);
 | 
					document.addEventListener('DOMContentLoaded', () => init());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let ctx, canvas = document.createElement("canvas");
 | 
				
			||||||
function init() {
 | 
					function init() {
 | 
				
			||||||
    var canvas = document.getElementById("canvas");
 | 
					    function start() {
 | 
				
			||||||
        canvas.width = window.innerWidth
 | 
					        canvas.width = window.innerWidth
 | 
				
			||||||
        canvas.height = window.innerHeight
 | 
					        canvas.height = window.innerHeight
 | 
				
			||||||
 | 
					        ctx = canvas.getContext('2d');
 | 
				
			||||||
 | 
					        document.body.insertBefore(canvas, document.body.childNodes[0]);
 | 
				
			||||||
 | 
					        document.addEventListener('keydown', controls);
 | 
				
			||||||
 | 
					        run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const ctx = canvas.getContext('2d');
 | 
					        for (let i = 0; i < 15; i++) add();
 | 
				
			||||||
    var balls = [];
 | 
					    }
 | 
				
			||||||
    add();
 | 
					 | 
				
			||||||
    document.addEventListener('keydown', control);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    anim();
 | 
					    function run() {
 | 
				
			||||||
 | 
					        ctx.clearRect(0, 0, canvas.width, canvas.height);
 | 
				
			||||||
 | 
					        update();
 | 
				
			||||||
 | 
					        requestAnimationFrame(run);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function control(e) {
 | 
					    function controls(e) {
 | 
				
			||||||
        switch(e.key) {
 | 
					        switch (e.key) {
 | 
				
			||||||
            case 'ArrowUp': add(); break;
 | 
					            case 'ArrowUp': add(); break;
 | 
				
			||||||
            case 'ArrowDown': remove(); break;
 | 
					            case 'ArrowDown': remove(); break;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // -------------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let balls = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function remove() {
 | 
					    function remove() {
 | 
				
			||||||
        balls.pop();
 | 
					        balls.pop();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -31,39 +42,47 @@ function init() {
 | 
				
			|||||||
        let speed = 1 + Math.floor(Math.random() * 10);
 | 
					        let speed = 1 + Math.floor(Math.random() * 10);
 | 
				
			||||||
        let angle = Math.floor(Math.random() * 360);
 | 
					        let angle = Math.floor(Math.random() * 360);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // angle must be in radians (now it's wrong)
 | 
					        balls.push(new ball(x, y, speed, angle));
 | 
				
			||||||
        balls.push({ x: x, y: y, size: 20, speed: speed, angle: angle, color:'black' });
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    function anim() {
 | 
					    function update() {
 | 
				
			||||||
        ctx.fillStyle = 'white';
 | 
					        balls.forEach(b => b.update());
 | 
				
			||||||
        ctx.fillRect(0, 0, canvas.width, canvas.height);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        balls.forEach(b => {
 | 
					 | 
				
			||||||
            update(b);
 | 
					 | 
				
			||||||
            draw(b);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
        requestAnimationFrame(anim);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    start();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function update(b) {
 | 
					class ball {
 | 
				
			||||||
        b.x += b.speed * Math.cos(b.angle);
 | 
					    constructor(x, y, speed, angle) {
 | 
				
			||||||
        b.y += b.speed * Math.sin(b.angle);
 | 
					        this.x = x;
 | 
				
			||||||
 | 
					        this.y = y;
 | 
				
			||||||
        if ( b.x<0 || b.x>canvas.width || b.y<0 || b.y>canvas.height ) bounce(b);
 | 
					        this.speed = speed;
 | 
				
			||||||
 | 
					        this.angle = angle;
 | 
				
			||||||
 | 
					        this.color = 'black';
 | 
				
			||||||
 | 
					        this.size = 20;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function bounce(b) {
 | 
					    update() {
 | 
				
			||||||
        b.angle += 180; 
 | 
					        this.move();
 | 
				
			||||||
 | 
					        this.draw();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function draw(b) {
 | 
					    draw() {
 | 
				
			||||||
        ctx.fillStyle = b.color;
 | 
					        ctx.fillStyle = this.color;
 | 
				
			||||||
        ctx.fillRect(b.x, b.y, b.size, b.size);
 | 
					        ctx.fillRect(this.x, this.y, this.size, this.size);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    move() {
 | 
				
			||||||
 | 
					        this.x += this.speed * Math.cos(this.angle);
 | 
				
			||||||
 | 
					        this.y += this.speed * Math.sin(this.angle);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height)
 | 
				
			||||||
 | 
					            this.bounce();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    bounce() {
 | 
				
			||||||
 | 
					        this.angle += 180;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user