sketch-json-parse
Version:
sketch-json解析
48 lines (47 loc) • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRotatedRectangleBounds = getRotatedRectangleBounds;
function degreesToRadians(degrees) {
return degrees * (Math.PI / 180);
}
function rotatePoint(point, angle) {
const cosTheta = Math.cos(angle);
const sinTheta = Math.sin(angle);
return {
x: point.x * cosTheta - point.y * sinTheta,
y: point.x * sinTheta + point.y * cosTheta,
};
}
function getRotatedRectangleBounds(rect) {
const { x, y, width, height } = rect;
const rotation = degreesToRadians(rect.rotation);
// 矩形的四个顶点
const points = [
{ x, y },
{ x: x + width, y },
{ x, y: y + height },
{ x: x + width, y: y + height },
];
// 旋转后的顶点
const rotatedPoints = points.map((point) => rotatePoint(point, rotation));
// 计算最小和最大 x, y 值
const minX = Math.min(...rotatedPoints.map((p) => p.x));
const maxX = Math.max(...rotatedPoints.map((p) => p.x));
const minY = Math.min(...rotatedPoints.map((p) => p.y));
const maxY = Math.max(...rotatedPoints.map((p) => p.y));
return {
x: x + (width - (maxX - minX)) / 2,
y: y + (height - (maxY - minY)) / 2,
width: maxX - minX,
height: maxY - minY,
};
}
// 示例使用
// const rect = {
// x: 95,
// y: 458,
// width: 150,
// height: 38,
// rotation: 20,
// };
// const bounds = getRotatedRectangleBounds(rect);