feat(canvas): add inherited elements, resizing and rotation
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import type { XYPosition } from "@xyflow/react";
|
||||
import type { CanvasNode } from "./canvas-elements";
|
||||
|
||||
export type ResizeDirection = "top" | "right" | "bottom" | "left" | "top-left" | "top-right" | "bottom-left" | "bottom-right";
|
||||
export type NodeTransform = { position?: XYPosition; width?: number; height?: number; rotation?: number; fontSize?: number };
|
||||
export function nodeSize(node: CanvasNode) {
|
||||
return {
|
||||
width: node.width || Number(node.style?.width) || node.measured?.width || (node.data.kind === "plain-text" ? 240 : 260),
|
||||
height: node.height || Number(node.style?.height) || node.measured?.height || (node.data.kind === "plain-text" ? 72 : 200),
|
||||
};
|
||||
}
|
||||
export function normalizeRotation(angle: number) { return ((angle % 360) + 360) % 360; }
|
||||
export function rotateVector(point: XYPosition, angle: number): XYPosition {
|
||||
const radians = angle * Math.PI / 180;
|
||||
return { x: point.x * Math.cos(radians) - point.y * Math.sin(radians), y: point.x * Math.sin(radians) + point.y * Math.cos(radians) };
|
||||
}
|
||||
export function nodeCenter(node: CanvasNode): XYPosition {
|
||||
const { width, height } = nodeSize(node);
|
||||
return { x: node.position.x + width / 2, y: node.position.y + height / 2 };
|
||||
}
|
||||
export function rotationFromPointer(node: CanvasNode, start: XYPosition, current: XYPosition, snap = false) {
|
||||
const center = nodeCenter(node);
|
||||
const startAngle = Math.atan2(start.y - center.y, start.x - center.x);
|
||||
const angle = Math.atan2(current.y - center.y, current.x - center.x);
|
||||
let rotation = (node.data.rotation || 0) + (angle - startAngle) * 180 / Math.PI;
|
||||
if (snap) rotation = Math.round(rotation / 15) * 15;
|
||||
return normalizeRotation(rotation);
|
||||
}
|
||||
export function resizeFromPointer(node: CanvasNode, direction: ResizeDirection, delta: XYPosition): NodeTransform {
|
||||
const { width, height } = nodeSize(node);
|
||||
const rotation = node.data.rotation || 0;
|
||||
const local = rotateVector(delta, -rotation);
|
||||
const horizontal = direction.includes("left") ? -1 : direction.includes("right") ? 1 : 0;
|
||||
const vertical = direction.includes("top") ? -1 : direction.includes("bottom") ? 1 : 0;
|
||||
const plain = node.data.kind === "plain-text";
|
||||
let nextWidth = Math.max(plain ? 60 : 140, Math.min(3000, width + horizontal * local.x));
|
||||
let nextHeight = Math.max(plain ? 24 : 100, Math.min(3000, height + vertical * local.y));
|
||||
let fontSize: number | undefined;
|
||||
if (plain) {
|
||||
const sx = 1 + horizontal * local.x / width;
|
||||
const sy = 1 + vertical * local.y / height;
|
||||
let scale = !horizontal ? sy : !vertical ? sx : Math.abs(sx - 1) > Math.abs(sy - 1) ? sx : sy;
|
||||
const font = node.data.fontSize || 24;
|
||||
scale = Math.max(60 / width, 24 / height, 8 / font, Math.min(scale, 3000 / width, 3000 / height, 160 / font));
|
||||
nextWidth = width * scale;
|
||||
nextHeight = height * scale;
|
||||
fontSize = font * scale;
|
||||
}
|
||||
const center = nodeCenter(node);
|
||||
const shift = rotateVector({ x: horizontal * (nextWidth - width) / 2, y: vertical * (nextHeight - height) / 2 }, rotation);
|
||||
return {
|
||||
position: { x: center.x + shift.x - nextWidth / 2, y: center.y + shift.y - nextHeight / 2 },
|
||||
width: nextWidth, height: nextHeight, ...(fontSize !== undefined ? { fontSize } : {}),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user