Skip to content

Instantly share code, notes, and snippets.

@kfitfk
Last active July 1, 2024 09:23
Show Gist options
  • Save kfitfk/a58b7ca2d8e2a669d6c7cc0fb5108ba6 to your computer and use it in GitHub Desktop.
Save kfitfk/a58b7ca2d8e2a669d6c7cc0fb5108ba6 to your computer and use it in GitHub Desktop.
Save canvas drawing to local image
function saveCanvas(canvas) {
var image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream'); // here is the most important part because if you dont replace you will get a DOM 18 exception.
window.location.href = image; // it will save locally
}
function openInNewWindow(canvas) {
// works in Firefox
window.open(canvas.toDataURL("image/png"), '_blank');
}
function openOffscreenCanvasInNewWindow(canvas) {
canvas.convertToBlob().then(blob => {
const url = URL.createObjectURL(blob);
window.open(url, '_blank');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment