Invalid blending results across all browsers with HTML5 canvas

后端 未结 2 1097
北海茫月
北海茫月 2021-01-01 14:49

Summary

When repeatedly drawing anything (apparently with a low alpha value) on a canvas, regardless of if it\'s with drawImage() or a fill

2条回答
  •  渐次进展
    2021-01-01 15:53

    Oh geez. I think we just stepped into the twilight zone here. I'm afraid I don't have an answer but I can provide some more information at least. Your hunch is right that at least something is larger here.

    I'd prefer an example that is drastically more simple (and thus less prone to any sort of error - I fear you might have a problem with 0-255 somewhere instead of 0-1 but didnt look thoroughly) than yours so I whipped up something tiny. What I found wasn't pleasant:

    
    
    

    +

    var can = document.getElementById('canvas1');
    var ctx = can.getContext('2d');
    var can2 = document.getElementById('canvas2');
    var ctx2 = can2.getContext('2d');
    
    var alpha = 2/255.0;
    
    ctx.fillStyle = 'rgba(127, 0, 0, ' + alpha + ')';
    ctx2.fillStyle = 'rgba(127, 0, 0, ' + alpha + ')'; // this is grey
    //ctx2.fillStyle = 'rgba(171, 0, 0, ' + alpha + ')'; // this works
    
    for (var i = 0; i < 32; i++) {
          ctx.fillRect(0,0,500,500);
          ctx2.fillRect(0,0,500,500);
    }
    

    Yields this:

    enter image description here

    See for yourself here:

    http://jsfiddle.net/jjAMX/

    It seems that if your surface area is too small (not the fillRect area, but the canvas area itself) then the drawing operation will be a wash.

    It seems to work fine in IE9 and FF7. I submitted it as a bug to Chromium. (We'll see what they say, but their typical M.O. so far is to ignore most bug reports unless they are security issues).

    I'd urge you to check your example - I think you might have a 0-255 to 0-1 issue somewhere. The code I have here seems to work just dandy, save for maybe the expected reddish color, but it is a consistent resultant color across all browsers with the exception of this Chrome issue.

    In the end Chrome is probably doing something to optimize canvases with a surface area smaller than 128x128 that is fouling things up.

提交回复
热议问题