How to download a drawing from canvas keeping the background colour? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to download a drawing from canvas keeping the background colour?

I am working on an app in javascript and html that lets me draw on a canvas. When I download the image, there is no background. How can I keep the background when downloading? My javascript code for changing the background of the canvas: document.getElementById('canvas').style.backgroundColor=#FF0000 I did not want to set the background using fillRect because I have a clear button and that would clear the background too. How I download: var dataUrl = canvas.toDataURL('image.png') btnDownload.href = dataUrl

28th Dec 2020, 5:53 PM
Ana-Maria
Ana-Maria  - avatar
3 Answers
28th Dec 2020, 7:36 PM
JaScript
JaScript - avatar
+ 1
There is hardly any explanation. First complete tutorials. Otherwise, as with any language almost everything is learned by heart, which means look and apply. Important: Path to the image file must be correct. Image that you have saved locally will only be visible to you. In general, save the image on the server with the code. Exceptionally for SL Playground for example on Dropbox. Here a second example. Have fun with it. https://code.sololearn.com/WfD0hM4Zz82u/?ref=app
29th Dec 2020, 9:28 AM
JaScript
JaScript - avatar
0
JaScript can you please explain?
29th Dec 2020, 8:02 AM
Ana-Maria
Ana-Maria  - avatar