How do i make my red bouncy ball change color on collision?
As of right now my ball just stays red and wont change to another color when it hits walls. How do I go from the color red to green when I hit the top or bottom of canvas and yellow when I hit the sides of the canvas? var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var x = canvas.width/2; var y = canvas.height-30; var dx = 2; var dy = -2; var ballRadius = 10; var R = "rgba(255,0,0,0.9)"; var G = "rdgba(0,255,0,0.9)"; var Y = "rgba(0,255,255,0.9)"; function drawBall(){ ctx.beginPath(); ctx.arc(x,y,ballRadius,0,Math.PI*2); ctx.fillStyle ="red"; ctx.stroke(); ctx.closePath(); } function draw(){ ctx.clearRect(0,0,canvas.width,canvas.height); drawBall(); x += dx; y += dy; ctx.fill(); if(x + dx > canvas.width || x + dx < ballRadius){ dx=-dx; ctx.fillStyle = 'yellow'; } if(y + dy > canvas.height || y + dy < ballRadius){ dy=-dy; ctx.fillStyle = 'green'; } } setInterval(draw, 10);