HTML5 canvas: is there a way to resize image with “nearest neighbour” resampling?

后端 未结 5 1689
天涯浪人
天涯浪人 2021-02-01 09:41

I have some JS that makes some manipulations with images. I want to have pixelart-like graphics, so I had to enlarge original images in graphics editor. But I think it\'d be goo

5条回答
  •  伪装坚强ぢ
    2021-02-01 10:20

    image-rendering: -webkit-optimize-contrast; /* webkit */
    image-rendering: -moz-crisp-edges /* Firefox */
    

    http://phrogz.net/tmp/canvas_image_zoom.html can provide a fallback case using canvas and getImageData. In short:

    // Create an offscreen canvas, draw an image to it, and fetch the pixels
    var offtx = document.createElement('canvas').getContext('2d');
    offtx.drawImage(img1,0,0);
    var imgData = offtx.getImageData(0,0,img1.width,img1.height).data;
    
    // Draw the zoomed-up pixels to a different canvas context
    for (var x=0;x

    More: MDN docs on image-rendering

提交回复
热议问题