diff --git a/src/components/Broadcast/BroadcastDirectorUI/Chyrons/ChyronsDirectorControls.tsx b/src/components/Broadcast/BroadcastDirectorUI/Chyrons/ChyronsDirectorControls.tsx index 5c5235e..6e4e87f 100644 --- a/src/components/Broadcast/BroadcastDirectorUI/Chyrons/ChyronsDirectorControls.tsx +++ b/src/components/Broadcast/BroadcastDirectorUI/Chyrons/ChyronsDirectorControls.tsx @@ -71,7 +71,6 @@ export function ChyronsDirectorControls() { } const newChyrons: BGChyrons = { openState: 0, - relativePos: { top: "0", right: "0", bottom: "0", left: "0" }, mode: chyronMode, subMode: chyronSubMode, driver: { diff --git a/src/components/Broadcast/Layout/Layout.tsx b/src/components/Broadcast/Layout/Layout.tsx index 1e1f818..fb7fe3f 100644 --- a/src/components/Broadcast/Layout/Layout.tsx +++ b/src/components/Broadcast/Layout/Layout.tsx @@ -1,6 +1,10 @@ -import { MockupBlock } from "../../Common/MockupBlock.styled"; +import { PadBox } from "@bedrock-layout/padbox"; + import VideoFeed from "../VideoFeed/VideoFeed"; +import { MockupBlock } from "../../Common/MockupBlock.styled"; import { LayoutGrid, LayoutGridItem } from "../../Common/LayoutGrid.styled"; +import { TimingTowerPrototypeControls } from "../../Workspaces/PrototypingControls/TimingTowerPrototype"; +import { ChyronsDirectorControls } from "../BroadcastDirectorUI/Chyrons"; type LayoutProps = { headerHeight?: number, @@ -16,7 +20,7 @@ export function Layout({ headerHeight }: LayoutProps) { >Timing Board UI - + @@ -32,10 +36,11 @@ export function Layout({ headerHeight }: LayoutProps) { >Race Director UI - - Broadcast Director UI + + + + + ); diff --git a/src/components/Broadcast/VideoFeed/VideoFeed.tsx b/src/components/Broadcast/VideoFeed/VideoFeed.tsx index 7de18c9..c8fd11c 100644 --- a/src/components/Broadcast/VideoFeed/VideoFeed.tsx +++ b/src/components/Broadcast/VideoFeed/VideoFeed.tsx @@ -1,9 +1,46 @@ -import { VideoFeedFrame, BackgroundImageDiv } from "./styles"; +import useMeasure from "react-use-measure"; + +import { calculateFitSize, calculateScale } from "../../../utils/styling"; +import { theme } from "../../../shared/theme"; + +import { VideoFeedFrame, BackgroundImageDiv, DimensionsSpan, BroadcastLayout, Logo } from "./styles"; +import { TimingBoard } from "../../BroadcastGraphics/TimingBoard"; +import { ChyronContainer } from "../../BroadcastGraphics/Chyrons/Container"; import bg from "../../../public/images/misc/bg-standin.jpg"; +import f1Logo from "../../../public/images/logos/f1-logo-white.svg"; export default function VideoFeed() { - return - - ; + const [frameRef, { + height: frameHeight, + width: frameWidth, + }] = useMeasure(); + + const { + dimensionsWidthPx: broadcastWidth, + dimensionsHeightPx: broadcastHeight, + logoHeightPx, + placement, + } = theme.broadcast; + + const [layoutWidth, layoutHeight] = calculateFitSize(frameWidth, frameHeight, + [broadcastWidth, broadcastHeight]); + const layoutScale = calculateScale(layoutWidth, layoutHeight, + [broadcastWidth, broadcastHeight]); + + return ( + + + Frame: {Math.floor(frameWidth)} x {Math.floor(frameHeight)}px +
+ Scaled: {Math.floor(layoutWidth)} x {Math.floor(layoutHeight)}px ({(layoutScale * 100).toFixed(1)}%) +
+ + + + + + +
+ ); } diff --git a/src/components/Broadcast/VideoFeed/styles.ts b/src/components/Broadcast/VideoFeed/styles.ts index ef4e844..734f2b1 100644 --- a/src/components/Broadcast/VideoFeed/styles.ts +++ b/src/components/Broadcast/VideoFeed/styles.ts @@ -1,9 +1,13 @@ import styled, { css } from "styled-components/macro"; +import { Fraction, Placement, Px } from "../../../types/style"; +import { placementStyleRules } from "../../../utils/styling"; + type VideoFeedFrameProps = { centered?: boolean, }; export const VideoFeedFrame = styled.div` + position: relative; height: 100%; width: 100%; background-color: ${p => p.theme.colors.videoFrameBG}; @@ -20,6 +24,19 @@ export const VideoFeedFrame = styled.div` `} `; +type BroadcastLayoutProps = { + height: Px, + width: Px, + scale: Fraction, +}; +export const BroadcastLayout = styled.div.attrs(({ + height, + width, + scale, +}) => ({ style: { height, width, transform: `scale(${scale})` } }))` + position: absolute; +`; + export const BackgroundImage = styled.img` height: 100%; max-width: 100%; @@ -37,3 +54,21 @@ export const BackgroundImageDiv = styled.div` background-repeat: no-repeat; background-position: 0% 50%; `; + +export const DimensionsSpan = styled.span` + position: absolute; + top: 5px; + right: 10px; + color: ${p => p.theme.colors.textWhite}; + font-family: ${p => p.theme.fonts.f1Regular}; +`; + +type LogoProps = { + placement: Placement, + height: Px, +}; +export const Logo = styled.img` + position: absolute; + ${({ placement }) => placementStyleRules(placement)} + height: ${({ height }) => height}px; +`; diff --git a/src/components/BroadcastGraphics/Chyrons/Container/ChyronContainer.tsx b/src/components/BroadcastGraphics/Chyrons/Container/ChyronContainer.tsx index 6766c0f..be3f11e 100644 --- a/src/components/BroadcastGraphics/Chyrons/Container/ChyronContainer.tsx +++ b/src/components/BroadcastGraphics/Chyrons/Container/ChyronContainer.tsx @@ -2,14 +2,16 @@ import { useSelector } from "react-redux"; import { broadcastGraphicsSelector } from "../../../../features/broadcast/graphics/broadcastGraphicsSelector"; import { workspaceSelector } from "../../../../features/workspace/workspaceSelector"; +import { Placement } from "../../../../types/style"; import { ChyronDriver } from "../Driver"; -import { DoubleChyronContainer } from "./styles"; +import { ChyronPlacement, DoubleChyronContainer } from "./styles"; type ChyronContainerProps = { debug?: boolean, + placement?: Placement, }; -export function ChyronContainer({ debug = false }: ChyronContainerProps) { +export function ChyronContainer({ debug = false, placement = {} }: ChyronContainerProps) { const { chyrons } = useSelector(broadcastGraphicsSelector); const { debugDurationMultiplier } = useSelector(workspaceSelector); @@ -23,19 +25,20 @@ export function ChyronContainer({ debug = false }: ChyronContainerProps) { debugDurationMultiplier={debug ? debugDurationMultiplier : 1} /> ); - if (chyrons.subMode === "medium" && chyrons.driver.secondary) { - return ( - - {primary} - - - ); - } else { - return primary; - } + + return ( + + {chyrons.subMode === "medium" && chyrons.driver.secondary ? ( + + {primary} + + + ) : primary} + + ); } diff --git a/src/components/BroadcastGraphics/Chyrons/Container/styles.ts b/src/components/BroadcastGraphics/Chyrons/Container/styles.ts index 7ce6563..7766d88 100644 --- a/src/components/BroadcastGraphics/Chyrons/Container/styles.ts +++ b/src/components/BroadcastGraphics/Chyrons/Container/styles.ts @@ -1,7 +1,19 @@ import styled from "styled-components/macro"; +import { Placement } from "../../../../types/style"; +import { placementStyleRules } from "../../../../utils/styling"; + export const DoubleChyronContainer = styled.div` display: flex; justify-content: space-between; width: 1196px; `; + + +type ChyronPlacementProps = { + placement: Placement, +}; +export const ChyronPlacement = styled.div` + position: absolute; + ${({ placement }) => placementStyleRules(placement)} +`; diff --git a/src/components/BroadcastGraphics/Chyrons/Driver/ChyronDriver.tsx b/src/components/BroadcastGraphics/Chyrons/Driver/ChyronDriver.tsx index 888442e..39d12e0 100644 --- a/src/components/BroadcastGraphics/Chyrons/Driver/ChyronDriver.tsx +++ b/src/components/BroadcastGraphics/Chyrons/Driver/ChyronDriver.tsx @@ -1,7 +1,7 @@ import { useSpring } from "@react-spring/web"; import { useDispatch } from "react-redux"; -import { BGChyronDriver, BGChyronSubMode, OpenState } from "../../../../types/state"; +import { BGChyronDriver, BGChyronSubMode, CarStatus, OpenState } from "../../../../types/state"; import { DebugDurationProps } from "../../../../types/style"; import { clearChyrons } from "../../../../features/broadcast/graphics/broadcastGraphicsSlice"; import { theme } from "../../../../shared/theme"; @@ -98,7 +98,7 @@ export function ChyronDriver({ progress, 5, ); - return `polygon(${coordinates.map(([x, y]) => `${x}px ${y}px`).join(", ")})`; + return `polygon(${coordinates.map(([x, y]) => `${x}% ${y}%`).join(", ")})`; }), }} /> @@ -133,7 +133,7 @@ export function ChyronDriver({ teamColor={teamColor} /> - {showPosFlag && ( + {showPosFlag && car.status !== CarStatus.Retired && ( `${x}px ${y}px`).join(", ")})`; + return `polygon(${coordinates.map(([x, y]) => `${x}% ${y}%`).join(", ")})`; }), }} ref={childRef} diff --git a/src/components/BroadcastGraphics/TimingBoard/TimingBoard.tsx b/src/components/BroadcastGraphics/TimingBoard/TimingBoard.tsx index 0164ed7..b1e8202 100644 --- a/src/components/BroadcastGraphics/TimingBoard/TimingBoard.tsx +++ b/src/components/BroadcastGraphics/TimingBoard/TimingBoard.tsx @@ -1,6 +1,7 @@ import { useSelector } from "react-redux"; import { workspaceSelector } from "../../../features/workspace/workspaceSelector"; +import { Placement } from "../../../types/style"; import { TimingBoardLayout } from "./styles"; import { TimingTower } from "./TimingTower"; @@ -8,11 +9,13 @@ import { StatusIndicator } from "./StatusIndicator"; type TimingBoardProps = { debug?: boolean, + placement?: Placement, }; -export function TimingBoard({ debug = false }: TimingBoardProps) { +export function TimingBoard({ debug = false, placement = {} }: TimingBoardProps) { const { debugDurationMultiplier } = useSelector(workspaceSelector); + return ( - + diff --git a/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTowerRow.tsx b/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTowerRow.tsx index d8c7772..e867d16 100644 --- a/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTowerRow.tsx +++ b/src/components/BroadcastGraphics/TimingBoard/TimingTower/TimingTowerRow.tsx @@ -153,6 +153,13 @@ export function TimingTowerRow({ xScale = rightHalfContent.includes(":") ? 0.75 : 0.9; yScale = rightHalfContent.includes("+") ? 1.1 : 1.0; + + if (rightHalfContent.includes("LAPS") && rightHalfContent.length > 7) { + xScale = 0.85; // Extreme edge case where +10 laps or more behind. + } + if (rightHalfContent.length > 9) { + xScale = 0.7; // Extreme edge case where +10 minutes or more behind. + } } return ( @@ -305,7 +312,7 @@ export function TimingTowerRow({ 3, 3, ); - return `polygon(${coordinates.map(([x, y]) => `${x}px ${y}px`).join(", ")})`; + return `polygon(${coordinates.map(([x, y]) => `${x}% ${y}%`).join(", ")})`; }), }} /> diff --git a/src/components/BroadcastGraphics/TimingBoard/TimingTower/styles.ts b/src/components/BroadcastGraphics/TimingBoard/TimingTower/styles.ts index d09dcca..1f2977f 100644 --- a/src/components/BroadcastGraphics/TimingBoard/TimingTower/styles.ts +++ b/src/components/BroadcastGraphics/TimingBoard/TimingTower/styles.ts @@ -221,6 +221,7 @@ export const TimeDiff = styled.span` color: ${p => p.theme.colors.textWhite}; font-family: ${p => p.theme.fonts.f1Regular}; font-size: 20px; + white-space: nowrap; text-align: end; diff --git a/src/components/BroadcastGraphics/TimingBoard/styles.ts b/src/components/BroadcastGraphics/TimingBoard/styles.ts index 347222d..1072346 100644 --- a/src/components/BroadcastGraphics/TimingBoard/styles.ts +++ b/src/components/BroadcastGraphics/TimingBoard/styles.ts @@ -1,7 +1,14 @@ import styled from "styled-components/macro"; -export const TimingBoardLayout = styled.div` - position: relative; +import { Placement } from "../../../types/style"; +import { placementStyleRules } from "../../../utils/styling"; + +type TimingBoardLayoutProps = { + placement: Placement, +}; +export const TimingBoardLayout = styled.div` + position: absolute; + ${({ placement }) => placementStyleRules(placement)} display: flex; flex-direction: column; diff --git a/src/domain/data/broadcastGraphics.ts b/src/domain/data/broadcastGraphics.ts index 05940ce..ed56aa0 100644 --- a/src/domain/data/broadcastGraphics.ts +++ b/src/domain/data/broadcastGraphics.ts @@ -1,17 +1,19 @@ import { keys } from "lodash"; -import { BGChyronSubMode, BGChyronMode, BGRelativePos, FlagMode } from "../../types/state"; + +import { BGChyronSubMode, BGChyronMode, FlagMode } from "../../types/state"; +import { Placement } from "../../types/style"; // Chyrons Section const chyronsData: { [mode: BGChyronMode]: { - relativePos: BGRelativePos, + placement: Placement, subModes: { [subMode: BGChyronSubMode]: Record, }, }, } = { driver: { - relativePos: { top: "0", right: "0", bottom: "0", left: "0" }, + placement: { top: "0", right: "0", bottom: "0", left: "0" }, subModes: { medium: { width: 582, height: 72 }, large: { width: 638, height: 98 }, @@ -25,8 +27,8 @@ export function getChyronModesList(): Array { return keys(chyronsData); } -export function getChyronRelativePos(mode: BGChyronMode): BGRelativePos { - return chyronsData[mode]?.relativePos +export function getChyronRelativePos(mode: BGChyronMode): Placement { + return chyronsData[mode]?.placement ?? { top: "0", right: "0", bottom: "0", left: "0" }; } diff --git a/src/features/broadcast/graphics/broadcastGraphicsSlice.ts b/src/features/broadcast/graphics/broadcastGraphicsSlice.ts index ffcbc5f..3e2fcd9 100644 --- a/src/features/broadcast/graphics/broadcastGraphicsSlice.ts +++ b/src/features/broadcast/graphics/broadcastGraphicsSlice.ts @@ -13,13 +13,12 @@ import { const initialState: RootState["broadcastGraphics"] = { chyrons: null, timingBoard: { - relativePos: {}, - openState: 0, + openState: 1, statusIndicator: { mode: BGStatusIndicatorModes.LapNarrow, }, timingTower: { - open: 1, + openState: 1, displayMode: BGTimingTowerDisplayMode.LeftAndRight, splitsMode: BGTimingTowerSplitsMode.Leader, focusedCars: [], diff --git a/src/features/event/eventSlice.ts b/src/features/event/eventSlice.ts index 7140802..f03dbe3 100644 --- a/src/features/event/eventSlice.ts +++ b/src/features/event/eventSlice.ts @@ -72,7 +72,7 @@ const initialState: RootState["event"] = { }, { position: 4, - status: CarStatus.Retired, + status: CarStatus.Normal, tyre: DEFAULT_TYRE, notices: [], distance: POLE_TO_START - GRID_POSITION_GAP * 3, @@ -88,7 +88,7 @@ const initialState: RootState["event"] = { }, { position: 6, - status: CarStatus.Retired, + status: CarStatus.Normal, tyre: DEFAULT_TYRE, notices: [], distance: POLE_TO_START - GRID_POSITION_GAP * 5, diff --git a/src/public/images/logos/f1-logo-red.svg b/src/public/images/logos/f1-logo-red.svg index e29d0ad..04ea67e 100644 --- a/src/public/images/logos/f1-logo-red.svg +++ b/src/public/images/logos/f1-logo-red.svg @@ -1,12 +1 @@ - - - - - - - - + \ No newline at end of file diff --git a/src/public/images/logos/f1-logo-white.svg b/src/public/images/logos/f1-logo-white.svg new file mode 100755 index 0000000..5635e08 --- /dev/null +++ b/src/public/images/logos/f1-logo-white.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/shared/theme.ts b/src/shared/theme.ts index 5e6536e..de7f0f5 100644 --- a/src/shared/theme.ts +++ b/src/shared/theme.ts @@ -102,6 +102,16 @@ export const theme = { medRadius: "11px", smRadius: "7px", }, + broadcast: { + dimensionsWidthPx: 1920, + dimensionsHeightPx: 1080, + logoHeightPx: 40, + placement: { + chyrons: { bottom: "52px", left: "378px" }, + timingBoard: { top: "54px", left: "96px" }, + logo: { left: "86px", bottom: "52px" }, + }, + }, }; export type MainThemeProps = ThemeProps; diff --git a/src/types/state.ts b/src/types/state.ts index ad83a28..19bffdc 100644 --- a/src/types/state.ts +++ b/src/types/state.ts @@ -1,5 +1,5 @@ import { Meters, Milliseconds, Optional } from "./util"; -import { Corner, Fraction } from "./style"; +import { Corner, Fraction, Placement } from "./style"; export interface RootState { pageDimensions: PageDimensions; @@ -253,14 +253,7 @@ export interface BroadcastDirector { } // Broadcast Graphics -export interface BGRelativePos { - top?: string; - right?: string; - bottom?: string; - left?: string; -} export interface BGBaseState { - relativePos: BGRelativePos; openState: OpenState; } /** @@ -293,7 +286,7 @@ export enum BGStatusIndicatorModes { // Timing Tower export interface BGTimingTower { - open: OpenState; + openState: OpenState; displayMode: BGTimingTowerDisplayMode; splitsMode: BGTimingTowerSplitsMode, focusedCars: Car[], diff --git a/src/utils/styling.ts b/src/utils/styling.ts index 82f833c..d0a616b 100644 --- a/src/utils/styling.ts +++ b/src/utils/styling.ts @@ -1,7 +1,7 @@ import { css }from "styled-components/macro"; import { clamp } from "lodash"; -import { Corner, Fraction, Placement, Px, TransitionArgs } from "../types/style"; +import { Corner, Fraction, Percent, Placement, Px, TransitionArgs } from "../types/style"; import { Seconds } from "../types/util"; export function placementStyleRules(pos: Placement) { @@ -14,6 +14,18 @@ export function placementStyleRules(pos: Placement) { } +/** + * Call to create a function that can be re-used to generate a pair of percent + * values to use on a clip-path. + * This was necessary to deal with double-applied scaling issues when using + * element measurement hooks. + * @param width the width of the element to be clipped in pixels + * @param height the height of the element to be clipped in pixels + * @returns A function that can be used to convert pixel values into percent values + */ +const clipConvertToPercent = (width: Px, height: Px) => + ([x, y]: [Px, Px]): [Percent, Percent] => [x / width * 100, y / height * 100]; + /** * Provides the x, y coordinates of a three point polygon for use in a clip-path * mask. By taking a fraction to indicate the progress through the animation it @@ -23,7 +35,7 @@ export function placementStyleRules(pos: Placement) { * @param progress fraction from 0 to 1 * @param startingCorner Which corner the wipe originates from * @param degrees The angle in degrees of the wipe - * @returns Array of six numbers representing pixel values of the clip-path + * @returns Array of three pairs of percentages to the coordinates of the clip-path */ export function wipeCustomDegClip( width: Px, @@ -31,33 +43,34 @@ export function wipeCustomDegClip( progress: Fraction, startingCorner: Corner, degrees: number, -): [[Px, Px], [Px, Px], [Px, Px]] { +): [[Percent, Percent], [Percent, Percent], [Percent, Percent]] { const tangent = Math.tan(clamp(degrees, 0.01, 90) * Math.PI / 180); + const convert = clipConvertToPercent(width, height); switch (startingCorner) { case "topLeft": return [ - [0, 0], - [(width + height / tangent) * progress, 0], - [0, (height + width * tangent) * progress ], + convert([0, 0]), + convert([(width + height / tangent) * progress, 0]), + convert([0, (height + width * tangent) * progress ]), ]; case "topRight": return [ - [width, 0], - [width, (height + width * tangent) * progress], - [width - (width + height / tangent) * progress, 0], + convert([width, 0]), + convert([width, (height + width * tangent) * progress]), + convert([width - (width + height / tangent) * progress, 0]), ]; case "bottomRight": return [ - [width, height], - [width, height - (height + width * tangent) * progress], - [width - (width + height / tangent) * progress, height], + convert([width, height]), + convert([width, height - (height + width * tangent) * progress]), + convert([width - (width + height / tangent) * progress, height]), ]; case "bottomLeft": return [ - [0, height], - [0, height - (height + width * tangent) * progress], - [(width + height / tangent) * progress, height], + convert([0, height]), + convert([0, height - (height + width * tangent) * progress]), + convert([(width + height / tangent) * progress, height]), ]; } } @@ -114,7 +127,7 @@ export function commonDirectionalTransition( * @param progress fraction from 0 to 1 * @param outlineThickness the expected max width of the outline * @param interiorThickness the expected max width of the outline internally - * @returns + * @returns Array of six pairs of percentages to the coordinates of the clip-path */ export function outlineClipPath( width: Px, @@ -122,45 +135,53 @@ export function outlineClipPath( progress: Fraction, outlineThickness: Px, interiorThickness: Px = 0, -): [[Px, Px], [Px, Px], [Px, Px], [Px, Px], [Px, Px], [Px, Px]] { +): [ + [Percent, Percent], + [Percent, Percent], + [Percent, Percent], + [Percent, Percent], + [Percent, Percent], + [Percent, Percent], +] { const travelDistance = width * 2 + height; const pt1End = width / travelDistance; const pt2End = (width + height) / travelDistance; + const convert = clipConvertToPercent(width, height); // Part 1: Reveal the bottom line. if (progress <= pt1End) { const x = width * progress / pt1End; return [ - [0, height - interiorThickness], - [0, height - interiorThickness], - [0, height - interiorThickness], - [x, height - interiorThickness], - [x, height + outlineThickness], - [0, height + outlineThickness], + convert([0, height - interiorThickness]), + convert([0, height - interiorThickness]), + convert([0, height - interiorThickness]), + convert([x, height - interiorThickness]), + convert([x, height + outlineThickness]), + convert([0, height + outlineThickness]), ]; // Part 2: Reveal the right line. } else if (progress > pt1End && progress <= pt2End) { const y = height - (height + outlineThickness) * ((progress - pt1End) / (pt2End - pt1End)); return [ - [0, height - interiorThickness], - [width - Math.max(10, interiorThickness), height - interiorThickness], - [width - interiorThickness, y], - [width + outlineThickness, y], - [width + outlineThickness, height + outlineThickness], - [0, height + outlineThickness], + convert([0, height - interiorThickness]), + convert([width - Math.max(10, interiorThickness), height - interiorThickness]), + convert([width - interiorThickness, y]), + convert([width + outlineThickness, y]), + convert([width + outlineThickness, height + outlineThickness]), + convert([0, height + outlineThickness]), ]; // Part 3: Reveal the top line. } else { const x = width - width * ((progress - pt2End) / (1 - pt2End)); return [ - [0, height - interiorThickness], - [x - Math.max(10, interiorThickness), height - interiorThickness], - [x - interiorThickness, -outlineThickness], - [width + outlineThickness, -outlineThickness], - [width + outlineThickness, height + outlineThickness], - [0, height + outlineThickness], + convert([0, height - interiorThickness]), + convert([x - Math.max(10, interiorThickness), height - interiorThickness]), + convert([x - interiorThickness, -outlineThickness]), + convert([width + outlineThickness, -outlineThickness]), + convert([width + outlineThickness, height + outlineThickness]), + convert([0, height + outlineThickness]), ]; } } @@ -182,3 +203,37 @@ export function formatTime(time: Seconds, limit: Seconds) { const seconds = time % 60; return `${minutes}:${seconds.toFixed(3).padStart(6, "0")}`; } + +/** + * Given a frame to work within, this will return the dimensions of an element + * that will best fit into this given frame with a specific ratio. + * @param frameWidth Width of the working frame in pixels + * @param frameHeight Width of the working frame in pixels + * @param ratioWH Ratio in the form of [width, height] + * @returns Dimensions [width, height] of a fitted element in pixels + */ +export function calculateFitSize(frameWidth: Px, frameHeight: Px, ratioWH: [number, number]): [Px, Px] { + const [w, h] = ratioWH; + let scaledWidth = frameWidth; + let scaledHeight = frameHeight; + + if ((frameWidth / frameHeight) > (w / h)) { // Wider frame. + scaledWidth = frameHeight / h * w; + } else if ((frameWidth / frameHeight) < (w / h)) { // Narrower frame. + scaledHeight = frameWidth / w * h; + } + return [scaledWidth, scaledHeight]; +} + +/** + * Given a fit element's dimensions in pixels this take the provided reference + * width and height defined in pixels and return the scale the fitted element + * is currently at. + * @param fitWidth Width of the fitted element in pixels + * @param fitHeight Height of the fitted element in pixels + * @param referenceWH Dimensions that we need to be scaled to [width, height] in pixels + * @returns Scale of the fitted element as a fraction + */ +export function calculateScale(fitWidth: Px, fitHeight: Px, referenceWH: [Px, Px]): Fraction { + return (fitWidth / referenceWH[0] + fitHeight / referenceWH[1]) / 2; +}