Skip to content

Instantly share code, notes, and snippets.

@DimitryDushkin
Last active March 4, 2018 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DimitryDushkin/e2eb5d7cd7129c804cf0bfa7376809a3 to your computer and use it in GitHub Desktop.
Save DimitryDushkin/e2eb5d7cd7129c804cf0bfa7376809a3 to your computer and use it in GitHub Desktop.
Code from Yandex Zen narrative editor
type DraggerPosition = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
const computeGeometryWhileDragging = (
position: DraggerPosition,
preserveAspectRatio: boolean,
initialGeometry: SlideElementGeometry,
currentGeometry: SlideElementGeometry,
dx: number,
dy: number
): SlideElementGeometry => {
const startWidth = initialGeometry.width;
const startHeight = initialGeometry.height;
const startX = initialGeometry.x;
const startY = initialGeometry.y;
// Необходимо умножать на (startWidth / startHeight) и (startHeight / startWidth),
// чтобы привести смещения к единой шкале измерения
const isScalingByWidth = (startWidth - dx) * startWidth / startHeight
> (startHeight - dy) * startHeight / startWidth;
const geometry = {};
if (position === 'top-left') {
if (preserveAspectRatio) {
if (isScalingByWidth) {
geometry.width = startWidth - dx;
geometry.height = startHeight / startWidth * geometry.width;
} else {
geometry.height = startHeight - dy;
geometry.width = startWidth / startHeight * geometry.height;
}
} else {
geometry.width = startWidth - dx;
geometry.height = startHeight - dy;
}
geometry.x = startX + startWidth - geometry.width;
geometry.y = startY + startHeight - geometry.height;
} else if (position === 'top-right') {
if (preserveAspectRatio) {
if (isScalingByWidth) {
geometry.width = startWidth + dx;
geometry.height = startHeight / startWidth * geometry.width;
} else {
geometry.height = startHeight - dy;
geometry.width = startWidth / startHeight * geometry.height;
}
geometry.y = startY + startHeight - geometry.height;
} else {
geometry.width = startWidth + dx;
geometry.height = startHeight - dy;
geometry.y = startY + dy;
}
} else if (position === 'bottom-left') {
if (preserveAspectRatio) {
if (isScalingByWidth) {
geometry.width = startWidth - dx;
geometry.height = startHeight / startWidth * geometry.width;
} else {
geometry.height = startHeight + dy;
geometry.width = startWidth / startHeight * geometry.height;
}
geometry.x = startX + startWidth - geometry.width;
} else {
geometry.width = startWidth - dx;
geometry.height = startHeight + dy;
geometry.x = startX + dx;
}
} else if (position === 'bottom-right') {
if (preserveAspectRatio) {
if (isScalingByWidth) {
geometry.width = startWidth + dx;
geometry.height = startHeight / startWidth * geometry.width;
} else {
geometry.height = startHeight + dy;
geometry.width = startWidth / startHeight * geometry.height;
}
} else {
geometry.width = startWidth + dx;
geometry.height = startHeight + dy;
}
}
if (geometry.width < MIN_WIDTH || geometry.height < MIN_HEIGHT) {
return currentGeometry;
}
return geometry;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment