1

I'd like the custom cursor to move with the mouse during the drag event of the carousel without the awkward "jump" back to its starting position, at the end of the drag. I'm using GSAP's quickSetter for the cursor and Flickity for the carousel. Here is a minimal --> Codepen that shows the issue.

I've managed to use the:

flkty.on( 'dragMove', function( event, pointer, moveVector ) {...});

so that the cursor moves with the mouse during the drag, however, when the drag ends, the new cursor position is not updated, so it jumps back to its starting position. I need a way if storing/updating the position after drag.

gsap.set(".ball", {xPercent: -50, yPercent: -50});

const ball = document.querySelector(".ball");
const pos = { x: window.innerWidth / 2, y: window.innerHeight / 2 };
const mouse = { x: pos.x, y: pos.y };
const speed = 0.35;
var active = false;

const xSet = gsap.quickSetter(ball, "x", "px");
const ySet = gsap.quickSetter(ball, "y", "px");

window.addEventListener("mousemove", e => {    
  mouse.x = e.x;
  mouse.y = e.y;  
});

gsap.ticker.add(() => {
  if(!active){
    const dt = 1.0 - Math.pow(1.0 - speed, gsap.ticker.deltaRatio()); 
    pos.x += (mouse.x - pos.x) * dt;
    pos.y += (mouse.y - pos.y) * dt;
    xSet(pos.x);
    ySet(pos.y);
  }
});

//--Flickity--//

var $carousel = $('.carousel').flickity();

$carousel.on( 'dragMove.flickity', function(mousemove, pointer, moveVector) {
    
  mouseX = mousemove.clientX;
  mouseY = mousemove.clientY;
  console.log(mouseX)
  
  gsap.to({}, 0.0, {
    onUpdate: function() {
      gsap.set(ball, {
        x: mouseX,
        y: mouseY
      })
    }
  })
  
  active = true;
  
});

$carousel.on( 'dragEnd.flickity', function() {
  active = false;   
});
Nish
  • 81
  • 6
  • 1
    Just update the mouse position inside of the flickity listener... `pos.x = mouseX; pos.y = mouseY;` – Zach Saucier Jan 24 '21 at 19:47
  • 1
    This post [was answered on the GreenSock forums](https://greensock.com/forums/topic/26913-how-to-updatestore-cursor-position-during-drag-event/) – Zach Saucier Jan 25 '21 at 14:26

0 Answers0