Skip to content

Instantly share code, notes, and snippets.

@stekhn
Created August 3, 2022 18:49
Show Gist options
  • Save stekhn/b7d4628d32267396ff448fda420172cd to your computer and use it in GitHub Desktop.
Save stekhn/b7d4628d32267396ff448fda420172cd to your computer and use it in GitHub Desktop.
Save HTML5 canvas element as PNG file
const canvas = document.getElementsByTagName('canvas')[0];
const dataURL = canvas.toDataURL('image/png');
const timestamp = Math.floor(Date.now() / 1000).toString(36).slice(2);
const link = document.createElement('a');
link.download = `download-${timestamp}.png`;
link.href = dataURL;
link.click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment