To clear canvas in JavaScript, do this:
context.clearRect(0, 0, canvas.width, canvas.height)
.For example, if this is your canvas:
<canvas id="canvas" width="400" height="400">
Then you can clear it with this JavaScript:
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);