-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #513 from Jameskmonger/fix-gameboard-scaling
fix: scale gameboard correctly with aspect ratio
- Loading branch information
Showing
6 changed files
with
197 additions
and
63 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
modules/@cc-web/ui/gameBoard/DynamicAspectRatioComponent.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import React from "react"; | ||
|
||
import { Meta, Story } from "@storybook/react"; | ||
|
||
import { GRID_SIZE } from "@creature-chess/models"; | ||
|
||
import { DynamicAspectRatioComponent } from "./DynamicAspectRatioComponent"; | ||
|
||
export default { | ||
title: "@ui / DynamicAspectRatioComponent", | ||
component: DynamicAspectRatioComponent, | ||
} as Meta; | ||
|
||
const Template: Story<any> = (args) => ( | ||
<div | ||
style={{ | ||
width: args.width + "px", | ||
height: args.height + "px", | ||
border: "2px solid red", | ||
}} | ||
> | ||
<DynamicAspectRatioComponent aspectRatio={args.aspectRatio}> | ||
<div style={{ width: "100%", height: "100%", background: "lightblue" }}> | ||
wawa | ||
</div> | ||
</DynamicAspectRatioComponent> | ||
</div> | ||
); | ||
|
||
export const Portrait_FullBoard = Template.bind({}); | ||
Portrait_FullBoard.args = { | ||
width: 300, | ||
height: 500, | ||
boardHeight: GRID_SIZE.height, | ||
aspectRatio: 7 / 7.2, | ||
}; | ||
|
||
export const Portrait_HalfBoard = Template.bind({}); | ||
Portrait_HalfBoard.args = { | ||
width: 300, | ||
height: 500, | ||
boardHeight: GRID_SIZE.height, | ||
aspectRatio: 7 / 4.2, | ||
}; | ||
|
||
export const Landscape_FullBoard = Template.bind({}); | ||
Landscape_FullBoard.args = { | ||
width: 500, | ||
height: 300, | ||
boardHeight: GRID_SIZE.height, | ||
aspectRatio: 7 / 7.2, | ||
}; | ||
|
||
export const Landscape_HalfBoard = Template.bind({}); | ||
Landscape_HalfBoard.args = { | ||
width: 500, | ||
height: 300, | ||
boardHeight: GRID_SIZE.height, | ||
aspectRatio: 7 / 4.2, | ||
}; | ||
|
||
export const Square_FullBoard = Template.bind({}); | ||
Square_FullBoard.args = { | ||
width: 400, | ||
height: 400, | ||
boardHeight: GRID_SIZE.height, | ||
aspectRatio: 7 / 7.2, | ||
}; | ||
|
||
export const Square_HalfBoard = Template.bind({}); | ||
Square_HalfBoard.args = { | ||
width: 400, | ||
height: 400, | ||
boardHeight: GRID_SIZE.height, | ||
aspectRatio: 7 / 4.2, | ||
}; |
75 changes: 75 additions & 0 deletions
75
modules/@cc-web/ui/gameBoard/DynamicAspectRatioComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { useRef, useEffect, useCallback } from "react"; | ||
|
||
/** | ||
* This component will adjust its size to maintain the given aspect ratio, | ||
* while filling its parent container as much as possible. | ||
* | ||
* @param aspectRatio The aspect ratio to maintain | ||
*/ | ||
export function DynamicAspectRatioComponent({ | ||
aspectRatio, | ||
children, | ||
}: { | ||
aspectRatio: number; | ||
children: React.ReactNode; | ||
}) { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const componentRef = useRef<HTMLDivElement>(null); | ||
|
||
const adjustComponentSize = useCallback(() => { | ||
const container = containerRef.current; | ||
const component = componentRef.current; | ||
|
||
if (container && component) { | ||
const containerWidth = container.clientWidth; | ||
const containerHeight = container.clientHeight; | ||
const containerAspectRatio = containerWidth / containerHeight; | ||
|
||
if (aspectRatio > containerAspectRatio) { | ||
// Width bound | ||
component.style.width = "100%"; | ||
component.style.height = `${containerWidth / aspectRatio}px`; | ||
} else { | ||
// Height bound | ||
component.style.height = "100%"; | ||
component.style.width = `${containerHeight * aspectRatio}px`; | ||
} | ||
} | ||
}, [aspectRatio]); | ||
|
||
useEffect(() => { | ||
// Initial adjustment | ||
adjustComponentSize(); | ||
|
||
// Listen to window resize event for edge cases (like window resizing) | ||
window.addEventListener("resize", adjustComponentSize); | ||
|
||
// Observe size changes in the container | ||
const resizeObserver = new ResizeObserver(() => { | ||
adjustComponentSize(); | ||
}); | ||
|
||
if (containerRef.current) { | ||
resizeObserver.observe(containerRef.current); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener("resize", adjustComponentSize); | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [adjustComponentSize]); | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
style={{ width: "100%", height: "100%", position: "relative" }} | ||
> | ||
<div | ||
ref={componentRef} | ||
// style={{ position: "absolute", background: "lightblue" }} | ||
> | ||
{children} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { useOnClickOutside } from "./useOnClickOutside"; | ||
export { useElementSize } from "./useElementSize"; |
This file was deleted.
Oops, something went wrong.