Answer the question
In order to leave comments, you need to log in
Mathematical calculations when cropping elements with different proportions?
Hello! I am writing an application that has a resize crop. I got stuck on resizing after crop, I can’t figure out the calculations during resizing, the rectangle that is stretched has a picture that should adapt to this rectangle.
There are no problems with the usual resize, but after the crop, the sizes and proportions of the picture and the rectangle change.
If you are not very interested in reading what I wrote below, then straight to the point:
I need help with resizing calculations for two elements - a rectangle that is stretched and a picture inside it. Due to the different sizes of the rectangle and the image after the crop, I can't find the right formula to correctly substitute the image under the rectangle. At the end of the link to the video and code.
How it works:
Element structure:
div.template-element
div.template-element-inner
img.template-element-content
Answer the question
In order to leave comments, you need to log in
Well, of course, it's overkill. The error is fixed quite simply:
if (img) {
//Масштаб ресайза
let scale = this.box.clientWidth/this.resizable.box.source.width;
//Соответственно изменяется размер изображения
img.style.height = img.sourceRect.height *scale + 'px';
img.style.width = img.sourceRect.width *scale + 'px';
let left = тут свойство css left изображения до ресайза
let top = ...
//Не забывает смещать положение изображения согласно масштабу
img.style.top = top*scale+'px';
img.style.left = left*scale+'px';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question