Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Stagnax

Cursor follows the eyes

Recommended Posts

Hi,

Id like to know if there is a GSAP function that can create the effect of a mouse following the cursor

 

See the Pen aqxoPg by stagnax (@stagnax) on CodePen

Link to post
Share on other sites

Hi @Stagnax :)

 

There isn't any 'built-in' function to easily do that. It's a matter of calculating mouse position and setting the eye position and/or rotation. It will also depend on whether you're using SVGs, divs or maybe canvas. There are a whole bunch of different styles and approaches on CodePen that should provide some inspiration for you.

 

See the Pen ?limit=all&page=1&q=eye+follow by search (@search) on CodePen

 

Happy tweening.

:)

 

 

 

 

  • Like 2
Link to post
Share on other sites

Hi @Stagnax

 

Give me a few, and I'll make an SVG version as mouse coordinates can get kind of screwy.

Link to post
Share on other sites

Here you go. 

 

See the Pen jzNgqY by osublake (@osublake) on CodePen

 

 

I show some of the trig used for this here.

 

 

And how to get mouse coordinates inside an SVG here.

 

 

  • Like 5
Link to post
Share on other sites

Hi @OSUblake,

Thank you for the examples and thread.

One thing that I'd like to know is , and I am basically referring to JavaScript is , how do i not get tangled in creating a certain animation or achieving a certain effect or  complex logical method with the overwhelming amount of resources found online as I learn everything form the web.

 

(I've read the links you've just sent me and I noticed creating small games can really help in building logic , so I am starting to do that)

:)

 

  • Like 2
Link to post
Share on other sites

That's a problem every developer faces. How to avoid spaghetti code, and there is no easy answer.

 

I think learning how to structure stuff like a game is really helpful. I would also look at how frameworks like React and Vue structure stuff into components. Creating logical grouping of the stuff you are working with can go a long away. 

  • Like 3
Link to post
Share on other sites

Hi @ZachSaucier and thanks for the answer. Your solution is awesome but what I was trying to do is "pupil" element moving inside the yellow "eyeball" element just like the reference 😅     I have to admit that I'm not so prepared in Js! 

Link to post
Share on other sites

The "reference" is equivalent to the g pen, so your intention is not clear.

 

What I think you are wanting (a translation) is quite different than what this thread is about (a rotation). These threads cover related questions:

In short:

  1. Create a max value for the translation in the x and y directions
  2. Create a max distance for the mouse position to the center of the element (the viewport dimensions wouldn't be a bad choice).
  3. Calculate the distance from the mouse position to the center.
  4. Put that number over the max distance from 2)
  5. Multiply that product by the max value from 1 and apply it to your element.

You should do steps 3-5 every update of the mouse move in the given target space.

Link to post
Share on other sites

Sorry Zach, maybe I cannot explain my intention correctly.
The pupil in the g code I've posted is moving with mouse but it goes outside the yellow circle , so what I'm trying to do is move the pupil with mouse but inside the yellow circle (just like the reference, sorry for being so repetitive).

 

Thanks a lot for your time, I'm a visual designer and I'm trying to learn some code.

Link to post
Share on other sites

I'm pretty sure I understand what you're wanting to do. Try doing the steps I describe above and see if it works.

Link to post
Share on other sites

Thank you @ZachSaucier I'm gonna try.

 

Update: Definitely I'm not able to fix this problem, I'll wait for someone who show me how to do.

Edited by giulioiamnot
Link to post
Share on other sites

Thanks @ZachSaucier, that was what I was tryin to do and explain!
Looking at it the only thing that I don't understand is why the pupil stops following the mouse when it is far from the svg in the viewport. But I'm gonna try to fix it.
Thanks again!

Link to post
Share on other sites

I just added the listener to the SVG itself:

document.querySelector('svg').addEventListener('mousemove', updateTrans);

You can change the element to whatever you need it to be. But I don't recommend applying it to the entire body or the window as you only want it to be running when the SVG is actually in view on the page.

  • Like 1
Link to post
Share on other sites

Hi @ZachSaucier thanks again for all the tips! I'll try to change the element because this will go basically on a one-page site. 
Thanks again!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×