Skip to content

Commit

Permalink
feat(logo animation): second iteration of logo animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Whorlow committed Sep 28, 2023
1 parent 362779c commit a40c837
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://harry-whorlow.github.io',
base: '/portfolio',
server: { port: 1234, host: true },
});
29 changes: 14 additions & 15 deletions src/components/Logo/Logo.astro
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@
#cell-15-1 {
width: 65px;
left: 10px;
background-color: var(--accent-pink);
}

#cell-15-2 {
Expand Down Expand Up @@ -417,25 +418,23 @@

<script>
import gsap from 'gsap';
let timeLine = gsap.timeline();

const screenWidthHalved = (window.screen.width - 100) / 2;

const getRandomStartPosInt = () => {
let position = screenWidthHalved * Math.random();
import { getRandomAnimationTime } from './utils/getRandomAnimationTime';
import { getRandomStartPosInt } from './utils/getRandomStartPosition';
import type { LocationMap } from './utils/getRandomStartPosition';

if (Math.random() >= 0.5) {
position = position * -1;
}
let timeLine = gsap.timeline();

return position;
};
const locationMap: LocationMap = {};

const getRandomStartTimeInt = () => {
return Math.random() * (3 - 1 + 1) + 1;
};
Array.from(document.querySelectorAll<HTMLElement>('.logo-cell')).map((element) => {
timeLine.fromTo(`#${element.id}`, { opacity: 0 }, { opacity: 1, duration: getRandomAnimationTime(3, 1) }, 1);

Array.from(document.querySelectorAll('.logo-cell')).map((element) => {
timeLine.fromTo(`#${element.id}`, { x: getRandomStartPosInt() }, { x: 0, duration: getRandomStartTimeInt() }, 1);
timeLine.fromTo(
`#${element.id}`,
{ x: getRandomStartPosInt(element, locationMap) },
{ x: 0, duration: getRandomAnimationTime(3, 1), ease: 'back' },
2
);
});
</script>
3 changes: 3 additions & 0 deletions src/components/Logo/utils/getRandomAnimationTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const getRandomAnimationTime = (max: number, min: number) => {
return Math.random() * (max - min + 1) + min;
};
56 changes: 56 additions & 0 deletions src/components/Logo/utils/getRandomStartPosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
type PositionType = [number, number];

export interface LocationMap {
[key: string]: PositionType[];
}

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

return leftBound || rightBound;
};

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 });
} else {
// re-ordered so that were comparing the smaller span to larger to save on more maths

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

export const getRandomStartPosInt = (element: HTMLElement, locationMap: LocationMap): number => {
const [, row] = element.id.split('-');

const { left: elementLeftPoint, width: elementWidth } = element.getBoundingClientRect();

let randomPoint = (window.screen.width - elementWidth - 20) * Math.random();
let startVector: number;

if (randomPoint > elementLeftPoint) {
startVector = randomPoint - elementLeftPoint;
} else {
startVector = (elementLeftPoint - randomPoint) * -1;
}

const newRandomPosition: PositionType = [randomPoint - 5, randomPoint + elementWidth];

switch (true) {
case locationMap[row] == undefined:
locationMap[row] = [newRandomPosition];
break;

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

default:
locationMap[row] = [...locationMap[row], ...[newRandomPosition]];
}

return startVector;
};
2 changes: 1 addition & 1 deletion src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { title } = Astro.props;
<meta charset="UTF-8" />
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="icon" type="image/svg+xml" href="./public/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<title>{title}</title>
</head>
Expand Down
2 changes: 0 additions & 2 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import CustomCursor from '../components/CustomCursor/CustomCursor.astro';
height: 1000px;
width: 100%;

overflow: hidden;

display: flex;
flex-direction: column;
align-items: center;
Expand Down

0 comments on commit a40c837

Please sign in to comment.