user5182503
user5182503

Reputation:

HTML5: canvas and image size

I want to draw an image on canvas using JS. This is my html code:

<canvas style="width:1000px;height:600px;border:1px solid black;" id="canvas"> </canvas>

This is my js code:

    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    var img = new Image();
    img.onload = function(){
      ctx.drawImage(img, 0, 0,1000,600)
    }
    img.src = someUrlToJpgImage;

This is the origin jpg image (1000x600) which is located on server: enter image description here

This is the result (1000x600) I see in browser: enter image description here

As you see this is scaled the top left corner of the image but not the whole image as expected. I tried to add to js code:

ctx.imageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;

but it didn't help.

How to solve this problem?

Upvotes: 0

Views: 72

Answers (1)

Schlaus
Schlaus

Reputation: 19212

You should set the amount of pixels desired on the canvas element:

<canvas style="width:1000px;height:600px;border:1px solid black;" height="600" width="1000" id="canvas"> </canvas>

Upvotes: 1

Related Questions