Skip to content

Instantly share code, notes, and snippets.

@paprikka
Created November 12, 2021 16:18
Show Gist options
  • Save paprikka/b519a8f5c4ba020900ffc8aa9baa27b1 to your computer and use it in GitHub Desktop.
Save paprikka/b519a8f5c4ba020900ffc8aa9baa27b1 to your computer and use it in GitHub Desktop.
Rainbow bg from https://potato.horse
import { useEffect } from 'react'
export const useRainbowBg = () =>
useEffect(() => {
const cb = () => {
const viewportHeight = window.innerHeight
const contentHeight = document.body.getBoundingClientRect().height
const viewportsPerRotation = Math.min(
3,
contentHeight / viewportHeight
)
const from = 51
const to = 219
const progress =
window.scrollY / (viewportHeight * viewportsPerRotation)
const h = from + (to - from) * progress
document.body.style.backgroundColor = `hsl(${h}deg 100% 50%)`
}
window.addEventListener('scroll', cb, { passive: true })
return () => window.removeEventListener('scroll', cb)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment