Animating a radial gradient with Framer Motion

Ойын-сауық

Learn how to build a spotlight effect by animating a radial gradient that follows the mouse cursor. Built with React, Framer Motion and Tailwind CSS.
🔗 LINKS
Code: buildui.com/recipes/spotlight
Motion values: www.framer.com/motion/motionv...
🕐 TIMESTAMPS
0:00 - Intro
0:21 - Radial gradient
2:55 - Tracking the mouse position
7:50 - Updating the gradient position with React state
9:26 - Too many re-renders
10:37 - Refactoring to motion values
13:41 - Revealing the spotlight on hover
15:30 - Adding a border glow
16:59 - Final demo

Пікірлер: 64

  • @sprioleau
    @sprioleau Жыл бұрын

    Pro tip: 👍 Like before watching. It's going to be good!

  • @lokkeestudios
    @lokkeestudios Жыл бұрын

    Been using these sort of card spotlight effects for quite a while now. Nice to see a different approach to them. Keep up the neat, high-quality content! 🎉

  • @ayoubahabchane
    @ayoubahabchane Жыл бұрын

    Sam is a gem, as always 💎 Not only do you learn how to make something work, but the gradual addressing of UX and performance considerations during each iteration gives you unshakeable confidence in the code you're writing.

  • @jordan_777.
    @jordan_777. Жыл бұрын

    Beautiful man. I saw you talking about your course the other day and was like "yeah I'm getting it". Saw the price and changed tune real fast. The thing is, after binge watching your videos, I'm beginning to see that most likely no one is going to be able to give me that same knowledge and experience. Gonna have to get that course very soon! Thanks for the amazing videos!

  • @igrb
    @igrb Жыл бұрын

    you just keep on posting bangers. Love it!

  • @halmic
    @halmic10 ай бұрын

    Crazy thanks for the tip to use groups for the detection- I was initially going to use another motionvalue, but the group idea keeping it in-css is much neater.

  • @zakioria7478
    @zakioria7478 Жыл бұрын

    Always impressed by your videos, Keep it up!

  • @BinhNguyen-os1dc
    @BinhNguyen-os1dc Жыл бұрын

    love your content so much ❤

  • @benshapiro9731
    @benshapiro973111 ай бұрын

    absolute god-tier video, hats off sir

  • @Benjamin-Chavez
    @Benjamin-Chavez Жыл бұрын

    Incredible vids. Thank you!!

  • @jacobkim9856
    @jacobkim9856 Жыл бұрын

    Glorious video

  • @wfl-junior
    @wfl-junior Жыл бұрын

    awesome effect

  • @jeffsegovia00
    @jeffsegovia00 Жыл бұрын

    We don't deserve this tutorial. Thanks Sam. I learned a lot... Again... Please keep it coming! ☺

  • @kristemmerman921
    @kristemmerman921 Жыл бұрын

    Great video Sam!

  • @FishTalkFish
    @FishTalkFish Жыл бұрын

    Great video, Sam! Thumbs up!

  • @YuraZavadenko
    @YuraZavadenko Жыл бұрын

    great as per usual

  • @alterickoo
    @alterickoo10 ай бұрын

    Great Video!!

  • @zyzlol
    @zyzlol Жыл бұрын

    hey that gradient position option is awesome

  • @DioArsya
    @DioArsya Жыл бұрын

    Framer should sponsored you for all hi quality contents on framer motion, love it!

  • @mukleshossen751
    @mukleshossen751 Жыл бұрын

    Really impressed brother 😍

  • @fiordnord
    @fiordnord Жыл бұрын

    I just love Jamie Lanister teaching me front end

  • @samselikoff

    @samselikoff

    Жыл бұрын

    LOL going through the show now for the first time... this is either an insult or compliment depending on if we're talking season 1 jamie or season 3 😂

  • @fiordnord

    @fiordnord

    Жыл бұрын

    @@samselikoff Definitely the compliment! Only based on visual resemblance😄

  • @FlorinPop
    @FlorinPop Жыл бұрын

    Loved it!! ❤ Recreated it in Vanilla and then in React also (no framer)

  • @ammargillani509

    @ammargillani509

    Жыл бұрын

    Would love to see a video on a vanilla version of this Florin. Great video Sam!!

  • @hampusnilsson2389
    @hampusnilsson238910 ай бұрын

    Love your videos! Would love to see this example with a smoothed out animation using lerp and Framer Motion! So the gradient “lags” behind the cursor a bit :)

  • @MrUltimateX
    @MrUltimateX Жыл бұрын

    Sam can you please make a video of how you have your VSCode setup? And what power-user shortcuts and tools you use? It always so satisfying to see you navigate and use VSCode so well

  • @kemplerart9707

    @kemplerart9707

    11 ай бұрын

    For navigation, I think he uses the Vim plugin.

  • @DEV_XO
    @DEV_XO Жыл бұрын

    🔥More framer motion content please!

  • @sillyleo
    @sillyleo Жыл бұрын

    Awesomeness 🎉

  • @icaro_rdp
    @icaro_rdp Жыл бұрын

    NEAT !

  • @spiritz0r317
    @spiritz0r317 Жыл бұрын

    Such a great video and effect. Unfortunately KZread's compression doesn't do it justice. You should consider publishing your videos in 4k too. Keep up the great work!

  • @samselikoff

    @samselikoff

    Жыл бұрын

    Great idea - I was wondering about how to fix that. Will be experimenting on this soon!

  • @rand0mtv660
    @rand0mtv660 Жыл бұрын

    I hope React introduces some primitives to make animations a first class citizen in React. It's funny that in order to get good performance, you essentially have to do things outside of React. I've seen React maintainers mention that they want to make animation primitives for React, but I'm guessing that's still far into the future considering server components are currently the main focus as far as I've seen.

  • @user-vu5ke6ws7f
    @user-vu5ke6ws7f Жыл бұрын

    Big like 👌 👍 😗. Thank you...

  • @ereburg
    @ereburg Жыл бұрын

    Awesome tutorial, thank you! Wondering, is it possible to create scroll-spy functionality with framer-motion?

  • @fam0uq
    @fam0uq Жыл бұрын

    Hey Sam, nice video and good introduction for Framer-motion! But you can do this easily without any animation library, just use ref instead (as libraries do under the hood). So code will be like this ``` function handleMouseMove(e: MouseEvent) { const { left, top } = e.currentTarget.getBoundingClientRect(); const mouseX = e.clientX - left; const mouseY = e.clientY - top; gradientRef.current?.style.setProperty( "background", `radial-gradient( 650px circle at ${mouseX}px ${mouseY}px, rgba(14, 165, 233, 0.15), transparent 80% ) ` ); } ``` p.s don't forget add ref to gradient div

  • @samselikoff

    @samselikoff

    Жыл бұрын

    I prefer having one approach to all techniques like this since (a) it saves energy from having to think about multiple approaches and (b) more importantly, composes with any new functionality I might want to add. For example I made a version of this that used a useMeasure() hook to update the circle width, x, and y positions based on window resizes. Because the Framer Motion version is in the render function all of that new functionality just worked. In your version, you'd have to refactor the imperative `setProperty` call back out into render. In general my rule is to use the declarative version of any API as a first pass since by default it's reactive, and to only dip into imperative APIs when they're necessary.

  • @muhammadrashid7724

    @muhammadrashid7724

    11 ай бұрын

    @@samselikoff nice explanation.

  • @faridhamzayev8082
    @faridhamzayev8082 Жыл бұрын

    Great animation! I'm wondering if the animation would still work if you changed react useState with useRef and update ref values instead?

  • @EvertJunior
    @EvertJunior Жыл бұрын

    Amazing as always! On a side note, are you using the oficial Dracula theme on vscode or is it some variant? It looks darker and nicer.

  • @dvlden
    @dvlden Жыл бұрын

    I see you combine some basic CSS transitions and Framer Motion. What do you think of using only one or the other? E.g. only CSS or only Framer Motion. Any pros and cons you could point out?

  • @ZachSchnackel
    @ZachSchnackel Жыл бұрын

    Classic Ugmonk shirt 😎

  • @maxwell_young
    @maxwell_young Жыл бұрын

    love this tutorial as i do all your work, was following along but got stuck with useMotionTemplate not fixing the "Rendered more hooks than during the previous render." error. Just curious if you know any reason this hook from framer motion might not have worked correctly in this instance.

  • @samselikoff

    @samselikoff

    Жыл бұрын

    thank you! that should just happen once, because here I put the hook directly in the render body, so the first time you save because of hot refresh, you're adding a hook that wasn't there the previous render. you can always move the hook up and set the return value in a variable, and then use that in the JSX, if it's causing you problems (for example due to any conditional rendering in your template).

  • @maxwebstudio
    @maxwebstudio Жыл бұрын

    You and @ThePrimeagen, inspired me give Vim a try, I installed the Intellij's IdeamVim plugin..... ☠ First two were hard... I really struggled... 🔥 But I must admit after 5 days now.... There is no going back !!! 🚀 So thank you so much for the good content and the inspiration 💪

  • @redaaaa

    @redaaaa

    Жыл бұрын

    He's using vscode...

  • @heyiamigor

    @heyiamigor

    Жыл бұрын

    @@redaaaa with Vim plugin

  • @maxwebstudio

    @maxwebstudio

    Жыл бұрын

    ​@@redaaaa Yes I know :) But I'm using PhpStorm. 😊

  • @ER1C897
    @ER1C897 Жыл бұрын

    Hey Sam, wondering what you use for the automatic string interpolation at 8:35? Namely just it automatically switch from " to ` after the ${} input? This would save me literal seconds!

  • @abe10

    @abe10

    Жыл бұрын

    Template String Coverter

  • @isneru
    @isneru Жыл бұрын

    Hey Sam, Framer-motion's lib seems so extensive, how do you find all these new hooks/functions? Do you just explore the docs?

  • @jafetl.ch.8961
    @jafetl.ch.8961 Жыл бұрын

    What Headwind config do you use? I saw when you wrote `group` in the parent div (14:53), automatically the group change position in class name pd: I love your content!

  • @bhatvikrant
    @bhatvikrant Жыл бұрын

    What extension do you use in VScode to turn quotes into template strings automatically Sam?

  • @rishigupta3739
    @rishigupta373911 ай бұрын

    Just one question to you Sam is it good to copy your code and it into our portfolio?

  • @yolla_4
    @yolla_4 Жыл бұрын

    hey Sam what extension your'e using for tailwind class auto alignment on save

  • @redaaaa

    @redaaaa

    Жыл бұрын

    It's called Headwind

  • @very_unique_username

    @very_unique_username

    Жыл бұрын

    Prettier plugin does it automatically.

  • @rizwansabir2448
    @rizwansabir24483 күн бұрын

    I realized that u should have used offsetX and offsetY to find the center and your video can be shorter too.😅

  • @bullettime2808
    @bullettime2808 Жыл бұрын

    damnn

  • @mukleshossen751
    @mukleshossen751 Жыл бұрын

    1st view 🙃

  • @very_unique_username
    @very_unique_username Жыл бұрын

    -I’d like to recommend to never ever use “transparent” keyword to specify a transparent colour, especially in gradient steps. First step colour with zero opacity works much better.-

  • @samselikoff

    @samselikoff

    Жыл бұрын

    Why?

  • @very_unique_username

    @very_unique_username

    Жыл бұрын

    @@samselikoff oh well, I guess there's no reason why, just tried using "transparent" myself and I don't see any difference at all. A long time ago I've had issues with using "transparent" because technically it's a black colour and in some environments it resulted in uneven gradient with visible dithering since it was interpolating from the starting colour to the transparent black. Actually, in React Native (react-native-svg) I've had this issue half a year ago, so I guess it's still a good rule of thumb for me.

Келесі