Join the other Mozillians on a week-long Ask a Fox event to win badges and special merch!

Eheka Pytyvõha

Emboyke pytyvõha apovai. Ndorojeruremo’ãi ehenói térã eñe’ẽmondóvo pumbyrýpe ha emoherakuãvo marandu nemba’etéva. Emombe’u tembiapo imarãkuaáva ko “Marandu iñañáva” rupive.

Kuaave

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

  • 7 Mbohovái
  • 0 oguereko ko apañuãi
  • 137 Hecha
  • Mbohovái ipaháva phobrain

more options

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!

Ñemoĩporã poravopyre

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.

Emoñe’ẽ ko mbohavái ejeregua reheve 👍 0

Opaite Mbohovái (7)

more options

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.

¿Imba’eporãva?

more options

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..."
   }

Moambuepyre phobrain rupive

¿Imba’eporãva?

more options

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.

Moambuepyre phobrain rupive

¿Imba’eporãva?

more options

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.

¿Imba’eporãva?

more options

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.

¿Imba’eporãva?

more options

Ñemoĩporã poravopyre

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.

Moambuepyre phobrain rupive

¿Imba’eporãva?

more options

¿Imba’eporãva?

Eporandumína

Nde eikéke nde mba’etepe embohovái hag̃ua ñe’ẽmondo. Ikatúpa, emoñepyrũ peteĩ porandu, ndereguerekói gueteriramo nemba’ete.