Bouncing Ball with Mouse CLick
by Ron Eaglin
HTML
<h1>Bouncing Ball with Mouse Click</h1>
<center><div id="container">
<canvas id="myCanvas" width="300" height="300"></canvas>
<button id="blue" onclick="changeColor();">Color</button>
</div></center>
CSS
body
{ background-color:#75797C; font:normal 12px/18px Arial, Helvetica, sans-serif; }
h1
{ display:block; width:600px; margin:20px auto; padding-bottom:20px; font:normal 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; text-align: center; }
#container
{ width:600px; margin:0 auto; }
#myCanvas
{ background:#C9E49C; border:1px solid #cbcbcb; }
JavaScript
var context;
var dx= 4;
var dy=4;
var y=150;
var x=10;
var color = "#800A39";
context= myCanvas.getContext('2d');
setInterval(draw,10);
function draw(){
context.clearRect(0,0,300,300);
context.beginPath();
context.fillStyle=color;
context.arc(x,y,20,0,Math.PI*2,true);
context.closePath();
context.fill();
if( x<0 || x>300)
dx=-dx;
if( y<0 || y>300)
dy=-dy;
x+=dx;
y+=dy;
}
function changeColor()
{
color = "#fff";
}