Windows 10 reached EOS (end of support) on October 14, 2025. For more information, see this article.

Pretraži podršku

Izbjegni prevare podrške. Nikad te nećemo tražiti da nas nazoveš, da nam pošalješ telefonski broj ili da podijeliš osobne podatke. Prijavi sumnjive radnje pomoću opcije „Prijavi zlouporabu”.

Saznaj više

Images from canvas black or transparent: img.src = canvas.toDataURL(); // FF: 'image/jpeg'=black. Png,webp,default=transparent.

  • 7 odgovora
  • 0 ima ovaj problem
  • 196 prikaza
  • Posljednji odgovor od phobrain

više opcija

I don't know if this ever worked in FF, since I've been on Chrome a while. I tried canvas.toBlob() in FF without a solution.

The behavior is the same whether the served image is jpg or webp. A Servlet delivers the image data, here in webp:

          response.setHeader("Content-Type", "image/webp");

On page, I use a canvas thus => see '// FIREFOX'

     var canvas = document.createElement( 'canvas' );
     canvas.width = width;
     canvas.height = avail_height;
     var context = canvas.getContext( '2d' );
     context.drawImage( imgsrc, 0, 0, width, avail_height);
     context.lineWidth = 150;
     // FIREFOX: canvas.toDataURL('image/jpeg')=black, png,webp,default=transparent
     img.src = canvas.toDataURL('image/jpeg');
     img.onmousedown = disableDragging; // for FF
     img.style.opacity = "1.0";
     console.log('setImg ok');

Inspecting a black jpeg:

        data:image/jpeg;base64,...

Thanks!

I don't know if this ever worked in FF, since I've been on Chrome a while. I tried canvas.toBlob() in FF without a solution. The behavior is the same whether the served image is jpg or webp. A Servlet delivers the image data, here in webp: response.setHeader("Content-Type", "image/webp"); On page, I use a canvas thus => see '// FIREFOX' var canvas = document.createElement( 'canvas' ); canvas.width = width; canvas.height = avail_height; var context = canvas.getContext( '2d' ); context.drawImage( imgsrc, 0, 0, width, avail_height); context.lineWidth = 150; // FIREFOX: canvas.toDataURL('image/jpeg')=black, png,webp,default=transparent img.src = canvas.toDataURL('image/jpeg'); img.onmousedown = disableDragging; // for FF img.style.opacity = "1.0"; console.log('setImg ok'); Inspecting a black jpeg: data:image/jpeg;base64,... Thanks!

Izabrano rješenje

Maybe I'll start a new thread since someone trashed it. I can avoid FF since it works on Chrome and Safari..

I posted a URL, but that triggered a review.

I guess this 'solves' this one.

Pročitaj ovaj odgovor u kontekstu 👍 0

Svi odgovori (7)

više opcija

Unlike Chrome, Firefox will happily keep alpha data in memory, but when you request JPEG (which has no alpha channel) it seems to fill transparent areas with black unless you explicitly give it a background. That’s why PNG/WebP look “transparent” and JPEG goes “black.”

You can work around it by painting a background before drawing your image:

var canvas = document.createElement('canvas'); canvas.width = width; canvas.height = avail_height; var ctx = canvas.getContext('2d');

// Fill white background for JPEG export ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.drawImage(imgsrc, 0, 0, width, avail_height);

img.src = canvas.toDataURL('image/jpeg', 0.92); If you need the transparency for PNG/WebP, you can skip the fillRect for those formats.

Also worth checking — if your source image is cross-origin and you haven’t set img.crossOrigin = 'anonymous' before loading, Firefox will block pixel access and toDataURL() can return empty/black results. That one trips people up a lot when coming from Chrome, since Chrome can be a bit more forgiving.

So in short:

Set a solid background before exporting to JPEG.

Make sure crossOrigin is set if you’re pulling images from another domain.

više opcija

Thanks! No effect from the changes, jpeg is still black if I do the resize. The image comes from the same server as the page, load code shown below.

Live version:

   http://phobrain.com/pr/home/view.html

The image content displays if I skip the resize:

   //img.src = imgsrc.src;

vs.

   ...
   context.drawImage( imgsrc, 0, 0, canvas.width, canvas.height);
   img.src = canvas.toDataURL('image/jpeg', 0.92);

Inspecting the resized image shows it seems empty, with this repeating pattern:

Pn6/9oADAMBAAIRAxEAPwD8qqACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKACgAoAKAC...

The request/receipt of the image:

   function nextImage(...) {
       ...
       var newimg = new Image();
       newimg.onerror=function(evt) {...}
       newimg.onload=function() {
           // ... logged dimensions correct 
           imgRcvd[screen] = newimg;
       }
       newimg.src = url + params;
       // "/pr/getmext?sess=rLhF5WBH&sc1=2&sc2=2&d=0&c=l..."
   }

Izmjenjeno od phobrain

više opcija

Thanks! That doesn't fix it. The image (from same sever as the page) displays ok as long as I don't rescale it with the canvas/context. If there were another way to size it to fit, I could try that.

Izmjenjeno od phobrain

više opcija

phobrain said

Thanks! That doesn't fix it. The image (from same server as the page) displays ok as long as I don't rescale it with the canvas/context. If there were another way to size it to fit, I could try that.
više opcija

phobrain said

Thanks! That doesn't fix it. The image (from same sever as the page) displays ok as long as I don't rescale it with the canvas/context. If there were another way to size it to fit, I could try that.

This thread is very hard to follow. Please link to an online demo, such as a lived page, CodePen or JSFiddle, so people can see what you are doing.

više opcija

Odabrano rješenje

Maybe I'll start a new thread since someone trashed it. I can avoid FF since it works on Chrome and Safari..

I posted a URL, but that triggered a review.

I guess this 'solves' this one.

Izmjenjeno od phobrain

više opcija
Postavi pitanje

Za odgovaranje na poruke moraš se prijaviti na tvoj račun. Postavi novo pitanje, ako još nemaš račun.