This commit is contained in:
Danilo Reyes
2025-11-02 14:13:56 -06:00
parent cd8ce33f5e
commit ce0b692aee
23 changed files with 2049 additions and 50 deletions

View File

@@ -0,0 +1,79 @@
/**
* Image rotation transformations
* Non-destructive rotation of canvas images
*/
import type Konva from 'konva';
/**
* Rotate image to specific angle (0-360 degrees)
*/
export function rotateImageTo(
image: Konva.Image | Konva.Group,
degrees: number,
animate: boolean = false
): void {
// Normalize to 0-360
const normalizedDegrees = ((degrees % 360) + 360) % 360;
if (animate) {
image.to({
rotation: normalizedDegrees,
duration: 0.3,
});
} else {
image.rotation(normalizedDegrees);
}
}
/**
* Rotate image by delta degrees
*/
export function rotateImageBy(
image: Konva.Image | Konva.Group,
degrees: number,
animate: boolean = false
): void {
const currentRotation = image.rotation();
const newRotation = (((currentRotation + degrees) % 360) + 360) % 360;
rotateImageTo(image, newRotation, animate);
}
/**
* Rotate image by 90 degrees clockwise
*/
export function rotateImage90CW(image: Konva.Image | Konva.Group): void {
rotateImageBy(image, 90);
}
/**
* Rotate image by 90 degrees counter-clockwise
*/
export function rotateImage90CCW(image: Konva.Image | Konva.Group): void {
rotateImageBy(image, -90);
}
/**
* Flip image to 180 degrees
*/
export function rotateImage180(image: Konva.Image | Konva.Group): void {
rotateImageTo(image, 180);
}
/**
* Reset rotation to 0 degrees
*/
export function resetImageRotation(
image: Konva.Image | Konva.Group,
animate: boolean = false
): void {
rotateImageTo(image, 0, animate);
}
/**
* Get current rotation angle
*/
export function getImageRotation(image: Konva.Image | Konva.Group): number {
return image.rotation();
}