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

后端 未结 5 1705
天涯浪人
天涯浪人 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:32

    I'll echo what others have said and tell you it's not a built-in function. After running into the same issue, I've made one below.

    It uses fillRect() instead of looping through each pixel and painting it. Everything is commented to help you better understand how it works.

    //img is the original image, scale is a multiplier. It returns the resized image.
    function Resize_Nearest_Neighbour( img, scale ){
        //make shortcuts for image width and height
        var w = img.width;
        var h = img.height;
    
        //---------------------------------------------------------------
        //draw the original image to a new canvas
        //---------------------------------------------------------------
    
        //set up the canvas
        var c = document.createElement("CANVAS");
        var ctx = c.getContext("2d");
        //disable antialiasing on the canvas
        ctx.imageSmoothingEnabled = false;
        //size the canvas to match the input image
        c.width = w;
        c.height = h;
        //draw the input image
        ctx.drawImage( img, 0, 0 );
        //get the input image as image data
        var inputImg = ctx.getImageData(0,0,w,h);
        //get the data array from the canvas image data
        var data = inputImg.data;
    
        //---------------------------------------------------------------
        //resize the canvas to our bigger output image
        //---------------------------------------------------------------
        c.width = w * scale;
        c.height = h * scale;
        //---------------------------------------------------------------
        //loop through all the data, painting each pixel larger
        //---------------------------------------------------------------
        for ( var i = 0; i < data.length; i+=4 ){
    
            //find the colour of this particular pixel
            var colour = "#";
    
            //---------------------------------------------------------------
            //convert the RGB numbers into a hex string. i.e. [255, 10, 100]
            //into "FF0A64"
            //---------------------------------------------------------------
            function _Dex_To_Hex( number ){
                var out = number.toString(16);
                if ( out.length < 2 ){
                    out = "0" + out;
                }
                return out;
            }
            for ( var colourIndex = 0; colourIndex < 3; colourIndex++ ){
                colour += _Dex_To_Hex( data[ i+colourIndex ] );
            }
            //set the fill colour
            ctx.fillStyle = colour;
    
            //---------------------------------------------------------------
            //convert the index in the data array to x and y coordinates
            //---------------------------------------------------------------
            var index = i/4;
            var x = index % w;
            //~~ is a faster way to do 'Math.floor'
            var y = ~~( index / w );
            //---------------------------------------------------------------
            //draw an enlarged rectangle on the enlarged canvas
            //---------------------------------------------------------------
            ctx.fillRect( x*scale, y*scale, scale, scale );
        }
    
        //get the output image from the canvas
        var output = c.toDataURL("image/png");
        //returns image data that can be plugged into an img tag's src
        return output;
    }
    

    Below is an example of it in use.

    Your image would appear in the HTML like this:

    
    

    The data-src tag contains the URL for the image you want to enlarge. This is a custom data tag. The code below will take the image URL from the data tag and put it through the resizing function, returning a larger image (30x the original size) which then gets injected into the src attribute of the img tag.

    Remember to put the function Resize_Nearest_Neighbour (above) into the

提交回复
热议问题