HTML Canvas Animation Bouncing off boundary
<!DOCTYPE html> <html> <head> <title>Making things move</title> <meta charset="utf-8"> //from w ww.ja v a 2s . com <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var canvas = $("#myCanvas"); var context = canvas.get(0).getContext("2d"); var canvasWidth = canvas.width(); var canvasHeight = canvas.height(); var playAnimation = true; var startButton = $("#startAnimation"); var stopButton = $("#stopAnimation"); startButton.hide(); startButton.click(function() { $(this).hide(); stopButton.show(); playAnimation = true; animate(); }); stopButton.click(function() { $(this).hide(); startButton.show(); playAnimation = false; }); // Class that defines new shapes to draw var Shape = function(x, y, width, height) { this.x = x; this.y = y; this.width = width; this.height = height; this.reverseX = true; this.reverseY = false; }; // Array that holds all the shapes to draw var shapes = new Array(); // Setting up some shapes for (var i = 0; i < 10; i++) { var x = Math.random()*250; var y = Math.random()*250; var width = height = Math.random()*30; shapes.push(new Shape(x, y, width, height)); }; // Animation loop that does all the fun stuff function animate() { // Clear context.clearRect(0, 0, canvasWidth, canvasHeight); // Loop through every object var shapesLength = shapes.length; for (var i = 0; i < shapesLength; i++) { var tmpShape = shapes[i]; // Update // Check direction to move shape if (!tmpShape.reverseX) { tmpShape.x += 2; } else { tmpShape.x -= 2; }; if (!tmpShape.reverseY) { tmpShape.y += 2; } else { tmpShape.y -= 2; }; // Draw shape context.fillRect(tmpShape.x, tmpShape.y, tmpShape.width, tmpShape.height); // Check the boundaries if (tmpShape.x < 0) { tmpShape.reverseX = false; } else if (tmpShape.x + tmpShape.width > canvasWidth) { tmpShape.reverseX = true; }; if (tmpShape.y < 0) { tmpShape.reverseY = false; } else if (tmpShape.y + tmpShape.height > canvasHeight) { tmpShape.reverseY = true; }; }; if (playAnimation) { // Run the animation loop again in 33 milliseconds setTimeout(animate, 33); }; }; // Start the animation loop animate(); }); </script> </head> <body> <canvas id="myCanvas" width="500" height="500"> <!-- Insert fallback content here --> </canvas> <div> <button id="startAnimation">Start</button> <button id="stopAnimation">Stop</button> </div> </body> </html>