From d083df7245f219d758dbb48354e55d9d4715c339 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Mon, 14 Mar 2022 18:23:18 -0500 Subject: [PATCH] ISSUE-053 - Have worked out the scaled video feed element. Didn't have to do anything to position the element, which made me pretty happy. I assumed I'd have to do some annoying amount of finagling to get it to position correctly in the frame. It just worked. Lucky me. --- .../Broadcast/VideoFeed/VideoFeed.tsx | 41 +++++++++++++++++-- src/components/Broadcast/VideoFeed/styles.ts | 24 +++++++++++ src/shared/theme.ts | 4 ++ src/utils/styling.ts | 34 +++++++++++++++ 4 files changed, 99 insertions(+), 4 deletions(-) diff --git a/src/components/Broadcast/VideoFeed/VideoFeed.tsx b/src/components/Broadcast/VideoFeed/VideoFeed.tsx index 7de18c9..e9d5908 100644 --- a/src/components/Broadcast/VideoFeed/VideoFeed.tsx +++ b/src/components/Broadcast/VideoFeed/VideoFeed.tsx @@ -1,9 +1,42 @@ -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 } from "./styles"; +import { MockupBlock } from "../../Common/MockupBlock.styled"; import bg from "../../../public/images/misc/bg-standin.jpg"; export default function VideoFeed() { - return - - ; + const [frameRef, { + height: frameHeight, + width: frameWidth, + }] = useMeasure(); + + const { + dimensionsWidthPx: broadcastWidth, + dimensionsHeightPx: broadcastHeight, + } = theme.broadcast; + + const [layoutWidth, layoutHeight] = calculateFitSize(frameWidth, frameHeight, + [broadcastWidth, broadcastHeight]); + const layoutScale = calculateScale(layoutWidth, layoutHeight, + [broadcastWidth, broadcastHeight]); + + return ( + + + {Math.floor(frameWidth)} x {Math.floor(frameHeight)}px + + + + + + + ); } diff --git a/src/components/Broadcast/VideoFeed/styles.ts b/src/components/Broadcast/VideoFeed/styles.ts index ef4e844..835d68e 100644 --- a/src/components/Broadcast/VideoFeed/styles.ts +++ b/src/components/Broadcast/VideoFeed/styles.ts @@ -1,9 +1,12 @@ import styled, { css } from "styled-components/macro"; +import { Fraction, Px } from "../../../types/style"; + type VideoFeedFrameProps = { centered?: boolean, }; export const VideoFeedFrame = styled.div` + position: relative; height: 100%; width: 100%; background-color: ${p => p.theme.colors.videoFrameBG}; @@ -20,6 +23,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 +53,11 @@ 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}; +`; diff --git a/src/shared/theme.ts b/src/shared/theme.ts index 5e6536e..fa5c275 100644 --- a/src/shared/theme.ts +++ b/src/shared/theme.ts @@ -102,6 +102,10 @@ export const theme = { medRadius: "11px", smRadius: "7px", }, + broadcast: { + dimensionsWidthPx: 1920, + dimensionsHeightPx: 1080, + }, }; export type MainThemeProps = ThemeProps; diff --git a/src/utils/styling.ts b/src/utils/styling.ts index 82f833c..e919523 100644 --- a/src/utils/styling.ts +++ b/src/utils/styling.ts @@ -182,3 +182,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; +}