I made a game where player press on rectangle which is moving at random location increases score. Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<canvas id = "c" width = "500" height = "500"></canvas>
<canvas id = "d" width = "60" height = "60" onclick = "inc()"></canvas>
<style>
body {
margin:0;
padding:0;
background-color:blue;
}
#d {
position:absolute;
}
</style>
<script>
var can = document.getElementById("c");
var can2 = document.getElementById("d");
can.height = window.innerHeight;
can.width = window.innerWidth;
can2.height = window.innerHeight;
can2.width = window.innerWidth;
var s = can.getContext("2d");
var s2 = can2.getContext("2d");
var points = 0;
function move() {
h = Math.ceil(Math.random()*(can2.height-60))+20;
w = Math.ceil(Math.random()*(can2.width-50))+20;
s2.clearRect(0, 0, window.innerHeight, window.innerWidth);
s2.fillRect(this.w, this.h, 60, 60);
}
function inc() {
s.clearRect(0, 0, window.innerHeight, window.innerWidth);
s.fillText(points++, 300, 40);
return points;
}
s.font = "bold 35px Arial";
s.textAlign = points;
s.fillText(points, 300, 40);
s2.fillStyle = "#DEF012";
s2.fillRect(60, 60, 58, 55);
setInterval(move, 200);
</script>
</body>
</html>
But when the rectangle is moving, the rectangle color at the side gets stuck. How do i can move the rectangle without that problem?