Skip to content

Commit 8584b59

Browse files
committed
feat: change render method
1 parent 02aa439 commit 8584b59

3 files changed

Lines changed: 42 additions & 31 deletions

File tree

browser/src/App.tsx

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef, useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22
import { Alert, Result, Spin } from 'antd';
33
import clsx from 'clsx';
44
import { useAtomValue, useSetAtom, useAtom } from 'jotai';
@@ -29,17 +29,17 @@ const App = () => {
2929
const isKeyboardEnable = useAtomValue(isKeyboardEnableAtom);
3030
const setResolution = useSetAtom(resolutionAtom);
3131
const canvasRef = useRef<HTMLCanvasElement>(null);
32-
const requestRef = useRef<number>(0);
3332
const videoRef = useRef<HTMLVideoElement>(null);
3433

3534
const [isLoading, setIsLoading] = useState(true);
3635
const [isCameraAvailable, setIsCameraAvailable] = useState(false);
3736

38-
const renderFrame = useCallback(() => {
39-
const video = videoRef.current;
37+
const videoStyle = clsx('block select-none origin-center max-w-full max-h-full object-scale-down', mouseStyle)
38+
39+
const renderFrame = (frame: VideoFrame) => {
4040
const canvas = canvasRef.current;
4141

42-
if (!video || !canvas || video.paused || video.ended) return;
42+
if (!canvas) return;
4343

4444
const ctx = canvas.getContext('2d');
4545
if (!ctx) return;
@@ -51,17 +51,15 @@ const App = () => {
5151
ctx.rotate((videoRotate * Math.PI) / 180);
5252

5353
ctx.drawImage(
54-
video,
55-
-video.videoWidth / 2,
56-
-video.videoHeight / 2,
57-
video.videoWidth,
58-
video.videoHeight
54+
frame,
55+
-frame.displayWidth / 2,
56+
-frame.displayHeight / 2,
57+
frame.displayWidth,
58+
frame.displayHeight
5959
);
6060

6161
ctx.restore();
62-
63-
requestRef.current = requestAnimationFrame(renderFrame);
64-
}, [videoRotate]);
62+
};
6563

6664
const setCanvas = () => {
6765
const video = videoRef.current;
@@ -77,11 +75,20 @@ const App = () => {
7775
canvas.height = video.videoWidth;
7876
}
7977

80-
if (requestRef.current) {
81-
cancelAnimationFrame(requestRef.current);
78+
if (videoRotate !== 0) {
79+
processVideoFrames();
8280
}
81+
}
8382

84-
renderFrame();
83+
const processVideoFrames = () => {
84+
const video = videoRef.current;
85+
if (video == null || videoRotate === 0) return;
86+
video.requestVideoFrameCallback(() => {
87+
const frame = new VideoFrame(video);
88+
renderFrame(frame);
89+
frame.close();
90+
processVideoFrames();
91+
})
8592
}
8693

8794
useEffect(() => {
@@ -173,21 +180,23 @@ const App = () => {
173180

174181
<video
175182
id="video"
176-
className="hidden"
183+
className={clsx(videoRotate == 0 ? videoStyle : "hidden", "min-h-[480px] min-w-[640px]")}
177184
ref={videoRef}
178185
autoPlay
179186
playsInline
180187
onLoadedMetadata={setCanvas}
181188
/>
182189

183-
<canvas
184-
id="video-canvas"
185-
ref={canvasRef}
186-
className={clsx('block min-h-[480px] min-w-[640px] select-none origin-center max-w-full max-h-full object-scale-down', mouseStyle)}
187-
style={{
188-
transform: `scale(${videoScale})`,
189-
}}
190-
/>
190+
{videoRotate == 0 ||
191+
<canvas
192+
id="video-canvas"
193+
ref={canvasRef}
194+
className={videoStyle}
195+
style={{
196+
transform: `scale(${videoScale})`,
197+
}}
198+
/>
199+
}
191200

192201
<VirtualKeyboard isBigScreen={isBigScreen} />
193202
</>

browser/src/components/mouse/absolute.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useRef } from 'react';
22
import { useAtomValue, useSetAtom } from 'jotai';
33

4-
import { resolutionAtom } from '@/jotai/device.ts';
4+
import { resolutionAtom, videoRotateAtom } from '@/jotai/device.ts';
55
import {
66
mouseJigglerModeAtom,
77
mouseLastMoveTimeAtom,
@@ -13,6 +13,7 @@ import { Key } from '@/libs/device/mouse.ts';
1313

1414
export const Absolute = () => {
1515
const resolution = useAtomValue(resolutionAtom);
16+
const videoRotate = useAtomValue(videoRotateAtom);
1617
const scrollDirection = useAtomValue(scrollDirectionAtom);
1718
const scrollInterval = useAtomValue(scrollIntervalAtom);
1819
const mouseJigglerMode = useAtomValue(mouseJigglerModeAtom);
@@ -23,7 +24,7 @@ export const Absolute = () => {
2324

2425
// listen mouse events
2526
useEffect(() => {
26-
const canvas = document.getElementById('video-canvas');
27+
const canvas = document.getElementById(videoRotate == 0 ? 'video': 'video-canvas');
2728
if (!canvas) return;
2829

2930
canvas.addEventListener('mousedown', handleMouseDown);
@@ -157,7 +158,7 @@ export const Absolute = () => {
157158
canvas.removeEventListener('click', disableEvent);
158159
canvas.removeEventListener('contextmenu', disableEvent);
159160
};
160-
}, [resolution, scrollDirection, scrollInterval, mouseJigglerMode, setMouseLastMoveTime]);
161+
}, [resolution, scrollDirection, scrollInterval, mouseJigglerMode, setMouseLastMoveTime, videoRotate]);
161162

162163
// disable default events
163164
function disableEvent(event: any) {

browser/src/components/mouse/relative.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { message } from 'antd';
33
import { useAtomValue, useSetAtom } from 'jotai';
44
import { useTranslation } from 'react-i18next';
55

6-
import { resolutionAtom } from '@/jotai/device.ts';
6+
import { resolutionAtom, videoRotateAtom } from '@/jotai/device.ts';
77
import {
88
mouseJigglerModeAtom,
99
mouseLastMoveTimeAtom,
@@ -18,6 +18,7 @@ export const Relative = () => {
1818
const [messageApi, contextHolder] = message.useMessage();
1919

2020
const resolution = useAtomValue(resolutionAtom);
21+
const videoRotate = useAtomValue(videoRotateAtom);
2122
const scrollDirection = useAtomValue(scrollDirectionAtom);
2223
const scrollInterval = useAtomValue(scrollIntervalAtom);
2324
const mouseJigglerMode = useAtomValue(mouseJigglerModeAtom);
@@ -41,7 +42,7 @@ export const Relative = () => {
4142

4243
// listen mouse events
4344
useEffect(() => {
44-
const canvas = document.getElementById('video-canvas');
45+
const canvas = document.getElementById(videoRotate == 0 ? 'video': 'video-canvas');
4546
if (!canvas) return;
4647

4748
document.addEventListener('pointerlockchange', handlePointerLockChange);
@@ -150,7 +151,7 @@ export const Relative = () => {
150151
canvas.removeEventListener('wheel', handleWheel);
151152
canvas.removeEventListener('contextmenu', disableEvent);
152153
};
153-
}, [resolution, scrollDirection, scrollInterval, mouseJigglerMode, setMouseLastMoveTime]);
154+
}, [resolution, scrollDirection, scrollInterval, mouseJigglerMode, setMouseLastMoveTime, videoRotate]);
154155

155156
async function send(x: number, y: number, scroll: number) {
156157
await device.sendMouseRelativeData(keyRef.current, x, y, scroll);

0 commit comments

Comments
 (0)