canvas.toDataURL() returns a transparent image

See original GitHub issue

Hello, I made a simple canvas which superpose two images and I would like to get the Data64 (or even a jpg file) of it but when I do canvas.toDataURL() the output is:

Promise { "_40": 0, "_55": null, "_65": 0, "_72": null, }

Is it normal ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
trickeydcommented, Sep 5, 2017

Hi, I have had a question about this too.

These methods return a promise, as you can see, so you would need to wait for it to resolve before the variable can be populated.

However, in my case the promise never actually got resolved. Here is a link to my issue: https://github.com/iddan/react-native-canvas/issues/25

If you haven’t already try rewriting with async as suggested by @iddan.

1reaction
iddancommented, Sep 30, 2017

You read the canvas before it was drawn. Your code should be:

	handleCanvas = async (canvas) => {
		canvas.width = 600;
		canvas.height = 800;
		const ctx = canvas.getContext('2d');

		var photo = new CanvasImage(canvas);
		photo.src = 'data:image/jpg;base64,' + this.state.image;
		photo.addEventListener('load', () => {
			ctx.drawImage(photo, 0, 0, 600, 800);
		});

		var watermark = new CanvasImage(canvas);
		watermark.src = '/img/watermark.png';
		watermark.addEventListener('load', async () => {
			ctx.drawImage(watermark, 85, 220, 107, 46);
                        const image_new_uri = await canvas.toDataURL()
		        this.setState({ image_new_uri });
                        console.log(image_new_uri)
		});
	}
Read more comments on GitHub >

github_iconTop Results From Across the Web

canvas.toDataURL results in solid black image?
toDataURL ("image/jpeg"); var img = new Image(); img.src = data; ... use the type image/png , the background will be transparent by default....
Read more >
How to create transparency in images with canvas - Patrick Wied
This article is about how to create transparency in all types of images ( .JPEG, .GIF, .PNG ) with only the html5 canvas...
Read more >
HTML : toDataURL() of webgl canvas returning transparent image ...
HTML : toDataURL() of webgl canvas returning transparent image [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] HTML : toDataURL() of w ...
Read more >
canvas.toDataURL() resulting in an empty transparent image
I am using blend4web to create a 3D product configurator with a varied set of options that would be live rendered. Blend4web renders...
Read more >
HTMLCanvasElement.toDataURL() - Web APIs | MDN
toDataURL() method returns a data URL containing a representation of the image ... toDataURL('image/jpeg', 0.5); const lowQuality = canvas.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found