Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  193] [ 7]  / answers: 1 / hits: 32019  / 9 Years ago, sun, june 7, 2015, 12:00:00

I'm working on a canvas drawing project. I convert the canvas as an image, then I save that image as '.png'. I have to right click on the image and select the 'save image as' option. But I want to provide that option through a button. When I click the button it should be saved.



Any example or idea would be appreciated.



This is a js function that converts canvas to png.



 function save2()
{
window.open(canvas.toDataURL('image/png'));
var gh=(canvas.toDataURL('png'));
alert(converted);
}

More From » jquery

 Answers
297

In modern browser you can use the download attribute



function save2() {
window.open(canvas.toDataURL('image/png'));
var gh = canvas.toDataURL('png');

var a = document.createElement('a');
a.href = gh;
a.download = 'image.png';

a.click()
}


just trigger the function from a button, or insert the anchor in the page and style it as a button.



FIDDLE


[#66298] Thursday, June 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
heidys

Total Points: 665
Total Questions: 102
Total Answers: 97

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;