sendou.ink/app/hooks/usePlannerBg.ts
William Lam c8e707488b
Implemented some dynamic background sizing & positioning calculations for the Map Planner (#1200)
* Implemented a relatively simple dynamic background sizing calculation for the Map Planner

* Moved useWindowsSize() hook usage near the top of Planner() function

* Removed some dead space above the background image

* Adjustments

* Added a *lot* of math to adjust image spawn location based on the background image size & position, which are based on the current window size & position
- Moved the background image stuff to its own React Hook, usePlannerBg()

* Fix Prettier issue

* Added another comment

* Fixed typing for the usePlannerBg hook
- Also improved comments & some variable names to be cleaner/more consistent

Co-authored-by: Kalle <38327916+Sendouc@users.noreply.github.com>
2023-01-08 11:24:44 +02:00

43 lines
1.2 KiB
TypeScript

import { useState, useEffect } from "react";
import { useWindowSize } from "react-use";
type PlannerBgParams = {
bgWidth: number;
bgHeight: number;
pointOffsetX: number;
pointOffsetY: number;
};
// Dynamic background size. See this issue for more info: https://github.com/Sendouc/sendou.ink/issues/1161
const bgSizeFactor = 0.8;
export function usePlannerBg() {
const [plannerBgParams, setPlannerBgParams] = useState<PlannerBgParams>({
bgWidth: 1200,
bgHeight: 800,
pointOffsetX: 240,
pointOffsetY: 96,
});
// Natively available WindowSize hook: https://usehooks-ts.com/react-hook/use-window-size
const windowSize = useWindowSize();
useEffect(() => {
const bgWidth = windowSize.width * bgSizeFactor;
const bgHeight = windowSize.height * bgSizeFactor;
// Point offsets that move the image closer to the center of the window
const pointOffsetX = bgWidth * (1 - bgSizeFactor);
const pointOffsetY = 0.6 * (bgHeight * (1 - bgSizeFactor)); // Removes some dead space above the image
setPlannerBgParams({
bgWidth,
bgHeight,
pointOffsetX,
pointOffsetY,
});
}, [windowSize.width, windowSize.height]);
return plannerBgParams;
}