<canvas style="border:1px solid black"></canvas>
<a href="" download="my-file-name.png" href="#"><button id="download">Download</button></a>
<script>
const canvas = document.querySelector('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillRect(25,25,100,100);
ctx.clearRect(45,45,60,60);
ctx.strokeRect(50,50,50,50);
}
// Скачать Canvas
document.querySelector('#download').addEventListener('click', function(){
var dataURL = canvas.toDataURL('image/png');
document.querySelector('a').href = dataURL;
});
</script>