video-ad-sdk
Version:
VAST/VPAID SDK that allows video ads to be played on top of any player
38 lines (37 loc) • 1.73 kB
JavaScript
const calculateArea = ({ height, width }) => height * width;
const ICON_MIN_SIZE = 0.35;
export const hasSpace = (newIcon, config) => {
const { drawnIcons, placeholder } = config;
const placeholderArea = calculateArea(placeholder.getBoundingClientRect());
const iconArea = calculateArea(newIcon);
const usedIconsArea = drawnIcons.reduce((accumulator, icon) => {
const { width, height } = icon;
if (!width || !height) {
return accumulator;
}
return accumulator + calculateArea({ width, height });
}, 0);
return iconArea + usedIconsArea <= placeholderArea * ICON_MIN_SIZE;
};
export const withinBoundaries = (newIcon, { placeholder }) => {
const phRect = placeholder.getBoundingClientRect();
return (newIcon.left >= 0 &&
newIcon.left + newIcon.width <= phRect.width &&
newIcon.top >= 0 &&
newIcon.top + newIcon.height <= phRect.height);
};
const right = ({ left, width }) => left + width;
const left = ({ left }) => left;
const top = ({ top }) => top;
const bottom = ({ top, height }) => top + height;
const overlap = (newIcon, drawnIcon) => left(newIcon) <= right(drawnIcon) &&
right(newIcon) >= left(drawnIcon) &&
bottom(newIcon) >= top(drawnIcon) &&
top(newIcon) <= bottom(drawnIcon);
export const withoutOverlaps = (newIcon, { drawnIcons }) => !drawnIcons.some((drawnIcon) => overlap(newIcon, drawnIcon));
export const canBeRendered = (newIcon, config) => {
const thereIsSpace = hasSpace(newIcon, config);
const isWithinTheContentArea = withinBoundaries(newIcon, config);
const doesNotOverlap = withoutOverlaps(newIcon, config);
return thereIsSpace && isWithinTheContentArea && doesNotOverlap;
};