Canvas drawImage scales my square image to a rectangle [duplicate]











up vote
1
down vote

favorite













This question already has an answer here:




  • Canvas is stretched when using CSS but normal with “width” / “height” properties

    7 answers




I have a fairly simple test, a 400 x 400 white image with text on it saying "1" over and over again.



I draw it on a fairly simple 1000 x 1000 canvas, trying to resize it to 100 x 100.



var image = new Image();
document.body.appendChild(image);

image.addEventListener("load",function (event) {
var image1 = event.target;
var tempCanvas = window.document.createElement("canvas");

tempCanvas.style.width = "1000px";
tempCanvas.style.height = "1000px";
tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

document.body.appendChild(tempCanvas);

tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
});

image.src = "1.png";


But despite all that being squares, I end up with an odd-looking, deformed, weirdly scaled result that's rectangular, low quality, and without having any of its dimensions being 100px.



Odd result



On the left, you can see the original image, on the right, that's the top left corner of my canvas.



If you want the original image, here it is: https://i.stack.imgur.com/yDkLx.png



What am I missing?










share|improve this question















marked as duplicate by Kaiido javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 4:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    1
    down vote

    favorite













    This question already has an answer here:




    • Canvas is stretched when using CSS but normal with “width” / “height” properties

      7 answers




    I have a fairly simple test, a 400 x 400 white image with text on it saying "1" over and over again.



    I draw it on a fairly simple 1000 x 1000 canvas, trying to resize it to 100 x 100.



    var image = new Image();
    document.body.appendChild(image);

    image.addEventListener("load",function (event) {
    var image1 = event.target;
    var tempCanvas = window.document.createElement("canvas");

    tempCanvas.style.width = "1000px";
    tempCanvas.style.height = "1000px";
    tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

    document.body.appendChild(tempCanvas);

    tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
    });

    image.src = "1.png";


    But despite all that being squares, I end up with an odd-looking, deformed, weirdly scaled result that's rectangular, low quality, and without having any of its dimensions being 100px.



    Odd result



    On the left, you can see the original image, on the right, that's the top left corner of my canvas.



    If you want the original image, here it is: https://i.stack.imgur.com/yDkLx.png



    What am I missing?










    share|improve this question















    marked as duplicate by Kaiido javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 20 at 4:34


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      This question already has an answer here:




      • Canvas is stretched when using CSS but normal with “width” / “height” properties

        7 answers




      I have a fairly simple test, a 400 x 400 white image with text on it saying "1" over and over again.



      I draw it on a fairly simple 1000 x 1000 canvas, trying to resize it to 100 x 100.



      var image = new Image();
      document.body.appendChild(image);

      image.addEventListener("load",function (event) {
      var image1 = event.target;
      var tempCanvas = window.document.createElement("canvas");

      tempCanvas.style.width = "1000px";
      tempCanvas.style.height = "1000px";
      tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

      document.body.appendChild(tempCanvas);

      tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
      });

      image.src = "1.png";


      But despite all that being squares, I end up with an odd-looking, deformed, weirdly scaled result that's rectangular, low quality, and without having any of its dimensions being 100px.



      Odd result



      On the left, you can see the original image, on the right, that's the top left corner of my canvas.



      If you want the original image, here it is: https://i.stack.imgur.com/yDkLx.png



      What am I missing?










      share|improve this question
















      This question already has an answer here:




      • Canvas is stretched when using CSS but normal with “width” / “height” properties

        7 answers




      I have a fairly simple test, a 400 x 400 white image with text on it saying "1" over and over again.



      I draw it on a fairly simple 1000 x 1000 canvas, trying to resize it to 100 x 100.



      var image = new Image();
      document.body.appendChild(image);

      image.addEventListener("load",function (event) {
      var image1 = event.target;
      var tempCanvas = window.document.createElement("canvas");

      tempCanvas.style.width = "1000px";
      tempCanvas.style.height = "1000px";
      tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

      document.body.appendChild(tempCanvas);

      tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
      });

      image.src = "1.png";


      But despite all that being squares, I end up with an odd-looking, deformed, weirdly scaled result that's rectangular, low quality, and without having any of its dimensions being 100px.



      Odd result



      On the left, you can see the original image, on the right, that's the top left corner of my canvas.



      If you want the original image, here it is: https://i.stack.imgur.com/yDkLx.png



      What am I missing?





      This question already has an answer here:




      • Canvas is stretched when using CSS but normal with “width” / “height” properties

        7 answers








      javascript image canvas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 4:30

























      asked Nov 20 at 4:01









      Yann Bergonzat

      351519




      351519




      marked as duplicate by Kaiido javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 at 4:34


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Kaiido javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 at 4:34


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Try setting the width and height values on the canvas element prior to the drawImage(), rather than relying on the styles as you are.



          Setting the width and height attributes corresponds to setting the dimensions of that canvas element.



          Once you've defined the dimensions of a canvas element, the rendering behavior of the canvas becomes much more predicatable:






          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }





          The code snippet above shows the original source image with red border, and the down-scaled canvas rendered image with blue border - hope this helps!






          share|improve this answer























          • If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
            – Yann Bergonzat
            Nov 20 at 4:26










          • strange - can you please upload your source image, eg the image with the 1's
            – Dacre Denny
            Nov 20 at 4:27










          • i.stack.imgur.com/yDkLx.png Done :)
            – Yann Bergonzat
            Nov 20 at 4:28








          • 1




            @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
            – Dacre Denny
            Nov 20 at 4:32






          • 1




            @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
            – Dacre Denny
            Nov 20 at 4:37


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Try setting the width and height values on the canvas element prior to the drawImage(), rather than relying on the styles as you are.



          Setting the width and height attributes corresponds to setting the dimensions of that canvas element.



          Once you've defined the dimensions of a canvas element, the rendering behavior of the canvas becomes much more predicatable:






          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }





          The code snippet above shows the original source image with red border, and the down-scaled canvas rendered image with blue border - hope this helps!






          share|improve this answer























          • If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
            – Yann Bergonzat
            Nov 20 at 4:26










          • strange - can you please upload your source image, eg the image with the 1's
            – Dacre Denny
            Nov 20 at 4:27










          • i.stack.imgur.com/yDkLx.png Done :)
            – Yann Bergonzat
            Nov 20 at 4:28








          • 1




            @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
            – Dacre Denny
            Nov 20 at 4:32






          • 1




            @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
            – Dacre Denny
            Nov 20 at 4:37















          up vote
          1
          down vote



          accepted










          Try setting the width and height values on the canvas element prior to the drawImage(), rather than relying on the styles as you are.



          Setting the width and height attributes corresponds to setting the dimensions of that canvas element.



          Once you've defined the dimensions of a canvas element, the rendering behavior of the canvas becomes much more predicatable:






          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }





          The code snippet above shows the original source image with red border, and the down-scaled canvas rendered image with blue border - hope this helps!






          share|improve this answer























          • If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
            – Yann Bergonzat
            Nov 20 at 4:26










          • strange - can you please upload your source image, eg the image with the 1's
            – Dacre Denny
            Nov 20 at 4:27










          • i.stack.imgur.com/yDkLx.png Done :)
            – Yann Bergonzat
            Nov 20 at 4:28








          • 1




            @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
            – Dacre Denny
            Nov 20 at 4:32






          • 1




            @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
            – Dacre Denny
            Nov 20 at 4:37













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Try setting the width and height values on the canvas element prior to the drawImage(), rather than relying on the styles as you are.



          Setting the width and height attributes corresponds to setting the dimensions of that canvas element.



          Once you've defined the dimensions of a canvas element, the rendering behavior of the canvas becomes much more predicatable:






          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }





          The code snippet above shows the original source image with red border, and the down-scaled canvas rendered image with blue border - hope this helps!






          share|improve this answer














          Try setting the width and height values on the canvas element prior to the drawImage(), rather than relying on the styles as you are.



          Setting the width and height attributes corresponds to setting the dimensions of that canvas element.



          Once you've defined the dimensions of a canvas element, the rendering behavior of the canvas becomes much more predicatable:






          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }





          The code snippet above shows the original source image with red border, and the down-scaled canvas rendered image with blue border - hope this helps!






          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }





          var image = new Image();
          document.body.appendChild(image);

          image.addEventListener("load",function (event) {
          var image1 = event.target;
          var tempCanvas = window.document.createElement("canvas");

          //tempCanvas.style.width = "400px";
          //tempCanvas.style.height = "400px";
          tempCanvas.style.background = "rgba(0, 0, 0, 0.1)";

          tempCanvas.width = 100;
          tempCanvas.height = 100;

          document.body.appendChild(tempCanvas);

          tempCanvas.getContext("2d").drawImage(image1, 0, 0, 100, 100);
          });

          image.src = "https://puu.sh/C4HE2/d96b531d08.png";

          canvas {
          border:1px solid blue;
          }

          img {
          border:1px solid red;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 4:42

























          answered Nov 20 at 4:15









          Dacre Denny

          9,0744729




          9,0744729












          • If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
            – Yann Bergonzat
            Nov 20 at 4:26










          • strange - can you please upload your source image, eg the image with the 1's
            – Dacre Denny
            Nov 20 at 4:27










          • i.stack.imgur.com/yDkLx.png Done :)
            – Yann Bergonzat
            Nov 20 at 4:28








          • 1




            @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
            – Dacre Denny
            Nov 20 at 4:32






          • 1




            @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
            – Dacre Denny
            Nov 20 at 4:37


















          • If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
            – Yann Bergonzat
            Nov 20 at 4:26










          • strange - can you please upload your source image, eg the image with the 1's
            – Dacre Denny
            Nov 20 at 4:27










          • i.stack.imgur.com/yDkLx.png Done :)
            – Yann Bergonzat
            Nov 20 at 4:28








          • 1




            @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
            – Dacre Denny
            Nov 20 at 4:32






          • 1




            @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
            – Dacre Denny
            Nov 20 at 4:37
















          If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
          – Yann Bergonzat
          Nov 20 at 4:26




          If I do that, the image is still weirdly resized, only thing is the canvas is smaller. Also, that doesn't suit my needs, I won't always have an image that's exactly the size of the canvas. puu.sh/C4Hz2/b81c6f836d.png
          – Yann Bergonzat
          Nov 20 at 4:26












          strange - can you please upload your source image, eg the image with the 1's
          – Dacre Denny
          Nov 20 at 4:27




          strange - can you please upload your source image, eg the image with the 1's
          – Dacre Denny
          Nov 20 at 4:27












          i.stack.imgur.com/yDkLx.png Done :)
          – Yann Bergonzat
          Nov 20 at 4:28






          i.stack.imgur.com/yDkLx.png Done :)
          – Yann Bergonzat
          Nov 20 at 4:28






          1




          1




          @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
          – Dacre Denny
          Nov 20 at 4:32




          @YannBergonzat thanks :-) , just updated answer with code snippet - seems to work for me, does this produce expected results for you?
          – Dacre Denny
          Nov 20 at 4:32




          1




          1




          @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
          – Dacre Denny
          Nov 20 at 4:37




          @YannBergonzat yes that's correct - typically when you work with canvas, you'll want to set the width and height to define the resolution/dimensions of the canvas. The styling just sets the dimensions of the canvas in terms of the box-model, which bear no meaning when calling functions like drawImage()
          – Dacre Denny
          Nov 20 at 4:37



          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python