1+ import { useResizeObserver } from '@vueuse/core'
12import { computed , onMounted , onUnmounted , ref , watch } from 'vue'
23
34import type { LGraphNode , NodeId } from '@/lib/litegraph/src/LGraphNode'
@@ -68,7 +69,11 @@ export const useImageCrop = (nodeId: NodeId) => {
6869 const resizeStartCropWidth = ref ( 0 )
6970 const resizeStartCropHeight = ref ( 0 )
7071
71- let resizeObserver : ResizeObserver | null = null
72+ useResizeObserver ( containerEl , ( ) => {
73+ if ( imageEl . value && imageUrl . value ) {
74+ updateDisplayedDimensions ( )
75+ }
76+ } )
7277
7378 const getWidgetValue = ( name : string ) : number => {
7479 if ( ! node . value ) return 0
@@ -143,6 +148,11 @@ export const useImageCrop = (nodeId: NodeId) => {
143148 naturalWidth . value = img . naturalWidth
144149 naturalHeight . value = img . naturalHeight
145150
151+ if ( naturalWidth . value <= 0 || naturalHeight . value <= 0 ) {
152+ scaleFactor . value = 1
153+ return
154+ }
155+
146156 const containerWidth = container . clientWidth
147157 const containerHeight = container . clientHeight
148158
@@ -161,20 +171,27 @@ export const useImageCrop = (nodeId: NodeId) => {
161171 imageOffsetY . value = 0
162172 }
163173
164- if ( naturalWidth . value > 0 && displayedWidth . value > 0 ) {
165- scaleFactor . value = displayedWidth . value / naturalWidth . value
166- } else {
174+ if ( naturalWidth . value <= 0 || displayedWidth . value <= 0 ) {
167175 scaleFactor . value = 1
176+ } else {
177+ scaleFactor . value = displayedWidth . value / naturalWidth . value
168178 }
169179 }
170180
171181 const getEffectiveScale = ( ) : number => {
172- if ( ! containerEl . value || naturalWidth . value === 0 ) return 1
182+ const container = containerEl . value
173183
174- const rect = containerEl . value . getBoundingClientRect ( )
184+ if ( ! container || naturalWidth . value <= 0 || displayedWidth . value <= 0 ) {
185+ return 1
186+ }
187+
188+ const rect = container . getBoundingClientRect ( )
189+ const clientWidth = container . clientWidth
190+
191+ if ( ! clientWidth || ! rect . width ) return 1
175192
176193 const renderedDisplayedWidth =
177- ( displayedWidth . value / containerEl . value . clientWidth ) * rect . width
194+ ( displayedWidth . value / clientWidth ) * rect . width
178195
179196 return renderedDisplayedWidth / naturalWidth . value
180197 }
@@ -335,22 +352,22 @@ export const useImageCrop = (nodeId: NodeId) => {
335352 const maxX = naturalWidth . value - cropWidth . value
336353 const maxY = naturalHeight . value - cropHeight . value
337354
338- const newX = Math . round (
355+ cropX . value = Math . round (
339356 Math . max ( 0 , Math . min ( maxX , dragStartCropX . value + deltaX ) )
340357 )
341- const newY = Math . round (
358+ cropY . value = Math . round (
342359 Math . max ( 0 , Math . min ( maxY , dragStartCropY . value + deltaY ) )
343360 )
344-
345- setWidgetValue ( 'x' , newX )
346- setWidgetValue ( 'y' , newY )
347361 }
348362
349363 const handleDragEnd = ( e : PointerEvent ) => {
350364 if ( ! isDragging . value ) return
351365
352366 isDragging . value = false
353367 releasePointer ( e )
368+
369+ setWidgetValue ( 'x' , cropX . value )
370+ setWidgetValue ( 'y' , cropY . value )
354371 }
355372
356373 const handleResizeStart = ( e : PointerEvent , direction : ResizeDirection ) => {
@@ -418,12 +435,12 @@ export const useImageCrop = (nodeId: NodeId) => {
418435 }
419436
420437 if ( affectsLeft || affectsRight ) {
421- setWidgetValue ( 'x' , Math . round ( newX ) )
422- setWidgetValue ( 'width' , Math . round ( newWidth ) )
438+ cropX . value = Math . round ( newX )
439+ cropWidth . value = Math . round ( newWidth )
423440 }
424441 if ( affectsTop || affectsBottom ) {
425- setWidgetValue ( 'y' , Math . round ( newY ) )
426- setWidgetValue ( 'height' , Math . round ( newHeight ) )
442+ cropY . value = Math . round ( newY )
443+ cropHeight . value = Math . round ( newHeight )
427444 }
428445 }
429446
@@ -433,6 +450,11 @@ export const useImageCrop = (nodeId: NodeId) => {
433450 isResizing . value = false
434451 resizeDirection . value = null
435452 releasePointer ( e )
453+
454+ setWidgetValue ( 'x' , cropX . value )
455+ setWidgetValue ( 'y' , cropY . value )
456+ setWidgetValue ( 'width' , cropWidth . value )
457+ setWidgetValue ( 'height' , cropHeight . value )
436458 }
437459
438460 const initialize = ( ) => {
@@ -443,21 +465,10 @@ export const useImageCrop = (nodeId: NodeId) => {
443465 updateImageUrl ( )
444466 registerWidgetChangeHandler ( )
445467 syncCropFromWidgets ( )
446-
447- resizeObserver = new ResizeObserver ( ( ) => {
448- if ( imageEl . value && imageUrl . value ) {
449- updateDisplayedDimensions ( )
450- }
451- } )
452-
453- if ( containerEl . value ) {
454- resizeObserver . observe ( containerEl . value )
455- }
456468 }
457469
458470 const cleanup = ( ) => {
459471 unregisterWidgetChangeHandler ( )
460- resizeObserver ?. disconnect ( )
461472 }
462473
463474 watch (
0 commit comments