Preloader
Web development
  • Estimated reading time: 1 Minute

Image created from canvas have black background (HTML5)

Image created from canvas have black background (HTML5)

Does your exported image from a canvas element a black background with toDataURL() method ? Really annoying problem isn't?.

The HTMLCanvasElement.toDataURL() method returns a data URIs containing a representation of the image in the format specified by the type parameter (defaults to PNG).

The black background of an image is caused by the incorrect exportation format, if you have this problem, you're probably exporting the image in this way :

var Image = canvas.toDataURL("image/jpeg");

Normally, you ommit the first parameter (The format) as it will be always PNG, but if you specify explicitly the format and your image has transparency, you'll face this problem.

To solve this error, change (or ommit) the first parameter from image/jpeg to image/png.

var Image = canvas.toDataURL("image/png");
// Or without parameter because the default exporting format is PNG
var Image = canvas.toDataURL();

Why the image needs to be exported in PNG format?

Simply because if your canvas have transparent background, and your exported format is JPG , will be impossible to generate the transparency to the JPG format because only the PNG format supports transparency.

Share:
Carlos Delgado

Carlos Delgado

Senior Software Engineer at Corvix. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Related articles
The Convergence of AI, Cybersecurity & Modern Web Development | OST
16 Jun, 2026
  • Estimated reading time: 4 Minutes
WordPress vs Custom Website: Which is Better for Your Business?
23 Apr, 2026
  • Estimated reading time: 5 Minutes
Guidelines To Follow In Test Case Writing
4 Mar, 2026
  • Estimated reading time: 4 Minutes
How to Build a Secure Authentication System in a Web Application
27 Feb, 2026
  • Estimated reading time: 5 Minutes
Weekly trending
How to Implement AI Voice Agents for Real-Time Query Handling
4 Jul, 2026
  • Estimated reading time: 7 Minutes
How Legal Counsel Supports Software Firms Through Contracts and M&A
4 Jul, 2026
  • Estimated reading time: 5 Minutes
How Technology Can Help Pet Owners Build Better Indoor Routines
4 Jul, 2026
  • Estimated reading time: 6 Minutes
Best Mabl Alternatives for Modern AI-Powered Test Automation
4 Jul, 2026
  • Estimated reading time: 3 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.