<!DOCTYPE html> <html> <head> <style> .canvas__container { height: 100%; position: relative; width: 100%; } .canvas__canvas { height: 100%; position: relative; width: 100%; z-index: 1; } .canvas__mirror { height: 100%; left: 0; position: absolute; top: 0; width: 100%; } </style> </head> <body> <div class="canvas__container"> <canvas id="cnvs" class="canvas__canvas"></canvas> <img src="" id="mirror" class="canvas__mirror" /> </div> <a href="#" class="button" id="btn-download" download="my-file-name.png">Download</a>
<script> var cnvs = document.getElementById('cnvs'), ctx = cnvs.getContext('2d'), mirror = document.getElementById('mirror');
cnvs.width = mirror.width = window.innerWidth; cnvs.height = mirror.height = window.innerHeight;
mirror.addEventListener('contextmenu', function (e) { var dataURL = canvas.toDataURL('image/png'); mirror.src = dataURL; });
var button = document.getElementById('btn-download'); button.addEventListener('click', function (e) { var canvas = document.getElementById('cnvs'); var dataURL = canvas.toDataURL('image/png'); button.href = dataURL; });
document.getElementById('btn-download').click();
</script> </body> </html> |