Resizing a canvas image without blurring it

前端 未结 2 582
无人共我
无人共我 2020-12-31 04:10

I have a small image, which I am rendering on a canvas, like this:

ctx.drawImage(img, 0, 0, img.width*2, img.height*2);

I would like this t

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 05:10

    Check this fiddle: http://jsfiddle.net/yPFjg/

    It loads the image into a canvas, then creates a resized copy and uses that as sprite.

    With few modifications, you can implement an image loader that resizes images on the fly.

    var ctx = document.getElementById('canvas1').getContext('2d');
    var img = new Image();
    var original = document.createElement("canvas");
    var scaled = document.createElement("canvas");
    
    img.onload = function() {
        var oc = original.getContext('2d');
        var sc = scaled.getContext('2d');
        oc.canvas.width = oc.canvas.height = 16;
        sc.canvas.width = sc.canvas.height = 32;
        oc.drawImage(this, 0, 0);    
        var od = oc.getImageData(0,0,16,16);
        var sd = sc.getImageData(0,0,32,32);
        for (var x=0; x<32; x++) {
            for (var y=0; y<32; y++) {
                for (var c=0; c<4; c++) {        
                    // you can improve these calculations, I let them so for clarity        
                    sd.data[(y*32+x)*4+c] = od.data[((y>>1)*16+(x>>1))*4+c];
                }
            }
        }
        sc.putImageData(sd, 0, 0);
        ctx.drawImage(scaled, 0, 0);    
    }
    
    img.src = document.getElementById('sprite').src;
    

    Some notes about getImageData: it returns an object with an array. The array has a height*width*4 size. The color components are stored in RGBA order (red, green, blue, alpha, 8 bits each value).

提交回复
热议问题