Jump to content
Search Community

Draggable dial with images always upright

skylarkitchen test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

  • Solution

Hi,

ย 

Just don't rotate the images in the initial setup so all keep a zero degrees rotation and then just use a Quick Setter in order to update their rotation using the onDrag and onThrowUpdate callbacks:

https://gsap.com/docs/v3/GSAP/gsap.quickSetter()

ย 

const rotationSetter = gsap.quickSetter(images, "rotation", "deg");

function setup() {
  let radius = wheel.offsetWidth / 2,
      center = radius,
      slice = 360 / images.length,
      DEG2RAD = Math.PI / 180;
  gsap.set(images, {
    x: i => center + radius * Math.sin(i * slice * DEG2RAD),
    y: i => center - radius * Math.cos(i * slice * DEG2RAD),
    xPercent: -50,
    yPercent: -50
  });
}

setup();

window.addEventListener("resize", setup);
Draggable.create(wheel, {
  allowContextMenu: true,
  type: "rotation",
  trigger: wheel,
  inertia: true,
  snap: {
    rotation: gsap.utils.snap(360 / images.length)
  },
  onDrag() {
    rotationSetter(-this.rotation);
  },
  onThrowUpdate() {
    rotationSetter(-this.rotation);
  }
});

Here is a fork of our demo:

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

ย 

Hopefully this helps.

Happy Tweening!

  • Thanks 1
Link to comment
Share on other sites

Hi,

ย 

Just create a timeline instance that rotates the wheel and the images and just kill it in the onPress callback:

const tl = gsap.timeline({
  repeat: -1,
  defaults: {
    duration: images.length,
    ease: "none",
  }
})
.to(wheel, { rotation: -360 })
.to(images, { rotation: 360 }, 0)

window.addEventListener("resize", setup);
Draggable.create(wheel, {
	allowContextMenu: true,
	type: "rotation",
	trigger: wheel,
	inertia: true,
  snap: {
    rotation: gsap.utils.snap(360 / images.length)
  },
  onPress() {
    // Kill the timeline here
    tl.kill();
  },
  onDrag() {
    rotationSetter(-this.rotation);
  },
  onThrowUpdate() {
    rotationSetter(-this.rotation);
  }
});

That should work the way you intend.

ย 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

  • 2 weeks later...

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