I usually work with the philosophy of “code now, fix later”. However, in my recent work, I’ve been trying to plan out my code ahead of time. So my question right now is, what’s the best way for working with mouseDragged() inside a class?
In the past, I just put a little bit of the detection in mousePressed(), a little in mouseDragged(), and the rest bouncing around in global variables. But I figure there’s got to be a better way. Here’s some of my spaghetti code if you want to take a look.
I have considered using p5.Elements to access their mouseOver() and mousePressed() functions, but unlike Processing, they don’t have a mouseDragged() method.
The Question:
Can I detect mouse-stuff within a class definition?
Anyways, my eventual hope is to have a class that implements the kind of drag-and-drop interface in the sketch above all in one neat class definition.
({ mouseX: pmouseX, mouseY: pmouseY } = window);
is equivalent to: pmouseX = window.mouseX, pmouseY = window.mouseY;
and b/c prefixing w/ window is optional, just: pmouseX = mouseX, pmouseY = mouseY;
That technique is called “Object Destructuring Assignment”, btW:
This is just a temporary workaround though.
p5js fails to update pmouseX & pmouseY for p5.Element mouse callbacks!
I believe they might be fixing it now, not sure.
I was looking at the p5.Element reference, and they mention that graphics buffers are considered elements. How does that work? I messed around a little with a PGraphic below: