I want to save canvas as PNG, without opening it in a new window as base64-encoded image.
I used this code:
jQuery("#btnPreview").click(function(){
if (!fabric.Canvas.supports('toDataURL')) {
alert('Sorry, your browser is not supported.');
}
else {
canvas.deactivateAll();
canvas.forEachObject(function(o){
if(o.get("title") == "||Watermark||"){
canvas.bringToFront(o);
}
});
window.open(canvas.toDataURL('png'), "");
canvas.forEachObject(function(o){
if(o.get("title") == "||Watermark||"){
canvas.sendToBack(o);
}
});
canvas.renderAll();
}
});
I want to make the button save the image as a PNG or JPG.
Canvas is a powerful drawing technology for the web. One of its biggest caveats is that it's not easy to save the canvas image data to your computer. In this post, I'll show two ways that will allow your users to save what they see in their browser to their desktop.
If you don’t have an image editor installed on your Linux, you can use the xclip command to save a clipboard image file as PNG or JPG. Find the image you want to save as JPG or PNG and right-click on it.
You can just put this code in console in firefox or chrom and after changed your canvas tag ID in this above script and run this script in console. After the execute this code you will see the link as text "click here" at bottom of the html page. click on this link and open the canvas drawing as a PNG image in new window save the image.
Among the major operating systems, Linux users are probably, the most comfortable with using Terminal to get things done. If you don’t have an image editor installed on your Linux, you can use the xclip command to save a clipboard image file as PNG or JPG. Find the image you want to save as JPG or PNG and right-click on it.
I use this on my jquery:
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
$('.save').attr({
'download': 'YourProduct.png', /// set filename
'href' : image /// set data-uri
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With