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.
This commit is contained in:
Kyle Geib committed 2022-03-14 18:23:18 -05:00
1 parent 40292620d6
commit d083df7245
4 files changed
+99 -4

No files matched your search

@@ -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 <VideoFeedFrame centered>
<BackgroundImageDiv src={bg} />
</VideoFeedFrame>;
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 (
<VideoFeedFrame centered ref={frameRef}>
<DimensionsSpan>
{Math.floor(frameWidth)} x {Math.floor(frameHeight)}px
</DimensionsSpan>
<BackgroundImageDiv src={bg} />
<BroadcastLayout width={broadcastWidth} height={broadcastHeight} scale={layoutScale} >
<MockupBlock
width="300px"
height="900px"
color="grey"
/>
</BroadcastLayout>
</VideoFeedFrame>
);
}
@@ -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<VideoFeedFrameProps>`
position: relative;
height: 100%;
width: 100%;
background-color: ${p => p.theme.colors.videoFrameBG};
@@ -20,6 +23,19 @@ export const VideoFeedFrame = styled.div<VideoFeedFrameProps>`
`}
`;
type BroadcastLayoutProps = {
height: Px,
width: Px,
scale: Fraction,
};
export const BroadcastLayout = styled.div.attrs<BroadcastLayoutProps>(({
height,
width,
scale,
}) => ({ style: { height, width, transform: `scale(${scale})` } }))<BroadcastLayoutProps>`
position: absolute;
`;
export const BackgroundImage = styled.img`
height: 100%;
max-width: 100%;
@@ -37,3 +53,11 @@ export const BackgroundImageDiv = styled.div<BackgroundImageDivProps>`
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};
`;
+4
View File
@@ -102,6 +102,10 @@ export const theme = {
medRadius: "11px",
smRadius: "7px",
},
broadcast: {
dimensionsWidthPx: 1920,
dimensionsHeightPx: 1080,
},
};
export type MainThemeProps = ThemeProps<typeof theme>;
+34
View File
@@ -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;
}