80 lines
1.6 KiB
TypeScript
80 lines
1.6 KiB
TypeScript
/**
|
|
* 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();
|
|
}
|