LCOV - code coverage report
Current view: top level - packages/mask-editor/src - usePanAndZoom.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 45.5 % 178 81
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { ref, watch } from 'vue'
       2              : 
       3              : import type {
       4              :   Point2D as Offset,
       5              :   Point2D as Point
       6              : } from '@comfyorg/shared-frontend-utils/geometry'
       7              : import { useMaskEditorStore } from './maskEditorStore'
       8              : 
       9              : import {
      10              :   calculateDragPan,
      11              :   calculateFitView,
      12              :   calculatePanZoomStyles,
      13              :   calculateSingleTouchPan,
      14              :   calculateZoomAroundPoint,
      15              :   easeOutCubic,
      16              :   getCursorPoint,
      17              :   getDistanceBetweenPoints,
      18              :   getMidpoint,
      19              :   getWheelZoomFactor,
      20              :   interpolateView,
      21              :   isDoubleTap
      22              : } from './panZoomUtils'
      23              : 
      24           29 : export function usePanAndZoom() {
      25           29 :   const store = useMaskEditorStore()
      26              : 
      27           29 :   const DOUBLE_TAP_DELAY = 300
      28              : 
      29           29 :   const lastTwoFingerTap = ref(0)
      30           29 :   const isTouchZooming = ref(false)
      31           29 :   const lastTouchZoomDistance = ref(0)
      32           29 :   const lastTouchMidPoint = ref<Point>({ x: 0, y: 0 })
      33           29 :   const lastTouchPoint = ref<Point>({ x: 0, y: 0 })
      34              : 
      35           29 :   const zoom_ratio = ref(1)
      36           29 :   const interpolatedZoomRatio = ref(1)
      37           29 :   const pan_offset = ref<Offset>({ x: 0, y: 0 })
      38              : 
      39           29 :   const mouseDownPoint = ref<Point | null>(null)
      40           29 :   const initialPan = ref<Offset>({ x: 0, y: 0 })
      41              : 
      42           29 :   const canvasContainer = ref<HTMLElement | null>(null)
      43           29 :   const maskCanvas = ref<HTMLCanvasElement | null>(null)
      44           29 :   const rgbCanvas = ref<HTMLCanvasElement | null>(null)
      45           29 :   const rootElement = ref<HTMLElement | null>(null)
      46              : 
      47           29 :   const toolPanelElement = ref<HTMLElement | null>(null)
      48           29 :   const sidePanelElement = ref<HTMLElement | null>(null)
      49              : 
      50           29 :   const image = ref<HTMLImageElement | null>(null)
      51           29 :   const imageRootWidth = ref(0)
      52           29 :   const imageRootHeight = ref(0)
      53              : 
      54           29 :   const cursorPoint = ref<Point>({ x: 0, y: 0 })
      55           29 :   const penPointerIdList = ref<number[]>([])
      56              : 
      57           29 :   const updateCursorPosition = (clientPoint: Point): void => {
      58          230 :     const point = getCursorPoint(clientPoint, pan_offset.value)
      59          230 :     cursorPoint.value = point
      60          230 :     store.setCursorPoint(point)
      61              :   }
      62              : 
      63           29 :   const handleDoubleTap = (): void => {
      64            0 :     store.canvasHistory.undo()
      65              :   }
      66              : 
      67           29 :   const invalidatePanZoom = async (): Promise<void> => {
      68           70 :     const currentImage = image.value
      69           70 :     if (!currentImage?.width || !currentImage.height || !zoom_ratio.value) {
      70            0 :       console.warn('Missing required properties for pan/zoom')
      71            0 :       return
      72              :     }
      73              : 
      74           70 :     const styles = calculatePanZoomStyles(
      75              :       currentImage.width,
      76              :       currentImage.height,
      77              :       zoom_ratio.value,
      78              :       pan_offset.value
      79              :     )
      80              : 
      81           70 :     if (!canvasContainer.value) {
      82           29 :       canvasContainer.value = store.canvasContainer
      83              :     }
      84           70 :     if (!canvasContainer.value) return
      85              : 
      86           70 :     Object.assign(canvasContainer.value.style, {
      87              :       width: styles.containerWidth,
      88              :       height: styles.containerHeight,
      89              :       left: styles.containerLeft,
      90              :       top: styles.containerTop
      91              :     })
      92              : 
      93           70 :     if (!rgbCanvas.value) {
      94           29 :       rgbCanvas.value = store.rgbCanvas
      95              :     }
      96           70 :     if (rgbCanvas.value) {
      97           70 :       if (
      98              :         rgbCanvas.value.width !== currentImage.width ||
      99              :         rgbCanvas.value.height !== currentImage.height
     100              :       ) {
     101            0 :         rgbCanvas.value.width = currentImage.width
     102            0 :         rgbCanvas.value.height = currentImage.height
     103              :       }
     104              : 
     105           70 :       rgbCanvas.value.style.width = styles.containerWidth
     106           70 :       rgbCanvas.value.style.height = styles.containerHeight
     107              :     }
     108              : 
     109           70 :     store.setPanOffset(pan_offset.value)
     110           70 :     store.setZoomRatio(zoom_ratio.value)
     111              :   }
     112              : 
     113           29 :   const handlePanStart = (event: PointerEvent): void => {
     114            1 :     mouseDownPoint.value = { x: event.clientX, y: event.clientY }
     115              : 
     116            1 :     store.isPanning = true
     117            1 :     initialPan.value = { ...pan_offset.value }
     118              :   }
     119              : 
     120           29 :   const handlePanMove = async (event: PointerEvent): Promise<void> => {
     121           10 :     if (mouseDownPoint.value === null) {
     122            0 :       console.error('mouseDownPoint is null')
     123            0 :       return
     124              :     }
     125              : 
     126           10 :     pan_offset.value = calculateDragPan(
     127              :       mouseDownPoint.value,
     128              :       { x: event.clientX, y: event.clientY },
     129              :       initialPan.value
     130              :     )
     131              : 
     132           10 :     await invalidatePanZoom()
     133              :   }
     134              : 
     135           29 :   const handleSingleTouchPan = async (touch: Touch): Promise<void> => {
     136            0 :     pan_offset.value = calculateSingleTouchPan(
     137              :       lastTouchPoint.value,
     138              :       { x: touch.clientX, y: touch.clientY },
     139              :       pan_offset.value
     140              :     )
     141              : 
     142            0 :     await invalidatePanZoom()
     143              : 
     144            0 :     lastTouchPoint.value = { x: touch.clientX, y: touch.clientY }
     145              :   }
     146              : 
     147           29 :   const touchToPoint = (touch: Touch): Point => ({
     148              :     x: touch.clientX,
     149              :     y: touch.clientY
     150              :   })
     151              : 
     152           29 :   const handleTouchStart = (event: TouchEvent): void => {
     153            0 :     event.preventDefault()
     154              : 
     155            0 :     if (penPointerIdList.value.length > 0) return
     156              : 
     157            0 :     store.brushVisible = false
     158              : 
     159            0 :     if (event.touches.length === 2) {
     160            0 :       const currentTime = new Date().getTime()
     161              : 
     162            0 :       if (isDoubleTap(currentTime, lastTwoFingerTap.value, DOUBLE_TAP_DELAY)) {
     163            0 :         handleDoubleTap()
     164            0 :         lastTwoFingerTap.value = 0
     165              :       } else {
     166            0 :         lastTwoFingerTap.value = currentTime
     167              : 
     168            0 :         const p0 = touchToPoint(event.touches[0])
     169            0 :         const p1 = touchToPoint(event.touches[1])
     170              : 
     171            0 :         isTouchZooming.value = true
     172            0 :         lastTouchZoomDistance.value = getDistanceBetweenPoints(p0, p1)
     173            0 :         lastTouchMidPoint.value = getMidpoint(p0, p1)
     174              :       }
     175            0 :     } else if (event.touches.length === 1) {
     176            0 :       lastTouchPoint.value = touchToPoint(event.touches[0])
     177              :     }
     178              :   }
     179              : 
     180           29 :   const handleTouchMove = async (event: TouchEvent): Promise<void> => {
     181            0 :     event.preventDefault()
     182              : 
     183            0 :     if (penPointerIdList.value.length > 0) return
     184              : 
     185            0 :     lastTwoFingerTap.value = 0
     186              : 
     187            0 :     if (isTouchZooming.value && event.touches.length === 2) {
     188            0 :       const p0 = touchToPoint(event.touches[0])
     189            0 :       const p1 = touchToPoint(event.touches[1])
     190              : 
     191            0 :       const newDistance = getDistanceBetweenPoints(p0, p1)
     192            0 :       const pinchFactor = newDistance / lastTouchZoomDistance.value
     193            0 :       const midpoint = getMidpoint(p0, p1)
     194              : 
     195              :       // Apply midpoint drag
     196            0 :       const draggedPan: Offset = {
     197              :         x: pan_offset.value.x + midpoint.x - lastTouchMidPoint.value.x,
     198              :         y: pan_offset.value.y + midpoint.y - lastTouchMidPoint.value.y
     199              :       }
     200              : 
     201            0 :       if (!maskCanvas.value) {
     202            0 :         maskCanvas.value = store.maskCanvas
     203              :       }
     204            0 :       if (!maskCanvas.value) return
     205              : 
     206            0 :       const rect = maskCanvas.value.getBoundingClientRect()
     207            0 :       const focalX = midpoint.x - rect.left
     208            0 :       const focalY = midpoint.y - rect.top
     209              : 
     210            0 :       const result = calculateZoomAroundPoint(
     211              :         zoom_ratio.value,
     212              :         pinchFactor,
     213              :         draggedPan,
     214              :         focalX,
     215              :         focalY
     216              :       )
     217              : 
     218            0 :       zoom_ratio.value = result.zoomRatio
     219            0 :       pan_offset.value = result.panOffset
     220              : 
     221            0 :       await invalidatePanZoom()
     222            0 :       lastTouchZoomDistance.value = newDistance
     223            0 :       lastTouchMidPoint.value = midpoint
     224            0 :     } else if (event.touches.length === 1) {
     225            0 :       await handleSingleTouchPan(event.touches[0])
     226              :     }
     227              :   }
     228              : 
     229           29 :   const handleTouchEnd = (event: TouchEvent): void => {
     230            0 :     event.preventDefault()
     231              : 
     232            0 :     if (event.touches.length > 0) {
     233            0 :       lastTouchPoint.value = touchToPoint(event.touches[0])
     234              :     } else {
     235            0 :       isTouchZooming.value = false
     236            0 :       lastTouchMidPoint.value = { x: 0, y: 0 }
     237              :     }
     238              :   }
     239              : 
     240           29 :   const zoom = async (event: WheelEvent): Promise<void> => {
     241            0 :     const cursorPosition = { x: event.clientX, y: event.clientY }
     242              : 
     243            0 :     if (!maskCanvas.value) {
     244            0 :       maskCanvas.value = store.maskCanvas
     245              :     }
     246            0 :     if (!maskCanvas.value) return
     247              : 
     248            0 :     const rect = maskCanvas.value.getBoundingClientRect()
     249            0 :     const focalX = cursorPosition.x - rect.left
     250            0 :     const focalY = cursorPosition.y - rect.top
     251              : 
     252            0 :     const result = calculateZoomAroundPoint(
     253              :       zoom_ratio.value,
     254              :       getWheelZoomFactor(event.deltaY),
     255              :       pan_offset.value,
     256              :       focalX,
     257              :       focalY
     258              :     )
     259              : 
     260            0 :     zoom_ratio.value = result.zoomRatio
     261            0 :     pan_offset.value = result.panOffset
     262              : 
     263            0 :     await invalidatePanZoom()
     264              : 
     265              :     const newImageWidth = maskCanvas.value.clientWidth
     266            0 :     const zoomRatio = newImageWidth / imageRootWidth.value
     267              : 
     268            0 :     interpolatedZoomRatio.value = zoomRatio
     269            0 :     store.displayZoomRatio = zoomRatio
     270              : 
     271            0 :     updateCursorPosition(cursorPosition)
     272              :   }
     273              : 
     274           29 :   const getPanelDimensions = (): {
     275              :     sidePanelWidth: number
     276              :     toolPanelWidth: number
     277              :   } => {
     278           29 :     const toolPanelWidth =
     279              :       toolPanelElement.value?.getBoundingClientRect().width || 64
     280              :     const sidePanelWidth =
     281              :       sidePanelElement.value?.getBoundingClientRect().width || 220
     282              : 
     283           29 :     return { sidePanelWidth, toolPanelWidth }
     284              :   }
     285              : 
     286           29 :   const smoothResetView = async (duration: number = 500): Promise<void> => {
     287            0 :     if (!image.value || !rootElement.value) return
     288              : 
     289            0 :     const startZoom = zoom_ratio.value
     290            0 :     const startPan = { ...pan_offset.value }
     291              : 
     292            0 :     const { sidePanelWidth, toolPanelWidth } = getPanelDimensions()
     293              : 
     294            0 :     const fitResult = calculateFitView({
     295              :       rootWidth: rootElement.value.clientWidth,
     296              :       rootHeight: rootElement.value.clientHeight,
     297              :       imageWidth: image.value.width,
     298              :       imageHeight: image.value.height,
     299              :       toolPanelWidth,
     300              :       sidePanelWidth
     301              :     })
     302              : 
     303            0 :     const startTime = performance.now()
     304            0 :     const animate = async (currentTime: number) => {
     305            0 :       const elapsed = currentTime - startTime
     306            0 :       const progress = Math.min(elapsed / duration, 1)
     307            0 :       const eased = easeOutCubic(progress)
     308              : 
     309            0 :       const frame = interpolateView(
     310              :         startZoom,
     311              :         fitResult.zoomRatio,
     312              :         startPan,
     313              :         fitResult.panOffset,
     314              :         eased
     315              :       )
     316              : 
     317            0 :       zoom_ratio.value = frame.zoomRatio
     318            0 :       pan_offset.value = frame.panOffset
     319              : 
     320            0 :       await invalidatePanZoom()
     321              : 
     322            0 :       const interpolatedRatio = startZoom + (1.0 - startZoom) * eased
     323            0 :       store.displayZoomRatio = interpolatedRatio
     324              : 
     325            0 :       if (progress < 1) {
     326            0 :         requestAnimationFrame(animate)
     327              :       }
     328              :     }
     329              : 
     330            0 :     requestAnimationFrame(animate)
     331            0 :     interpolatedZoomRatio.value = 1.0
     332              :   }
     333              : 
     334           29 :   const initializeCanvasPanZoom = async (
     335              :     img: HTMLImageElement,
     336              :     root: HTMLElement,
     337              :     toolPanel?: HTMLElement | null,
     338              :     sidePanel?: HTMLElement | null
     339              :   ): Promise<void> => {
     340           29 :     rootElement.value = root
     341           29 :     toolPanelElement.value = toolPanel || null
     342           29 :     sidePanelElement.value = sidePanel || null
     343              : 
     344           29 :     const { sidePanelWidth, toolPanelWidth } = getPanelDimensions()
     345              : 
     346           29 :     const fitResult = calculateFitView({
     347              :       rootWidth: root.clientWidth,
     348              :       rootHeight: root.clientHeight,
     349              :       imageWidth: img.width,
     350              :       imageHeight: img.height,
     351              :       toolPanelWidth,
     352              :       sidePanelWidth
     353              :     })
     354              : 
     355           29 :     if (image.value === null) {
     356           29 :       image.value = img
     357              :     }
     358              : 
     359           29 :     imageRootWidth.value = fitResult.fittedWidth
     360           29 :     imageRootHeight.value = fitResult.fittedHeight
     361              : 
     362           29 :     zoom_ratio.value = fitResult.zoomRatio
     363           29 :     pan_offset.value = fitResult.panOffset
     364              : 
     365           29 :     penPointerIdList.value = []
     366              : 
     367           29 :     await invalidatePanZoom()
     368              :   }
     369              : 
     370           29 :   watch(
     371              :     () => store.resetZoomTrigger,
     372              :     async () => {
     373            0 :       if (interpolatedZoomRatio.value === 1) return
     374            0 :       await smoothResetView()
     375              :     }
     376              :   )
     377              : 
     378           29 :   const addPenPointerId = (pointerId: number): void => {
     379            0 :     if (!penPointerIdList.value.includes(pointerId)) {
     380            0 :       penPointerIdList.value.push(pointerId)
     381              :     }
     382              :   }
     383              : 
     384           29 :   const removePenPointerId = (pointerId: number): void => {
     385            0 :     const index = penPointerIdList.value.indexOf(pointerId)
     386            0 :     if (index !== -1) {
     387            0 :       penPointerIdList.value.splice(index, 1)
     388              :     }
     389              :   }
     390              : 
     391           29 :   return {
     392              :     initializeCanvasPanZoom,
     393              :     handlePanStart,
     394              :     handlePanMove,
     395              :     handleTouchStart,
     396              :     handleTouchMove,
     397              :     handleTouchEnd,
     398              :     updateCursorPosition,
     399              :     zoom,
     400              :     invalidatePanZoom,
     401              :     addPenPointerId,
     402              :     removePenPointerId
     403              :   }
     404              : }
        

Generated by: LCOV version 2.0-1