Canvas has inconsistent pixel grid across browsers when using drawImage()

前端 未结 2 454
栀梦
栀梦 2021-01-21 06:55

I recognize that Canvas drawImage is inexplicably offset by 1 Pixel is a very similar issue, but I was already applying the advice given in that question\'s answer before I

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 07:14

    I can't tell for IE, but at least for Safari, this sounds like a bug in their nearest-neighbor algorithm when the transformation matrix translate is set to exactly n.5.

    onload = function() {
    
      var ctx = document.querySelector('canvas').getContext('2d');
      ctx.imageSmoothingEnabled = false;
      var img = document.querySelector('#hero');
    
      ctx.setTransform(2, 0, 0, 2, 99.49, 99.49);
      ctx.drawImage(img, 32, 32, 16, 16, 0, 0, 16, 16);
    
      ctx.setTransform(2, 0, 0, 2, 99.5, 99.5);
      ctx.drawImage(img, 32, 32, 16, 16, 16, 0, 16, 16);
    
      ctx.setTransform(2, 0, 0, 2, 99.51, 99.51);
      ctx.drawImage(img, 32, 32, 16, 16, 32, 0, 16, 16);
    
    };
    
    

    Result in Safari 11.0.3

    You may want to let them know about it from their bug-tracker.

    The workaround would be to make sure that you never lay on floating coordinates, even in your transformation matrix.

提交回复
热议问题