-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d33efe3
commit c3eefea
Showing
11 changed files
with
130 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,61 @@ | ||
import { animate } from "framer-motion" | ||
|
||
export const utilities = { | ||
toCartesianCoords: ({ x, y }) => { | ||
const { width, height } = document.documentElement.getBoundingClientRect() | ||
const halfWidth = width / 2 | ||
const halfHeight = height / 2 | ||
const xPos = x - halfWidth | ||
const yPos = y - halfHeight | ||
return { | ||
x: xPos / width, | ||
y: yPos / height | ||
} | ||
}, | ||
toPolarCoords: ({ x, y }) => { | ||
const { width, height } = document.documentElement.getBoundingClientRect() | ||
const halfWidth = width / 2 | ||
const halfHeight = height / 2 | ||
const xPos = x - halfWidth | ||
const yPos = y - halfHeight | ||
const distance = Math.sqrt(xPos * xPos + yPos * yPos) | ||
const angle = Math.atan2(yPos, xPos) | ||
return { | ||
distance: distance / Math.min(width, height), | ||
angle | ||
} | ||
}, | ||
setCustomProperties: ( | ||
properties: Record<string, string>, | ||
element?: HTMLElement | ||
) => { | ||
const targetElement = element || document.documentElement; | ||
|
||
Object.entries(properties).forEach(([key, value]) => { | ||
const formattedKey = key.startsWith('--') ? key : `--${key}`; | ||
targetElement.style.setProperty(formattedKey, value); | ||
}); | ||
}, | ||
getCustomProperty: (property: string, element?: HTMLElement) => { | ||
const targetElement = element || document.documentElement; | ||
return getComputedStyle(targetElement).getPropertyValue(property); | ||
export const toCartesianCoords = ({ x, y }) => { | ||
const { width, height } = document.documentElement.getBoundingClientRect() | ||
const halfWidth = width / 2 | ||
const halfHeight = height / 2 | ||
const xPos = x - halfWidth | ||
const yPos = y - halfHeight | ||
return { | ||
x: xPos / width, | ||
y: yPos / height | ||
} | ||
} | ||
|
||
export const toPolarCoords = ({ x, y }) => { | ||
const { width, height } = document.documentElement.getBoundingClientRect() | ||
const halfWidth = width / 2 | ||
const halfHeight = height / 2 | ||
const xPos = x - halfWidth | ||
const yPos = y - halfHeight | ||
const distance = Math.sqrt(xPos * xPos + yPos * yPos) | ||
const angle = Math.atan2(yPos, xPos) | ||
return { | ||
distance: distance / Math.min(width, height), | ||
angle | ||
} | ||
} | ||
|
||
export const setCustomProperties = ( | ||
properties: Record<string, string>, | ||
element?: HTMLElement | ||
) => { | ||
const targetElement = element || document.documentElement; | ||
|
||
Object.entries(properties).forEach(([key, value]) => { | ||
const formattedKey = key.startsWith('--') ? key : `--${key}`; | ||
targetElement.style.setProperty(formattedKey, value); | ||
}); | ||
} | ||
|
||
export const getCustomProperty = (property: string, element?: HTMLElement) => { | ||
const targetElement = element || document.documentElement; | ||
return getComputedStyle(targetElement).getPropertyValue(property); | ||
} | ||
|
||
export const animateProperties = (from, to, property) => { | ||
animate(from, to, { | ||
duration: 2, | ||
onUpdate: (latest) => utilities.setCustomProperties({ [property]: latest }), | ||
}); | ||
} | ||
|
||
export const utilities = { | ||
toCartesianCoords, | ||
toPolarCoords, | ||
setCustomProperties, | ||
getCustomProperty, | ||
animateProperties | ||
} | ||
|
||
export default utilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters