Skip to main content
Inspiring
March 31, 2021
Answered

Constraining drag and drop

  • March 31, 2021
  • 1 reply
  • 3227 views

I have a bar symbol that I need to constrain to vertical movement as well as just how far it can go up and down when dragged. I have been looking at the constrain js function but cannot quite figure it out. Here's the script without constraint. I think it is fairly simply to add a constraint function, but I'm not quite sure how.

var startDrag = startDragF.bind(this);
this.Bar1.addEventListener("pressmove", startDrag);
function startDragF(e){
var p = stage.globalToLocal(e.stageX, e.stageY);
e.currentTarget.x = p.x;
e.currentTarget.y = p.y;
}

 

This topic has been closed for replies.
Correct answer kglad

Sorry to aggravate you. I guess I worded it poorly. I said, "I was trying to constrain horizontal movement and allow the user to drag vertically." I meant I wanted them to only be able to move it veritcally. I'll give it a shot again with that full code. For one thing, I mistook your "var minX = xxx;" etc lines as examples in which the x's and y's needed numbers. Fingers crossed....


if you want only vertical movement with no constraints on the vertical range:

 

function startDragF(e){

var p = stage.globalToLocal(e.stageX, e.stageY);

e.currentTarget.y = p.y;

}
}

1 reply

Legend
March 31, 2021

Just use if then statements to check the coordinate values. if x > max value then make x equal to max value yadda yadda etc. Nothing hard about it.

Inspiring
March 31, 2021

Well, it's not hard if you have a good command of js. I'll give it a shot. Thank you.