Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions lib/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,43 +163,51 @@ export const Provider: React.FC<ComponentProps> = (props) => {
}
}, [two, twoState, props.width, props.height]);

// Auto-update dimensions if fullscreen / fitted
// Auto-update dimensions when Two.js instance resizes
useEffect(() => {
const isRoot = !two;

if (isRoot) {
// Only update root instance
if (twoState) {
const instance = twoState;
let width = instance.width;
let height = instance.height;

if (props.fullscreen || props.fitted) {
instance.bind('update', update);
}
if (isRoot && twoState) {
const instance = twoState;

function update() {
const widthFlagged = instance.width !== width;
const heightFlagged = instance.height !== height;
// Handler for Two.js resize events
function handleResize() {
setWidth(instance.width);
setHeight(instance.height);
}

if (widthFlagged) {
width = instance.width;
}
if (heightFlagged) {
height = instance.height;
}
if (widthFlagged || heightFlagged) {
// Handler for resizing Two.js canvas based on fitted
function handleUpdate() {
const parent = instance.renderer.domElement.parentElement;
if (parent) {
const width = parent.offsetWidth;
const height = parent.offsetHeight;
if (instance.width !== width) {
setWidth(width);
}
if (instance.height !== height) {
setHeight(height);
}
}
}

if (props.fitted) {
// Catch renderer size change on fitted
instance.bind('update', handleUpdate);

return () => {
instance.unbind('update', handleUpdate);
};
} else {
// Bind to renderer's resize event (fired by setSize(), etc.)
instance.renderer.bind('resize', handleResize);

return () => {
instance.unbind('update', update);
instance.renderer.unbind('resize', handleResize);
};
}
}
}, [two, twoState, props.fullscreen, props.fitted]);
}, [two, twoState, props.fitted]);

// Validate children in development mode
useEffect(() => {
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react": "^4.3.3",
"@vitest/ui": "^3.2.4",
"baseline-browser-mapping": "^2.9.14",
"clsx": "^2.1.1",
"eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0",
Expand All @@ -82,4 +83,4 @@
"react-dom": ">=19",
"two.js": ">=v0.8.23"
}
}
}
Loading