Jump to content
Search Community

Cursor follows the eyes

Stagnax test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

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 comment
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 comment
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 comment
Share on other sites

  • 2 years later...

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 comment
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 comment
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 comment
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.
×
×
  • Create New...