UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

38 lines 1.32 kB
import { transform } from './transform.js'; /** * Rotate an image anti-clockwise of a given angle. * @param image - Original image. * @param angle - Angle in degrees. * @param options - Rotate options. * @returns A new rotated image. */ export function transformRotate(image, angle, options = {}) { const { center = 'center', scale = 1, ...otherOptions } = options; let centerCoordinates; if (typeof center === 'string') { centerCoordinates = image.getCoordinates(center); } else { centerCoordinates = center; } const transformMatrix = getRotationMatrix(angle, centerCoordinates, scale); return transform(image, transformMatrix, otherOptions); } /** * Generates a rotation matrix for the given angle. * @param angle - Angle in degrees. * @param center - Center point of the image. * @param scale - Scaling factor. * @returns 3 x 3 rotation matrix. */ function getRotationMatrix(angle, center, scale) { const angleRadians = (angle * Math.PI) / 180; const cos = scale * Math.cos(angleRadians); const sin = scale * Math.sin(angleRadians); return [ [cos, sin, (1 - cos) * center.column - sin * center.row], [-sin, cos, sin * center.column + (1 - cos) * center.row], [0, 0, 1], ]; } //# sourceMappingURL=transformRotate.js.map