-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dataroom): sine wave animation (#441)
* Open branch * feat: πΈ Add See More Section * refactor: π‘ address PR comments * fix: π address PR comments + import framer motion on client * fix: π address console error * refactor: π‘ update animation to use canvas instead of svg * fix: remove duplicated section --------- Co-authored-by: iamacook <[email protected]> Co-authored-by: Diogo Soares <[email protected]>
- Loading branch information
1 parent
3c36729
commit 49c1950
Showing
4 changed files
with
200 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { BaseBlock } from '@/components/Home/types' | ||
import { useIsMediumScreen } from '@/hooks/useMaxWidth' | ||
import { Typography } from '@mui/material' | ||
import { motion, useScroll, useTransform } from 'framer-motion' | ||
import type { RefObject } from 'react' | ||
import css from './styles.module.css' | ||
import Wave from './Wave' | ||
|
||
export default function SlidingWave({ | ||
text, | ||
containerRef, | ||
}: { | ||
text: BaseBlock['text'] | ||
containerRef: RefObject<HTMLDivElement> | ||
}) { | ||
const isMobile = useIsMediumScreen() | ||
const { scrollYProgress } = useScroll({ | ||
target: containerRef, | ||
offset: ['start end', 'end start'], | ||
}) | ||
|
||
const opacity = useTransform(scrollYProgress, [0, 0.3, 0.6, 0.75], [0, 1, 1, 0]) | ||
const textOpacity = useTransform(scrollYProgress, [0, 0.3, 0.5, 0.75], [0, 1, 1, 0]) | ||
const translate = useTransform(scrollYProgress, [0, 0.2, 0.55, 0.75], ['-250px', '0px', '0px', '250px']) | ||
|
||
return ( | ||
<> | ||
<motion.div style={{ opacity: textOpacity, translateY: translate }} className={css.text}> | ||
<Typography variant="h1">{text}</Typography> | ||
</motion.div> | ||
<div className={css.gradientBox}></div> | ||
<motion.div style={{ opacity }} className={css.wave}> | ||
<Wave colors={['#003C1C', '#008A40', '#12FF80']} amplitude={isMobile ? 100 : 200} /> | ||
</motion.div> | ||
</> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import useContainerSize from '@/hooks/useContainerSize' | ||
import React, { useEffect, useRef, useMemo, useCallback } from 'react' | ||
|
||
type WaveProps = { | ||
height?: number | ||
colors?: string[] | ||
amplitude?: number | ||
frequency?: number | ||
speed?: number | ||
} | ||
|
||
const DEFAULT_HEIGHT = 600 | ||
const DEFAULT_COLORS = ['#12FF80', '#008A40', '#003C1C'] | ||
const DEFAULT_AMPLITUDE = 100 | ||
const DEFAULT_FREQUENCY = 1 | ||
const DEFAULT_SPEED = 0.8 | ||
const STROKE_WIDTH = 4 | ||
|
||
const Wave = ({ | ||
height = DEFAULT_HEIGHT, | ||
colors = DEFAULT_COLORS, | ||
amplitude = DEFAULT_AMPLITUDE, | ||
frequency = DEFAULT_FREQUENCY, | ||
speed = DEFAULT_SPEED, | ||
}: WaveProps) => { | ||
const canvasRef = useRef<HTMLCanvasElement>(null) | ||
const containerRef = useRef<HTMLDivElement>(null) | ||
const { width } = useContainerSize(containerRef) | ||
const animationRef = useRef<number>() | ||
|
||
const dpr = useMemo(() => window.devicePixelRatio || 1, []) | ||
|
||
const paths = useMemo(() => { | ||
if (width === 0) return [[], [], []] | ||
const calculatePath = (amplitudeOffset: number) => | ||
Array.from( | ||
{ length: width + 1 }, | ||
(_, i) => (amplitude - amplitudeOffset) * Math.sin((i / width) * 2 * Math.PI * frequency), | ||
) | ||
return [calculatePath(80), calculatePath(40), calculatePath(0)] | ||
}, [width, amplitude, frequency]) | ||
|
||
const setupCanvas = useCallback(() => { | ||
const canvas = canvasRef.current | ||
const ctx = canvas?.getContext('2d') | ||
if (!ctx || !canvas || width === 0) return | ||
|
||
canvas.width = width * dpr | ||
canvas.height = height * dpr | ||
canvas.style.width = `${width}px` | ||
canvas.style.height = `${height}px` | ||
ctx.scale(dpr, dpr) | ||
}, [width, height, dpr]) | ||
|
||
const drawWave = useCallback( | ||
(ctx: CanvasRenderingContext2D, path: number[], color: string, shift: number) => { | ||
ctx.beginPath() | ||
ctx.lineWidth = STROKE_WIDTH | ||
ctx.strokeStyle = color | ||
|
||
for (let i = 0; i <= width; i++) { | ||
ctx.lineTo(i, height / 2 + path[(i + Math.floor(shift)) % width]) | ||
} | ||
|
||
ctx.stroke() | ||
}, | ||
[width, height], | ||
) | ||
|
||
const animate = useCallback( | ||
(time: number) => { | ||
const canvas = canvasRef.current | ||
const ctx = canvas?.getContext('2d') | ||
if (!ctx || !canvas) return | ||
|
||
const shift = (time * speed) % width | ||
|
||
ctx.clearRect(0, 0, width, height) | ||
|
||
paths.forEach((path, index) => { | ||
drawWave(ctx, path, colors[index % colors.length], shift) | ||
}) | ||
|
||
animationRef.current = requestAnimationFrame(animate) | ||
}, | ||
[width, height, paths, speed, colors, drawWave], | ||
) | ||
|
||
useEffect(() => { | ||
setupCanvas() | ||
animationRef.current = requestAnimationFrame(animate) | ||
|
||
return () => { | ||
if (animationRef.current) { | ||
cancelAnimationFrame(animationRef.current) | ||
} | ||
} | ||
}, [setupCanvas, animate]) | ||
|
||
return ( | ||
<div ref={containerRef} style={{ width: '100%' }}> | ||
<canvas ref={canvasRef} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Wave |
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