Skip to content

Commit

Permalink
feat(landing section): optimisation of animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Whorlow committed Nov 8, 2023
1 parent 2237715 commit 9095133
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions src/components/GentleGoodnight/utils/getRandomStartPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface LocationMap {
[key: string]: PositionType[];
}

const isBetween = ({ largerEl, smallerEl }: { largerEl: [number, number]; smallerEl: [number, number] }) => {
const isBetween = (largerEl: PositionType, smallerEl: PositionType) => {
const leftBound = smallerEl[0] >= largerEl[0] && smallerEl[0] <= largerEl[1];
const rightBound = smallerEl[1] >= largerEl[0] && smallerEl[1] <= largerEl[1];

Expand All @@ -13,13 +13,9 @@ const isBetween = ({ largerEl, smallerEl }: { largerEl: [number, number]; smalle

const checkCellBounds = (newElement: [number, number], existingElement: [number, number]) => {
if (newElement[1] - newElement[0] > existingElement[1] - existingElement[0]) {
// if new element if smaller than old element

return isBetween({ largerEl: newElement, smallerEl: existingElement });
return isBetween(newElement, existingElement);
} else {
// re-ordered so that were comparing the smaller span to larger to save on more maths

return isBetween({ largerEl: existingElement, smallerEl: newElement });
return isBetween(existingElement, newElement);
}
};

Expand All @@ -35,19 +31,13 @@ export const getRandomStartPosInt = (element: HTMLElement, locationMap: Location

const newRandomPosition: PositionType = [randomPoint - 5, randomPoint + element.clientWidth];

switch (true) {
case locationMap[element.offsetTop] == undefined:
locationMap[element.offsetTop] = [newRandomPosition];
break;
const existingPositions = locationMap[element.offsetTop] || [];
const overlaps = existingPositions.some((existingPosition) => checkCellBounds(newRandomPosition, existingPosition));

case locationMap[element.offsetTop].some((existingPosition) =>
checkCellBounds(newRandomPosition, existingPosition)
):
startVector = getRandomStartPosInt(element, locationMap);
break;

default:
locationMap[element.offsetTop] = [...locationMap[element.offsetTop], ...[newRandomPosition]];
if (overlaps) {
startVector = getRandomStartPosInt(element, locationMap);
} else {
locationMap[element.offsetTop] = [...existingPositions, newRandomPosition];
}

return startVector;
Expand Down

0 comments on commit 9095133

Please sign in to comment.