Draw apple and cloth on canvas with mouse position
HTML
<h1>Emoji Rush</h1>
<canvas id="gameCanvas" width="400" height="400"></canvas>
CSS
#gameCanvas {
background-color: #f0f0f0;
border: 2px solid #333;
display: block;
margin: 0 auto;
}
JavaScript
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
let x = 200;
document.addEventListener("mousemove", (e) => {
const rect = canvas.getBoundingClientRect();
x = e.clientX - rect.left;
});
function draw() {
ctx.clearRect(0, 0, 400, 400);
ctx.font = "40px Arial";
ctx.fillText("🍎", 180, 50);
ctx.fillText("đź§ş", x - 25, 380);
requestAnimationFrame(draw);
}
draw();