@@ -30,6 +30,8 @@ export default function WebRTCVideo() {
30
30
const {
31
31
setClientSize : setVideoClientSize ,
32
32
setSize : setVideoSize ,
33
+ width : videoWidth ,
34
+ height : videoHeight ,
33
35
clientWidth : videoClientWidth ,
34
36
clientHeight : videoClientHeight ,
35
37
} = useVideoStore ( ) ;
@@ -102,20 +104,43 @@ export default function WebRTCVideo() {
102
104
const mouseMoveHandler = useCallback (
103
105
( e : MouseEvent ) => {
104
106
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
+ }
106
126
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 ) ;
110
130
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 ) ;
114
138
115
139
// Send mouse movement
140
+ const { buttons } = e ;
116
141
sendMouseMovement ( x , y , buttons ) ;
117
142
} ,
118
- [ sendMouseMovement , videoClientHeight , videoClientWidth ] ,
143
+ [ sendMouseMovement , videoClientHeight , videoClientWidth , videoWidth , videoHeight ] ,
119
144
) ;
120
145
121
146
const mouseWheelHandler = useCallback (
0 commit comments