Jump to content
Search Community

Draggable joystick

naxiya1778 test
Moderator Tag

Go to solution Solved by ZachSaucier,

Recommended Posts

Hello, I'm using Draggable to be able to drag a map with the mouse, but what I'm looking for is to create a floating joystick (with buttons up, down, right and left), so when user press "up" it can simulate the dragging of the mouse up and so on. It's possible?

 

im just using this:

 

  Draggable.create("#map", {
      type:"x,y",
      bounds:"#wrapper",        
  });

 

Thank you :(

Link to comment
Share on other sites

Hey naxiya1778 and welcome to the GreenSock forums. 

 

Draggable affects the x and y transform properties of the target(s), in this case your map. To add buttons that affect the same properties, you simply need to add click event listeners on the buttons and transform the same target(s) of your Draggable in whichever direction is appropriate. 

Link to comment
Share on other sites

Thankyou Zach :)

 

For example:

I have this button:

 

<span id="button-up"> UP </span>

 

and in JS:

 

document.getElementById("button-up").addEventListener("click", function(){

 

});

 

Wich event could be used inside de event listener to simulate de "X" up drag?

Can you help me?

 

With this in mind i can replicate to down, and "y" direction 😃

 

Thank you.

Link to comment
Share on other sites

  • Solution

Here's some untested code:

var btnUp = document.getElementById("button-up");
var tween;
btnUp.addEventListener("mousedown", function() {
  tween = gsap.to("#map", {
    yPercent: "-=10",
    duration: 1,
    repeat: -1,
    repeatRefresh: true
  });
});
btnUp.addEventListener("mouseup", function() {
  tween.kill();
});

 

  • Like 1
Link to comment
Share on other sites

Dear Zach,

One last question.

 

It works fine, but when dragging, the map travels more than it should and leaves a blank space, it is possible to limit it to a wrapper (in draggable i use this property:  bounds:"#wrapper" ) but in gsap.to  how can i fix this? 🤔

Link to comment
Share on other sites

Zach, im applying this:

 

var mapdrag = document.getElementById("map");

 

  tween = gsap.to("#map img", {
    y: "+=10",
    duration: 1,
    repeat: 0,
    repeatRefresh: true,
    modifiers: {
         y: gsap.utils.unitize( gsap.utils.wrap(0, (mapdrag.height)), "px") 
    }
  });

 

But it dissapear the map :( 

 

also  i tried : .startDrag() but it get stuck.

The maps is just an image, thats why i check the height.

 

without being able to achieve it

 

Another clue? :(

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