G
G
Genri_Rus2020-06-11 23:32:38
JavaScript
Genri_Rus, 2020-06-11 23:32:38

How to crop image in canvas?

Here is an example: vintagetechnics.ru/comment

When you click on add your review, you can add a picture and it will be loaded as a preview using canvas

. I did something similar: https://codepen.io/Genri_Rus/pen/vYLGxzV

But the problem is that it is highly compressed, an example of a picture:

spoiler
5ee2947acc7f7513279583.jpeg


How to do exactly the same?
5ee294c47f900391398842.png

Those. it is slightly cropped at the top and bottom

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
profesor08, 2020-06-11
@profesor08

For canvas, you need to set the size of the image, or the target size of the image if you want to do something with it. Next, on CSS, set the canvas to the desired size so that it fits normally into the block.

img.onload = function() {
  canvas.width  = img.width;
  canvas.height = img.height;
  ctx.drawImage(img, 0, 0, img.width, img.height);
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question