@@ -30,6 +30,8 @@ export default function WebRTCVideo() {
3030 const {
3131 setClientSize : setVideoClientSize ,
3232 setSize : setVideoSize ,
33+ width : videoWidth ,
34+ height : videoHeight ,
3335 clientWidth : videoClientWidth ,
3436 clientHeight : videoClientHeight ,
3537 } = useVideoStore ( ) ;
@@ -102,20 +104,43 @@ export default function WebRTCVideo() {
102104 const mouseMoveHandler = useCallback (
103105 ( e : MouseEvent ) => {
104106 if ( ! videoClientWidth || ! videoClientHeight ) return ;
105- const { buttons } = e ;
107+ // Get the aspect ratios of the video element and the video stream
108+ const videoElementAspectRatio = videoClientWidth / videoClientHeight ;
109+ const videoStreamAspectRatio = videoWidth / videoHeight ;
110+
111+ // Calculate the effective video display area
112+ let effectiveWidth = videoClientWidth ;
113+ let effectiveHeight = videoClientHeight ;
114+ let offsetX = 0 ;
115+ let offsetY = 0 ;
116+
117+ if ( videoElementAspectRatio > videoStreamAspectRatio ) {
118+ // Pillarboxing: black bars on the left and right
119+ effectiveWidth = videoClientHeight * videoStreamAspectRatio ;
120+ offsetX = ( videoClientWidth - effectiveWidth ) / 2 ;
121+ } else if ( videoElementAspectRatio < videoStreamAspectRatio ) {
122+ // Letterboxing: black bars on the top and bottom
123+ effectiveHeight = videoClientWidth / videoStreamAspectRatio ;
124+ offsetY = ( videoClientHeight - effectiveHeight ) / 2 ;
125+ }
106126
107- // Clamp mouse position within the video boundaries
108- const currMouseX = Math . min ( Math . max ( 1 , e . offsetX ) , videoClientWidth ) ;
109- const currMouseY = Math . min ( Math . max ( 1 , e . offsetY ) , videoClientHeight ) ;
127+ // Clamp mouse position within the effective video boundaries
128+ const clampedX = Math . min ( Math . max ( offsetX , e . offsetX ) , offsetX + effectiveWidth ) ;
129+ const clampedY = Math . min ( Math . max ( offsetY , e . offsetY ) , offsetY + effectiveHeight ) ;
110130
111- // Normalize mouse position to 0-32767 range (HID absolute coordinate system)
112- const x = Math . round ( ( currMouseX / videoClientWidth ) * 32767 ) ;
113- const y = Math . round ( ( currMouseY / videoClientHeight ) * 32767 ) ;
131+ // Map clamped mouse position to the video stream's coordinate system
132+ const relativeX = ( clampedX - offsetX ) / effectiveWidth ;
133+ const relativeY = ( clampedY - offsetY ) / effectiveHeight ;
134+
135+ // Convert to HID absolute coordinate system (0-32767 range)
136+ const x = Math . round ( relativeX * 32767 ) ;
137+ const y = Math . round ( relativeY * 32767 ) ;
114138
115139 // Send mouse movement
140+ const { buttons } = e ;
116141 sendMouseMovement ( x , y , buttons ) ;
117142 } ,
118- [ sendMouseMovement , videoClientHeight , videoClientWidth ] ,
143+ [ sendMouseMovement , videoClientHeight , videoClientWidth , videoWidth , videoHeight ] ,
119144 ) ;
120145
121146 const mouseWheelHandler = useCallback (
0 commit comments