Skip to content

Commit

Permalink
Merge pull request #1 from harry-whorlow/logo-v2
Browse files Browse the repository at this point in the history
feat(logo animation): second iteration of logo animation
  • Loading branch information
harry-whorlow authored and Harry Whorlow committed Sep 28, 2023
2 parents 362779c + a40c837 commit 4edc1f7
Show file tree
Hide file tree
Showing 7 changed files with 80 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;
};
File renamed without changes
7 changes: 6 additions & 1 deletion src/layouts/Layout.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
import { getImage } from 'astro:assets';
import faviconSvgSrc from '../images/favicon.svg';
const faviconSvg = await getImage({ src: faviconSvgSrc, format: 'svg' });
interface Props {
title: string;
}
Expand All @@ -12,7 +17,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" sizes="any" href={faviconSvg.src} />
<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 4edc1f7

Please sign in to comment.