Menu

How I Built a Mouse-Responsive Gradient Using Just CSS and Math
πŸ“°

How I Built a Mouse-Responsive Gradient Using Just CSS and Math

HackerNoonΒ·sammiiΒ·4 days ago
#mtFJpRBi
#hackernoon#technology#current#colour#gradient
Reading 0:00
15s threshold

The logo on my portfolio site changes colour as you move your mouse. Not with a library. Not with a pre-built animation. With about 15 lines of maths that convert cursor position into RGB values and generate a three-point radial gradient in real time.

And when you scroll, the same gradient system responds using sine and cosine wave functions.

Here's how the whole thing works.

The core function: gradientCreator

The entire gradient system lives in one function that takes two numbers and returns a CSS background:

export const gradientCreator = (xPc: number, yPc: number) => { const colourCreator = (number: number) => { const colour = Math.floor((255 / 100) * number); return colour < 255 ?…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.