Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add draggable() feature to p5.Element #6381

Merged
merged 3 commits into from
Nov 23, 2023
Merged

Conversation

ffd8
Copy link
Contributor

@ffd8 ffd8 commented Aug 27, 2023

Resolves #6248

Changes:
As discussed, adds a very simple 'p5.js' way to make DOM elements draggable without the need for additional libraries.
This works for both the element itself or using elm to move a parent container.

Basic example added to documentation) here:
https://editor.p5js.org/ffd8/sketches/c-gRg5Rv-

PR Checklist

  • npm run lint passes
  • [Inline documentation] is included / updated
  • [Unit tests] are included / updated

@Qianqianye
Copy link
Contributor

Qianqianye commented Aug 28, 2023

Thanks @ffd8! Looks good to me. Will merge it after @limzykenneth approves it.

src/dom/dom.js Outdated
pos4 = 0;
if (elmnt !== this.elt && elmnt.elt !== this.elt) {
elmnt = elmnt.elt;
this.elt.onmousedown = dragMouseDown;
Copy link
Contributor

@davepagurek davepagurek Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about using addEventListener to avoid overwriting the onmousedown property if something else on a page has set it? Probably more for when we do it to document than to an element, but it might have less unintended side effects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davepagurek Aha - good catch! especially since the implementation was also using document.onmouseup and document.onmousemove, which very well could run into issues with other interactions on the page. Here's a new example with listeners which runs great = will update code and PR now...

src/dom/dom.js Outdated
e = e || window.event;
pos1 = pos3 - parseInt(e.clientX);
pos2 = pos4 - parseInt(e.clientY);
pos3 = parseInt(e.clientX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can give these some more descriptive names so future maintainers will be able to read it more easily. Maybe prevX/Y and deltaX/Y or differenceX/Y?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea – was paying homage to where the code came from, but makes sense to update.

src/dom/dom.js Outdated
* }
* </code></div>
*/
p5.Element.prototype.draggable = function (elmnt = this.elt) {
Copy link
Contributor

@davepagurek davepagurek Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might simplify the if statement below if the default value is this -- that way the input is always a p5.Element.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a while to figure out/undo my weird if logic – but solved it!

src/dom/dom.js Outdated
@@ -2421,6 +2421,93 @@ p5.Element.prototype.drop = function (callback, fxn) {
return this;
};

/**
* Turns p5.Element into a draggable item with the mouse. If argument is given, it will drag a different p5.Element (parent container) instead, ie. for a GUI title bar.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can describe the example's behaviour in a bit more detail here, something like: this lets you drag the title bar of a panel to move the whole panel around

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted and updated

src/dom/dom.js Outdated
this.elt.addEventListener('mousedown', dragMouseDown, false);
this.elt.style.cursor = 'move';
} else {
elmnt.addEventListener('mousedown', dragMouseDown, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying this out on my phone and noticed (at least in the p5 editor) that dragging doesn't work. do you think we need to also add a fallback pointer event listener for mobile support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point – I was only testing on desktop, nevertheless got it working for touch as well now!

@Qianqianye
Copy link
Contributor

Hi, just following up on this PR. @ffd8, could you take a look at the comments from @davepagurek? @limzykenneth, let us know if you have any comments. Thanks.

@ffd8
Copy link
Contributor Author

ffd8 commented Nov 21, 2023

@Qianqianye Hihi – this fell to the back burner since there wasn't time to make requested changes – nevertheless, had a close look and implemented all suggestions above. Hope it's now ready to make into the next release!

Perhaps not the place, but is there a timeline for a minor release to address introduced with 1.8.0? Personal to me and my lil world: get() semi-breaks code of students exploring layers/webcam.

@limzykenneth
Copy link
Member

@ffd8 The plan is to do a patch release next Tuesday, we'll need to review any critical fixes need merging before then as well.

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making the updates!

@davepagurek davepagurek merged commit dcfe3bc into processing:main Nov 23, 2023
2 checks passed
@Qianqianye
Copy link
Contributor

Thanks @ffd8 for working on it, and @limzykenneth @davepagurek for reviewing. With this new feature added, the next release will be a minor version, which is 1.9.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow p5.element's to become draggable
4 participants