Windows 10 已于2025年10月14日停止支持。如果您正在使用 Windows 10,参见 这篇文章

Windows 10 reached EOS (end of support) on October 14, 2025. If you are on Windows 10, see this article.

搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解
Solved 已存档

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

phobrain 已回复
phobrain

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!
定位到答案原位置

选择的解决方案

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.

由phobrain于修改

所有回复 (7)

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.

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

由phobrain于修改

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.

由phobrain于修改

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.

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.

选择的解决方案

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.

由phobrain于修改