Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 59 additions & 39 deletions src/content/TextAnimations/TextPressure/TextPressure.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
// Component ported from https://codepen.io/JuanFuentes/full/rgXKGQ

import { useEffect, useRef, useState } from 'react';
import { useEffect, useRef, useState, useMemo, useCallback } from 'react';

const dist = (a, b) => {
const dx = b.x - a.x;
const dy = b.y - a.y;
return Math.sqrt(dx * dx + dy * dy);
};

const getAttr = (distance, maxDist, minVal, maxVal) => {
const val = maxVal - Math.abs((maxVal * distance) / maxDist);
return Math.max(minVal, val + minVal);
};

const debounce = (func, delay) => {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
};

const TextPressure = ({
text = 'Compressa',
fontFamily = 'Compressa VF',
// This font is just an example, you should not use it in commercial projects.
fontUrl = 'https://res.cloudinary.com/dr6lvwubh/raw/upload/v1529908256/CompressaPRO-GX.woff2',

width = true,
Expand Down Expand Up @@ -36,12 +56,6 @@ const TextPressure = ({

const chars = text.split('');

const dist = (a, b) => {
const dx = b.x - a.x;
const dy = b.y - a.y;
return Math.sqrt(dx * dx + dy * dy);
};

useEffect(() => {
const handleMouseMove = e => {
cursorRef.current.x = e.clientX;
Expand All @@ -54,7 +68,7 @@ const TextPressure = ({
};

window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('touchmove', handleTouchMove, { passive: false });
window.addEventListener('touchmove', handleTouchMove, { passive: true });

if (containerRef.current) {
const { left, top, width, height } = containerRef.current.getBoundingClientRect();
Expand All @@ -70,7 +84,7 @@ const TextPressure = ({
};
}, []);

const setSize = () => {
const setSize = useCallback(() => {
if (!containerRef.current || !titleRef.current) return;

const { width: containerW, height: containerH } = containerRef.current.getBoundingClientRect();
Expand All @@ -92,14 +106,14 @@ const TextPressure = ({
setLineHeight(yRatio);
}
});
};
}, [chars.length, minFontSize, scale]);

useEffect(() => {
setSize();
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
// eslint-disable-next-line
}, [scale, text]);
const debouncedSetSize = debounce(setSize, 100);
debouncedSetSize();
window.addEventListener('resize', debouncedSetSize);
return () => window.removeEventListener('resize', debouncedSetSize);
}, [setSize]);

useEffect(() => {
let rafId;
Expand All @@ -122,18 +136,19 @@ const TextPressure = ({

const d = dist(mouseRef.current, charCenter);

const getAttr = (distance, minVal, maxVal) => {
const val = maxVal - Math.abs((maxVal * distance) / maxDist);
return Math.max(minVal, val + minVal);
};
const wdth = width ? Math.floor(getAttr(d, maxDist, 5, 200)) : 100;
const wght = weight ? Math.floor(getAttr(d, maxDist, 100, 900)) : 400;
const italVal = italic ? getAttr(d, maxDist, 0, 1).toFixed(2) : 0;
const alphaVal = alpha ? getAttr(d, maxDist, 0, 1).toFixed(2) : 1;

const wdth = width ? Math.floor(getAttr(d, 5, 200)) : 100;
const wght = weight ? Math.floor(getAttr(d, 100, 900)) : 400;
const italVal = italic ? getAttr(d, 0, 1).toFixed(2) : 0;
const alphaVal = alpha ? getAttr(d, 0, 1).toFixed(2) : 1;
const newFontVariationSettings = `'wght' ${wght}, 'wdth' ${wdth}, 'ital' ${italVal}`;

span.style.opacity = alphaVal;
span.style.fontVariationSettings = `'wght' ${wght}, 'wdth' ${wdth}, 'ital' ${italVal}`;
if (span.style.fontVariationSettings !== newFontVariationSettings) {
span.style.fontVariationSettings = newFontVariationSettings;
}
if (alpha && span.style.opacity !== alphaVal) {
span.style.opacity = alphaVal;
}
});
}

Expand All @@ -142,20 +157,10 @@ const TextPressure = ({

animate();
return () => cancelAnimationFrame(rafId);
}, [width, weight, italic, alpha, chars.length]);
}, [width, weight, italic, alpha]);

const dynamicClassName = [className, flex ? 'flex' : '', stroke ? 'stroke' : ''].filter(Boolean).join(' ');

return (
<div
ref={containerRef}
style={{
position: 'relative',
width: '100%',
height: '100%',
background: 'transparent'
}}
>
const styleElement = useMemo(() => {
return (
<style>{`
@font-face {
font-family: '${fontFamily}';
Expand Down Expand Up @@ -187,7 +192,22 @@ const TextPressure = ({
color: ${textColor};
}
`}</style>
);
}, [fontFamily, fontUrl, flex, stroke, textColor, strokeColor]);

const dynamicClassName = [className, flex ? 'flex' : '', stroke ? 'stroke' : ''].filter(Boolean).join(' ');

return (
<div
ref={containerRef}
style={{
position: 'relative',
width: '100%',
height: '100%',
background: 'transparent'
}}
>
{styleElement}
<h1
ref={titleRef}
className={`text-pressure-title ${dynamicClassName}`}
Expand Down
77 changes: 49 additions & 28 deletions src/tailwind/TextAnimations/TextPressure/TextPressure.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
// Component ported from https://codepen.io/JuanFuentes/full/rgXKGQ

import { useEffect, useRef, useState } from 'react';
import { useEffect, useRef, useState, useMemo, useCallback } from 'react';

const dist = (a, b) => {
const dx = b.x - a.x;
const dy = b.y - a.y;
return Math.sqrt(dx * dx + dy * dy);
};

const getAttr = (distance, maxDist, minVal, maxVal) => {
const val = maxVal - Math.abs((maxVal * distance) / maxDist);
return Math.max(minVal, val + minVal);
};

const debounce = (func, delay) => {
let timeoutId;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
};

const TextPressure = ({
text = 'Compressa',
Expand Down Expand Up @@ -37,12 +58,6 @@ const TextPressure = ({

const chars = text.split('');

const dist = (a, b) => {
const dx = b.x - a.x;
const dy = b.y - a.y;
return Math.sqrt(dx * dx + dy * dy);
};

useEffect(() => {
const handleMouseMove = e => {
cursorRef.current.x = e.clientX;
Expand All @@ -55,7 +70,7 @@ const TextPressure = ({
};

window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('touchmove', handleTouchMove, { passive: false });
window.addEventListener('touchmove', handleTouchMove, { passive: true });

if (containerRef.current) {
const { left, top, width, height } = containerRef.current.getBoundingClientRect();
Expand All @@ -71,7 +86,7 @@ const TextPressure = ({
};
}, []);

const setSize = () => {
const setSize = useCallback(() => {
if (!containerRef.current || !titleRef.current) return;

const { width: containerW, height: containerH } = containerRef.current.getBoundingClientRect();
Expand All @@ -93,14 +108,14 @@ const TextPressure = ({
setLineHeight(yRatio);
}
});
};
}, [chars.length, minFontSize, scale]);

useEffect(() => {
setSize();
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [scale, text]);
const debouncedSetSize = debounce(setSize, 100);
debouncedSetSize();
window.addEventListener('resize', debouncedSetSize);
return () => window.removeEventListener('resize', debouncedSetSize);
}, [setSize]);

useEffect(() => {
let rafId;
Expand All @@ -123,18 +138,19 @@ const TextPressure = ({

const d = dist(mouseRef.current, charCenter);

const getAttr = (distance, minVal, maxVal) => {
const val = maxVal - Math.abs((maxVal * distance) / maxDist);
return Math.max(minVal, val + minVal);
};
const wdth = width ? Math.floor(getAttr(d, maxDist, 5, 200)) : 100;
const wght = weight ? Math.floor(getAttr(d, maxDist, 100, 900)) : 400;
const italVal = italic ? getAttr(d, maxDist, 0, 1).toFixed(2) : 0;
const alphaVal = alpha ? getAttr(d, maxDist, 0, 1).toFixed(2) : 1;

const wdth = width ? Math.floor(getAttr(d, 5, 200)) : 100;
const wght = weight ? Math.floor(getAttr(d, 100, 900)) : 400;
const italVal = italic ? getAttr(d, 0, 1).toFixed(2) : 0;
const alphaVal = alpha ? getAttr(d, 0, 1).toFixed(2) : 1;
const newFontVariationSettings = `'wght' ${wght}, 'wdth' ${wdth}, 'ital' ${italVal}`;

span.style.opacity = alphaVal;
span.style.fontVariationSettings = `'wght' ${wght}, 'wdth' ${wdth}, 'ital' ${italVal}`;
if (span.style.fontVariationSettings !== newFontVariationSettings) {
span.style.fontVariationSettings = newFontVariationSettings;
}
if (alpha && span.style.opacity !== alphaVal) {
span.style.opacity = alphaVal;
}
});
}

Expand All @@ -143,10 +159,10 @@ const TextPressure = ({

animate();
return () => cancelAnimationFrame(rafId);
}, [width, weight, italic, alpha, chars.length]);
}, [width, weight, italic, alpha]);

return (
<div ref={containerRef} className="relative w-full h-full overflow-hidden bg-transparent">
const styleElement = useMemo(() => {
return (
<style>{`
@font-face {
font-family: '${fontFamily}';
Expand All @@ -168,7 +184,12 @@ const TextPressure = ({
-webkit-text-stroke-color: ${strokeColor};
}
`}</style>
);
}, [fontFamily, fontUrl, stroke, textColor, strokeColor, strokeWidth]);

return (
<div ref={containerRef} className="relative w-full h-full overflow-hidden bg-transparent">
{styleElement}
<h1
ref={titleRef}
className={`text-pressure-title ${className} ${
Expand Down
Loading