Move createBounds to measure.ts (#290)

* nit

* Move createBounds to measure

* Export createBounds
This commit is contained in:
Chenlei Hu
2024-11-08 13:49:30 -05:00
committed by GitHub
parent 6ad864bc20
commit c7d5c863ec
4 changed files with 27 additions and 27 deletions

View File

@@ -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)

View File

@@ -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<Positionable>, 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<Positionable>, 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

View File

@@ -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

View File

@@ -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<Positionable>, 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)
]
}