diff --git a/examples/config/examples.ts b/examples/config/examples.ts
index a843085..4d4ff81 100644
--- a/examples/config/examples.ts
+++ b/examples/config/examples.ts
@@ -57,6 +57,11 @@ const examples: Example[] = [
pathname: "interactions",
component: React.lazy(() => import("@/examples/interactions")),
},
+ {
+ title: "Aspect Ratio",
+ pathname: "aspect-ratio",
+ component: React.lazy(() => import("@/examples/aspect-ratio")),
+ },
];
export default examples;
diff --git a/examples/examples/aspect-ratio.tsx b/examples/examples/aspect-ratio.tsx
new file mode 100644
index 0000000..01d1468
--- /dev/null
+++ b/examples/examples/aspect-ratio.tsx
@@ -0,0 +1,49 @@
+import React from "react";
+import Example from "@/components/example";
+import { Layer } from "react-xr-ui";
+import ViewCode from "@/components/view-code";
+import Button from "@/components/button";
+
+export default function RelativeSizing() {
+ const [aspectRatio, setAspectRatio] = React.useState(16 / 9);
+
+ return (
+ <>
+
+
+
+
+
+
+ >
+ );
+}
diff --git a/src/components/layer.tsx b/src/components/layer.tsx
index 8b6201f..fccc113 100644
--- a/src/components/layer.tsx
+++ b/src/components/layer.tsx
@@ -33,6 +33,7 @@ function Layer(
autoLayout = true,
width,
height,
+ aspectRatio,
opacity = 1,
backgroundColor = "transparent",
backgroundImage,
@@ -140,8 +141,22 @@ function Layer(
} else {
size.height = height ?? 1;
}
+ if (aspectRatio !== undefined) {
+ if (width === undefined) {
+ size.width = size.height * aspectRatio;
+ }
+ if (height === undefined) {
+ size.height = size.width * aspectRatio;
+ }
+ }
return size;
- }, [width, height, layerContext.parentUuid, layerContext.parentSize]);
+ }, [
+ width,
+ height,
+ layerContext.parentUuid,
+ layerContext.parentSize,
+ aspectRatio,
+ ]);
const uuid = React.useMemo(() => {
return THREE.MathUtils.generateUUID();
diff --git a/src/types.ts b/src/types.ts
index b612b34..d926540 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -39,6 +39,7 @@ export type LayerProps = GroupProps & {
resolution?: number;
width?: number | string;
height?: number | string;
+ aspectRatio?: number;
opacity?: number;
backgroundColor?: string;
backgroundImage?: string;