I am writing a little script that allows the user to move and resize the div. I need to keep aspect ratio and my logic is not working.
function resizing() {
var currentHeight = elmnt.offsetHeight;
var currentWidth = elmnt.offsetWidth;
var newHeight = currentHeight + (event.pageY - currentY);
var newWidth = currentWidth + (event.pageX - currentX);
var ratio = currentWidth / currentHeight;
if(ratio < 1) {
newwidth = parseInt(newHeight * ratio);
}
else {
newheight = parseInt(newWidth / ratio);
}
elmnt.style.height = newHeight + "px";
elmnt.style.width = newWidth + "px";
currentY = event.pageY;
currentX = event.pageX;
}
Job script. But, unfortunately, it does not fully support the aspect ratio. Sometimes, when I change only horizontally, the old height remains unchanged, sometimes it works, but one length changes with a slight shift.
When I resize up and down and again and again, the lengths become more and more equal, and when it is the right square, everything is correct.
Hwo can I fix my problems? Where is my mistake ?!
source
share