Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to save an image from html5 canvas

Posted on February 26, 2018August 4, 2020 by Habibie

With HTML5’s Canvas we can draw nice graphics. But how we can save it as an image? Of course we can right click on the canvas and download that image. But here I’m going to share a script to download it programmatically by a clicking a link.

<!DOCTYPE html>
<html>
	<head>
		<title>ZK Tutorials</title>
	</head>
	<body>
		<canvas id="myCanvas" width="300" height="300" style="border:1px solid #000000;"></canvas>
		<br>
		<a id="downloadbtn">Click to Download</a>
		<script>
			var c = document.getElementById("myCanvas");
			var ctx = c.getContext("2d");
			ctx.fillRect(10, 10, 100, 100);
			ctx.fillRect(120, 120, 50, 50);
			ctx.fillRect(200, 200, 70, 70);
			var imagelink = document.getElementById("downloadbtn");
			imagelink.href = c.toDataURL("image/png").replace("image/png", "image/octet-stream");
			imagelink.download = "ImageFromCanvas.png";
		</script>
	</body>
</html>

You can try it in this live preview:



Click to Download

Post Views: 384
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme