16

I am trying to get image from canvas. enter image description herePNG image is coming properly but the JPEG is producing a problem. I attached image here.First image is my canvas.Followed by PNG then JPEG.So i want my JPEG image with white background or i need a solution for PNG to JPEG conversion in JS

canvas =  $('.jSignature')[0];

            imgData = canvas.toDataURL();
            imgDatajpeg = canvas.toDataURL("image/jpeg");                   

            $(".sigCapHolder").append('<img src='+imgData+' /> ')
            $(".sigCapHolder").append('<img src='+imgDatajpeg+' /> ')
4
  • Showing us the code that generated those images sure would help Commented Dec 23, 2013 at 13:32
  • 2
    Cleary is a transparency problem. Put a white background rectangle to your canvas. Commented Dec 23, 2013 at 13:34
  • Data =$('.jSignature')[0].getContext("2d"); Data.fillRect(0,0,10,100); Data.fillStyle="white"; Data.fill(); I tried this but still am getting same issue only Commented Dec 23, 2013 at 14:05
  • It won't be displayed black if used in any GUI or web application Commented Jan 9, 2021 at 16:20

3 Answers 3

21

Cause

The reason for this to happen is due to canvas being transparent. However the transparancy color is black with a transparent alpha-channel so it won't show on screen.

JPEG on the other side doesn't support alpha-channel so that black color which is the default is stripped of its alpha channel leaving a black background.

You PNG supports alpha-channel so it is compatible with the way canvas work.

Solution

To get around this you will have to manually draw in a white background on the canvas before you draw in the image:

var canvas =  $('.jSignature')[0];
var ctx = canvas.getContext('2d');

ctx.fillStyle = '#fff';  /// set white fill style
ctx.fillRect(0, 0, canvas.width, canvas.height);

/// draw image and then use toDataURL() here
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much.I rectified my issue.I tried after drawn and changed dynamically.I hope this answer will help to many people in future.
This does not seem to work, even though I draw before the image I get just a white image (as if the white rect is above the image that I draw afterwards). Should I do something additional to work?
1

Just as an alternative way, using a package to convert black background of transparent image to white or any other other based on the provided HEX value, in our

const Jimp = require("jimp");

// Read the PNG file and convert it to editable format
Jimp.read("./images/your-image.png", function (err, image) {
    if (err) {
        // Return if any error
        console.log(err);
        return;
    }

    image.background(0xFFFFFFFF, (err, val) => {
        // Convert image to JPG and store it to 
        // './output/' folder with 'out.jpg' name
        image.write("./output/out.jpg");
    })

});

Comments

0

this works in firefox, oCanvas.toDataURL("image/jpeg")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.