72 lines
4.4 KiB
TypeScript
72 lines
4.4 KiB
TypeScript
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 === "frame" ? 640 : node.data.kind === "plain-image" ? 320 : node.data.kind === "icon" ? 80 : node.data.kind === "plain-text" ? 240 : 260),
|
|
height: node.height || Number(node.style?.height) || node.measured?.height || (node.data.kind === "frame" ? 400 : node.data.kind === "plain-image" ? 240 : node.data.kind === "icon" ? 80 : 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";
|
|
const icon = node.data.kind === "icon" || node.data.kind === "plain-image";
|
|
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));
|
|
if (icon) {
|
|
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;
|
|
scale = Math.max(24 / width, 24 / height, Math.min(scale, 3000 / width, 3000 / height));
|
|
nextWidth = width * scale;
|
|
nextHeight = height * 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,
|
|
};
|
|
}
|
|
|
|
export type ConnectorSide = "top" | "right" | "bottom" | "left";
|
|
export type Connector = { point: XYPosition; normal: XYPosition };
|
|
/** Attach to the card border itself, independent of the handle's hit area. */
|
|
export function nodeConnector(node: CanvasNode, side: ConnectorSide): Connector {
|
|
const { width, height } = nodeSize(node);
|
|
const vectors = { top: { x: 0, y: -1 }, right: { x: 1, y: 0 }, bottom: { x: 0, y: 1 }, left: { x: -1, y: 0 } };
|
|
const local = vectors[side];
|
|
const angle = node.data.rotation || 0;
|
|
const normal = rotateVector(local, angle);
|
|
const offset = rotateVector({ x: local.x * width / 2, y: local.y * height / 2 }, angle);
|
|
const center = nodeCenter(node);
|
|
return { point: { x: center.x + offset.x, y: center.y + offset.y }, normal };
|
|
}
|
|
export function connectorBezier(from: Connector, to: Connector) {
|
|
const reach = Math.min(240, Math.max(40, Math.hypot(to.point.x - from.point.x, to.point.y - from.point.y) * 0.4));
|
|
return `M ${from.point.x},${from.point.y} C ${from.point.x + from.normal.x * reach},${from.point.y + from.normal.y * reach} ${to.point.x + to.normal.x * reach},${to.point.y + to.normal.y * reach} ${to.point.x},${to.point.y}`;
|
|
}
|