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
@@ -161,20 +166,27 @@ export const useImageCrop = (nodeId: NodeId) => {
161166 imageOffsetY . value = 0
162167 }
163168
164- if ( naturalWidth . value > 0 && displayedWidth . value > 0 ) {
165- scaleFactor . value = displayedWidth . value / naturalWidth . value
166- } else {
169+ if ( naturalWidth . value <= 0 || displayedWidth . value <= 0 ) {
167170 scaleFactor . value = 1
171+ } else {
172+ scaleFactor . value = displayedWidth . value / naturalWidth . value
168173 }
169174 }
170175
171176 const getEffectiveScale = ( ) : number => {
172- if ( ! containerEl . value || naturalWidth . value === 0 ) return 1
177+ const container = containerEl . value
178+
179+ if ( ! container || naturalWidth . value <= 0 || displayedWidth . value <= 0 ) {
180+ return 1
181+ }
173182
174- const rect = containerEl . value . getBoundingClientRect ( )
183+ const rect = container . getBoundingClientRect ( )
184+ const clientWidth = container . clientWidth
185+
186+ if ( ! clientWidth || ! rect . width ) return 1
175187
176188 const renderedDisplayedWidth =
177- ( displayedWidth . value / containerEl . value . clientWidth ) * rect . width
189+ ( displayedWidth . value / clientWidth ) * rect . width
178190
179191 return renderedDisplayedWidth / naturalWidth . value
180192 }
@@ -335,22 +347,22 @@ export const useImageCrop = (nodeId: NodeId) => {
335347 const maxX = naturalWidth . value - cropWidth . value
336348 const maxY = naturalHeight . value - cropHeight . value
337349
338- const newX = Math . round (
350+ cropX . value = Math . round (
339351 Math . max ( 0 , Math . min ( maxX , dragStartCropX . value + deltaX ) )
340352 )
341- const newY = Math . round (
353+ cropY . value = Math . round (
342354 Math . max ( 0 , Math . min ( maxY , dragStartCropY . value + deltaY ) )
343355 )
344-
345- setWidgetValue ( 'x' , newX )
346- setWidgetValue ( 'y' , newY )
347356 }
348357
349358 const handleDragEnd = ( e : PointerEvent ) => {
350359 if ( ! isDragging . value ) return
351360
352361 isDragging . value = false
353362 releasePointer ( e )
363+
364+ setWidgetValue ( 'x' , cropX . value )
365+ setWidgetValue ( 'y' , cropY . value )
354366 }
355367
356368 const handleResizeStart = ( e : PointerEvent , direction : ResizeDirection ) => {
@@ -418,12 +430,12 @@ export const useImageCrop = (nodeId: NodeId) => {
418430 }
419431
420432 if ( affectsLeft || affectsRight ) {
421- setWidgetValue ( 'x' , Math . round ( newX ) )
422- setWidgetValue ( 'width' , Math . round ( newWidth ) )
433+ cropX . value = Math . round ( newX )
434+ cropWidth . value = Math . round ( newWidth )
423435 }
424436 if ( affectsTop || affectsBottom ) {
425- setWidgetValue ( 'y' , Math . round ( newY ) )
426- setWidgetValue ( 'height' , Math . round ( newHeight ) )
437+ cropY . value = Math . round ( newY )
438+ cropHeight . value = Math . round ( newHeight )
427439 }
428440 }
429441
@@ -433,6 +445,11 @@ export const useImageCrop = (nodeId: NodeId) => {
433445 isResizing . value = false
434446 resizeDirection . value = null
435447 releasePointer ( e )
448+
449+ setWidgetValue ( 'x' , cropX . value )
450+ setWidgetValue ( 'y' , cropY . value )
451+ setWidgetValue ( 'width' , cropWidth . value )
452+ setWidgetValue ( 'height' , cropHeight . value )
436453 }
437454
438455 const initialize = ( ) => {
@@ -443,21 +460,10 @@ export const useImageCrop = (nodeId: NodeId) => {
443460 updateImageUrl ( )
444461 registerWidgetChangeHandler ( )
445462 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- }
456463 }
457464
458465 const cleanup = ( ) => {
459466 unregisterWidgetChangeHandler ( )
460- resizeObserver ?. disconnect ( )
461467 }
462468
463469 watch (
0 commit comments