Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving canvas as a PNG or JPG

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.

like image 903
UXX1 Avatar asked Jun 26 '12 12:06

UXX1


People also ask

Is it easy to save canvas image data to your computer?

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.

How do I save an image as a PNG in Linux?

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.

How to open canvas drawing as PNG image?

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.

How to save a clipboard image file as PNG or jpg?

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.


1 Answers

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
});
like image 105
Dakshika Avatar answered Nov 09 '22 21:11

Dakshika