HTML5 - Canvas, drawImage() draws image blurry

后端 未结 6 1508
[愿得一人]
[愿得一人] 2020-12-15 20:29

I am trying to draw the following image to a canvas but it appears blurry despite defining the size of the canvas. As you can see below, the image is crisp and clear whereas

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 20:56

    The reason this is happening is because of Anti Aliasing. Simply set the imageSmoothingEnabled to false like so

    context.imageSmoothingEnabled = false;
    

    Here is a jsFiddle verson

    jsFiddle : https://jsfiddle.net/mt8sk9cb/

    var c = document.getElementById('canvas')
    var ctx = c.getContext('2d')
    var playerImg = new Image()
    
    // http://i.imgur.com/ruZv0dl.png sees a CLEAR, CRISP image
    playerImg.src = 'http://i.imgur.com/ruZv0dl.png'
    
    playerImg.onload = function() {
      ctx.imageSmoothingEnabled = false;
      ctx.drawImage(playerImg, 0, 0, 256, 256);
    };
    

提交回复
热议问题