Skip to content

Commit

Permalink
fix: handle player resize correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
justusmattern27 committed Sep 23, 2024
1 parent 59bc0d0 commit e183daa
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/2d/src/lib/scenes/Scene2D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ export class Scene2D extends GeneratorScene<View2D> implements Inspectable {

for (const media of mediaNodes) {
media.setVolume(media.getVolume() * volumeScale);
console.log(media.key);
}
}

Expand Down
17 changes: 9 additions & 8 deletions packages/player-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface RevideoPlayerProps {
height?: number;
quality?: number;
fps?: number;
volume?: number;
}

declare global {
Expand Down Expand Up @@ -140,19 +141,18 @@ export function Player({
const handlePlayerResize = useCallback(
(entries: ResizeObserverEntry[]) => {
const [firstEntry] = entries;
if (!firstEntry || !wrapperRef.current) {
if (!firstEntry || !wrapperRef.current || !lastRect.current) {
return;
}

const newRect = firstEntry.contentRect;
const sameWidth = newRect.width === lastRect.current.width;
const sameHeight = newRect.height === lastRect.current.height;
if (lastRect.current && sameWidth && sameHeight) {
return;
if (
newRect.width !== lastRect.current.width ||
newRect.height !== lastRect.current.height
) {
lastRect.current = newRect;
onPlayerResize(newRect);
}

lastRect.current = newRect;
onPlayerResize(newRect);
},
[onPlayerResize],
);
Expand Down Expand Up @@ -234,6 +234,7 @@ export function Player({
height={height}
quality={quality}
fps={fps}
volume={volumeState}
/>
<div
className={`p-absolute p-bottom-0 p-w-full p-transition-opacity p-duration-200 ${
Expand Down
7 changes: 5 additions & 2 deletions packages/player-react/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class RevideoPlayer extends HTMLElement {
'quality',
'width',
'height',
'volume',
];
}

Expand Down Expand Up @@ -99,7 +100,7 @@ class RevideoPlayer extends HTMLElement {
private duration: number = 0; // in frames
private looping = true;
private volume = 1;
private volumeChangeRequested = false;
private volumeChangeRequested = true;

public constructor() {
super();
Expand All @@ -110,7 +111,6 @@ class RevideoPlayer extends HTMLElement {
this.canvas = this.stage.finalBuffer;
this.canvas.classList.add('canvas');
this.root.prepend(this.canvas);

this.setState(State.Initial);
}

Expand Down Expand Up @@ -205,6 +205,9 @@ class RevideoPlayer extends HTMLElement {
case 'height':
this.updateSettings();
break;
case 'volume':
this.volume = newValue;
this.volumeChangeRequested = true;
}
}

Expand Down

0 comments on commit e183daa

Please sign in to comment.