Jump to content
Search Community

Mouse movement combined with ScrollTrigger

federico.pian test
Moderator Tag

Recommended Posts

Hi,

 

I a group of mesh and I'm animating some attributes on scroll, like scale, rotation, position etc. with a timeline and ScrollTrigger, and it works fine.
 

tlHero.to(
  canvas.group.rotation,
  {
    y: `-=${Math.PI * 0.26}`,
    x: `+=${Math.PI * 0.26}`,
  },
0);

tlHero.to(
  canvas.group.position,

  {
    x: "-=1",
  },
  0
);

 

 The starting rotation values of the group of mesh are:

canvas.group.position.x = 1.43;
canvas.group.position.y = 0.85;
canvas.group.rotation.x = Math.PI * -0.14;
canvas.group.rotation.y = Math.PI * -0.34;

 

Now I want to add also a mouse move animation where I animate the rotation and position of the group, also this one works:

onMouseMove() {
    this.cursor.x = event.clientX / window.innerWidth - 0.5;
    this.cursor.y = event.clientY / window.innerHeight - 0.5;  

    gsap.to(canvas.group.position, {
      x: 1.43 + this.cursor.x * 0.02,
      y: 0.85 + this.cursor.y * 0.02,
      duration: 1,
    });
    gsap.to(canvas.group.rotation, {
      x: Math.PI * -0.14 - this.cursor.y * 0.04,
      y: Math.PI * -0.34 - this.cursor.x * 0.04,
      duration: 1,
    });
 }


Now the question is: how can I combine the mouse movement and ScrollTrigger animation?

This is an example of what I want to achieve:
https://crosswire.unseen.co/

As you can see there is a mouse movement effect where some objects of the scene are moving and the scroll started always from the right position. 

 

Thanks in advance for your help!

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi @GSAP Helper this is a very basic example of the issue.

 

See the Pen GRPQeYa by fedeweb (@fedeweb) on CodePen

 

As you can see there is a blue box animated with ScrollTrigger and there is also a mousemove listener which animate the box. If I scroll and then I move the mouse the box go back to the initial position, I need to sync the two actions, so the box has to stay in the right position during the scroll and at the same time I can move the mouse.

 

Thanks for your help!

Link to comment
Share on other sites

It is certainly possible to have to separate tweens control the same element, but this requires A LOT of logic tweaking to get it to work, a much easier approach is to wrap the element in another element and tween the two separate elements. Hope it helps and happy tweening! 

 

See the Pen OJrQYaw?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen

  • Like 1
Link to comment
Share on other sites

Hi @mvaneijgen,

 

thanks for your answer. So going back to my first message where my first need was to tween a group of meshes, the easy solution could be to create another group (for ex. groupWrap) and add my group of meshes to the groupWrap.

 

In this way I can tween the position and rotation of the inner group on mouse move, and animate the groupWrap with ScrollTrigger, sounds good?

 

Thanks!

Link to comment
Share on other sites

I think so? It is a bit hard to give advise without seeing the issue you’re facing.
 

Depending on the effect you’re going for you could wrap your elements in a container or wrap each individual element in its own container. 
 

I would just try it out on codepen and post back here if you’re facing an issue

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...