javascripthtmlimagecanvasblurry

Image on canvas looks different from image in html <img> tag


I am trying to use the HTML canvas to crop an image. But even before cropping the image, the image looks blurry when I rendered it on the canvas. I then rendered the same image using HTML tag, and the image wasn't blurry. I used a width and height of 300px for both the canvas and the img tag. I can't seem to be able to find out why the image looks blurry in the canvas.

const img = document.querySelector(".my-image");
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, canvas.width, canvas.height);

The image rendered on the image tag looks like this: Image rendered with tag

And the image rendered on the canvas looks like this: Image rendered on canvas


Solution

  • If I look at the two images you share, the two images seem to be 600x600px.

    Information about the image sizes given by the broswer