Skip to content

Commit

Permalink
feat: add aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
enijar committed Mar 8, 2023
1 parent be11520 commit 127b030
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/config/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
49 changes: 49 additions & 0 deletions examples/examples/aspect-ratio.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<Example>
<Layer width={1} height={0.1} gap={0.05} position-y={0.6}>
<Button
width={0.1}
height={0.1}
fontSize={0.35}
textContent="16:9"
selected={aspectRatio === 16 / 9}
onClick={() => setAspectRatio(16 / 9)}
/>
<Button
width={0.1}
height={0.1}
fontSize={0.35}
textContent="1:1"
selected={aspectRatio === 1}
onClick={() => setAspectRatio(1)}
/>
<Button
width={0.1}
height={0.1}
fontSize={0.35}
textContent="2:4"
selected={aspectRatio === 2 / 4}
onClick={() => setAspectRatio(2 / 4)}
/>
</Layer>
<Layer
height={1}
aspectRatio={aspectRatio}
gap={0.1}
backgroundColor="crimson"
/>
</Example>
<ViewCode pathname="aspect-ratio" />
</>
);
}
17 changes: 16 additions & 1 deletion src/components/layer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Layer(
autoLayout = true,
width,
height,
aspectRatio,
opacity = 1,
backgroundColor = "transparent",
backgroundImage,
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type LayerProps = GroupProps & {
resolution?: number;
width?: number | string;
height?: number | string;
aspectRatio?: number;
opacity?: number;
backgroundColor?: string;
backgroundImage?: string;
Expand Down

0 comments on commit 127b030

Please sign in to comment.