From c7d5c863ecffb6842921fbe896479254f6213729 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Fri, 8 Nov 2024 13:49:30 -0500 Subject: [PATCH] Move createBounds to measure.ts (#290) * nit * Move createBounds to measure * Export createBounds --- src/LGraphCanvas.ts | 2 +- src/LGraphGroup.ts | 25 ++----------------------- src/litegraph.ts | 1 + src/measure.ts | 26 +++++++++++++++++++++++--- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index d832eb613..9c0b79aab 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -7885,7 +7885,7 @@ export class LGraphCanvas { } targetX = -bounds[0] - bounds[2] * 0.5 + (cw * 0.5) / targetScale targetY = -bounds[1] - bounds[3] * 0.5 + (ch * 0.5) / targetScale - + const animate = (timestamp: number) => { const elapsed = timestamp - startTimestamp const progress = Math.min(elapsed / duration, 1) diff --git a/src/LGraphGroup.ts b/src/LGraphGroup.ts index 1406508d9..28477feec 100644 --- a/src/LGraphGroup.ts +++ b/src/LGraphGroup.ts @@ -3,7 +3,7 @@ import type { LGraph } from "./LGraph" import type { ISerialisedGroup } from "./types/serialisation" import { LiteGraph } from "./litegraph" import { LGraphCanvas } from "./LGraphCanvas" -import { isInsideRectangle, containsCentre, containsRect } from "./measure" +import { isInsideRectangle, containsCentre, containsRect, createBounds } from "./measure" import { LGraphNode } from "./LGraphNode" import { RenderShape, TitleMode } from "./types/globalEnums" @@ -229,7 +229,7 @@ export class LGraphGroup implements Positionable, IPinnable { * @param padding Value in graph units to add to all sides of the group. Default: 10 */ resizeTo(objects: Iterable, padding: number = 10): void { - const boundingBox = LGraphGroup.createBounds(objects, padding); + const boundingBox = createBounds(objects, padding); if(boundingBox === null) return this.pos[0] = boundingBox[0] @@ -238,27 +238,6 @@ export class LGraphGroup implements Positionable, IPinnable { this.size[1] = boundingBox[3] + this.titleHeight } - static createBounds(objects: Iterable, padding: number = 10): ReadOnlyRect | null - { - const bounds = new Float32Array([Infinity, Infinity, -Infinity, -Infinity]) - - for (const obj of objects) { - const rect = obj.boundingRect - bounds[0] = Math.min(bounds[0], rect[0]) - bounds[1] = Math.min(bounds[1], rect[1]) - bounds[2] = Math.max(bounds[2], rect[0] + rect[2]) - bounds[3] = Math.max(bounds[3], rect[1] + rect[3]) - } - if (!bounds.every(x => isFinite(x))) return null - - return [ - bounds[0] - padding, - bounds[1] - padding, - bounds[2] - bounds[0] + (2 * padding), - bounds[3] - bounds[1] + (2 * padding) - ] - } - /** * Add nodes to the group and adjust the group's position and size accordingly * @param {LGraphNode[]} nodes - The nodes to add to the group diff --git a/src/litegraph.ts b/src/litegraph.ts index 6a66d5d70..36bf75dc7 100644 --- a/src/litegraph.ts +++ b/src/litegraph.ts @@ -25,6 +25,7 @@ export { LGraphBadge, BadgePosition } export { SlotShape, LabelPosition, SlotDirection, SlotType } export { EaseFunction } from "./types/globalEnums" export type { SerialisableGraph, SerialisableLLink } from "./types/serialisation" +export { createBounds } from "./measure" export function clamp(v: number, a: number, b: number): number { return a > v ? a : b < v ? b : v diff --git a/src/measure.ts b/src/measure.ts index a632c1300..bdefaf888 100644 --- a/src/measure.ts +++ b/src/measure.ts @@ -1,4 +1,4 @@ -import type { Point, ReadOnlyPoint, ReadOnlyRect } from "./interfaces" +import type { Point, Positionable, ReadOnlyPoint, ReadOnlyRect } from "./interfaces" import { LinkDirection } from "./types/globalEnums" /** @@ -162,7 +162,7 @@ export function addDirectionalOffset(amount: number, direction: LinkDirection, o /** * Rotates an offset in 90° increments. - * + * * Swaps/flips axis values of a 2D vector offset - effectively rotating {@link offset} by 90° * @param offset The zero-based offset to rotate * @param from Direction to rotate from @@ -238,7 +238,7 @@ export function getOrientation(lineStart: ReadOnlyPoint, lineEnd: ReadOnlyPoint, } /** - * + * * @param out The array to store the point in * @param a Start point * @param b End point @@ -264,3 +264,23 @@ export function findPointOnCurve( out[0] = (c1 * a[0]) + (c2 * controlA[0]) + (c3 * controlB[0]) + (c4 * b[0]) out[1] = (c1 * a[1]) + (c2 * controlA[1]) + (c3 * controlB[1]) + (c4 * b[1]) } + +export function createBounds(objects: Iterable, padding: number = 10): ReadOnlyRect | null { + const bounds = new Float32Array([Infinity, Infinity, -Infinity, -Infinity]) + + for (const obj of objects) { + const rect = obj.boundingRect + bounds[0] = Math.min(bounds[0], rect[0]) + bounds[1] = Math.min(bounds[1], rect[1]) + bounds[2] = Math.max(bounds[2], rect[0] + rect[2]) + bounds[3] = Math.max(bounds[3], rect[1] + rect[3]) + } + if (!bounds.every(x => isFinite(x))) return null + + return [ + bounds[0] - padding, + bounds[1] - padding, + bounds[2] - bounds[0] + (2 * padding), + bounds[3] - bounds[1] + (2 * padding) + ] +} \ No newline at end of file