I cannot clear the cursor image (canvas.getBoundingClientRect) after the cursor moves across the canvas element! I am left with a trail of appended images on the canvas.
- How can I clear the trail of appended images and still keep my customized cursor (canvas.getBoundingClientRect) visible each time the canvas is cleared?
See my code :
<script>
window.addEventListener("load", CanvasProperties, false);
//Global Variables
var canvas, context;
// Canvase Element - 2D Properties
function CanvasProperties(){
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
window.addEventListener("mousemove", CustomCursor, false);};
// Customized Cursor for Game's Canvas
function CustomCursor(e){
var canvasRect = canvas.getBoundingClientRect();
var xPosition = e.clientX - 5 - canvasRect.left;
var yPosition = e.clientY - 5 - canvasRect.top;
var img = new Image();
img.src = "hero.png";
context.drawImage(img, xPosition, yPosition, 80, 80);};
</script>