Resizing an image in an HTML5 canvas

后端 未结 18 2729
半阙折子戏
半阙折子戏 2020-11-22 03:37

I\'m trying to create a thumbnail image on the client side using javascript and a canvas element, but when I shrink the image down, it looks terrible. It looks as if it was

18条回答
  •  你的背包
    2020-11-22 03:53

    For resizing to image with width less that original, i use:

        function resize2(i) {
          var cc = document.createElement("canvas");
          cc.width = i.width / 2;
          cc.height = i.height / 2;
          var ctx = cc.getContext("2d");
          ctx.drawImage(i, 0, 0, cc.width, cc.height);
          return cc;
        }
        var cc = img;
        while (cc.width > 64 * 2) {
          cc = resize2(cc);
        }
        // .. than drawImage(cc, .... )
    

    and it works =).

提交回复
热议问题