Line data Source code
1 : import type {
2 : Size2D as Size,
3 : Size2D,
4 : Bounds as ViewportRect,
5 : Point2D
6 : } from '@comfyorg/shared-frontend-utils/geometry'
7 : /** Linearly maps `value` from [min, max] to [0, 1]; 0 when min equals max. */
8 0 : export function normalize(value: number, min: number, max: number): number {
9 0 : return max === min ? 0 : (value - min) / (max - min)
10 : }
11 :
12 0 : function denormalize(normalized: number, min: number, max: number): number {
13 0 : return min + normalized * (max - min)
14 : }
15 :
16 : export type LetterboxedViewport = Size2D & {
17 : offsetX: number
18 : offsetY: number
19 : }
20 : export type { Bounds as ViewportRect } from '@comfyorg/shared-frontend-utils/geometry'
21 :
22 1052 : export function computeLetterboxedViewport(
23 : container: Size,
24 : targetAspectRatio: number
25 : ): LetterboxedViewport {
26 : const containerAspectRatio = container.width / container.height
27 :
28 1052 : if (containerAspectRatio > targetAspectRatio) {
29 1052 : const height = container.height
30 1052 : const width = height * targetAspectRatio
31 1052 : return {
32 : offsetX: (container.width - width) / 2,
33 : offsetY: 0,
34 : width,
35 : height
36 : }
37 : }
38 :
39 0 : const width = container.width
40 0 : const height = width / targetAspectRatio
41 0 : return {
42 : offsetX: 0,
43 : offsetY: (container.height - height) / 2,
44 : width,
45 : height
46 : }
47 : }
48 :
49 : export type LetterboxNdc = Point2D & { inside: boolean }
50 :
51 : export type PointerNdcSource = (
52 : clientX: number,
53 : clientY: number
54 : ) => LetterboxNdc | null
55 :
56 0 : export function clientPointToLetterboxNdc(
57 : normalizedX: number,
58 : normalizedY: number,
59 : container: Size,
60 : targetAspectRatio: number | null
61 : ): LetterboxNdc | null {
62 0 : const toNdc = (localX: number, localY: number): LetterboxNdc => ({
63 : x: denormalize(localX, -1, 1),
64 : y: -denormalize(localY, -1, 1),
65 : inside: localX >= 0 && localX <= 1 && localY >= 0 && localY <= 1
66 : })
67 :
68 0 : if (targetAspectRatio === null) {
69 0 : return toNdc(normalizedX, normalizedY)
70 : }
71 0 : const { offsetX, offsetY, width, height } = computeLetterboxedViewport(
72 : container,
73 : targetAspectRatio
74 : )
75 0 : if (width <= 0 || height <= 0) return null
76 0 : return toNdc(
77 : normalize(normalizedX * container.width, offsetX, offsetX + width),
78 : normalize(normalizedY * container.height, offsetY, offsetY + height)
79 : )
80 : }
81 :
82 919 : export function computeLetterboxBars(
83 : container: Size,
84 : viewport: LetterboxedViewport
85 : ): ViewportRect[] {
86 919 : if (viewport.offsetX >= 1) {
87 919 : return [
88 : { x: 0, y: 0, width: viewport.offsetX, height: container.height },
89 : {
90 : x: viewport.offsetX + viewport.width,
91 : y: 0,
92 : width: container.width - viewport.offsetX - viewport.width,
93 : height: container.height
94 : }
95 : ]
96 : }
97 :
98 0 : if (viewport.offsetY >= 1) {
99 0 : return [
100 : { x: 0, y: 0, width: container.width, height: viewport.offsetY },
101 : {
102 : x: 0,
103 : y: viewport.offsetY + viewport.height,
104 : width: container.width,
105 : height: container.height - viewport.offsetY - viewport.height
106 : }
107 : ]
108 : }
109 :
110 0 : return []
111 : }
112 :
113 : export type Load3dActivityFlags = {
114 : mouseOnNode: boolean
115 : mouseOnScene: boolean
116 : mouseOnViewer: boolean
117 : recording: boolean
118 : initialRenderDone: boolean
119 : animationPlaying: boolean
120 : }
121 :
122 456 : export function isLoad3dActive(flags: Load3dActivityFlags): boolean {
123 456 : return (
124 : flags.mouseOnNode ||
125 : flags.mouseOnScene ||
126 : flags.mouseOnViewer ||
127 : flags.recording ||
128 : !flags.initialRenderDone ||
129 : flags.animationPlaying
130 : )
131 : }
|