@vbarbarosh/dd
v1.9.0
Published
An abstraction for working with the mouse
Downloads
547
Readme
An abstraction for working with the mouse.
dd({
event, // Pass original event object in
begin: function (ctx) {
console.log('The beginning');
},
end: function (ctx) {
console.log('The end');
},
move: function (ctx) {
console.log(`Position: x=${ctx.x} y={ctx.y}; distance: dx=${ctx.dx} dy=${ctx.dy}`);
},
});
Installation
npm i @vbarbarosh/dd
Using from browser
<script src="https://unpkg.com/@vbarbarosh/[email protected]/dist/dd.js"></script>
Description
This abstraction comes from the following observations:
Each interaction with the mouse has the following workflow:
mousedown -> mousemove ... mousemove -> mouseup
(here is
context.begin
,context.end
, andcontext.move
).Almost always what you are really looking for is how far a mouse was moved from the beginning (here is
context.dx
andcontext.dy
).In general, you have to work with 2 coordinate system: the screen coordinates, and local coordinates. And the only thing you are interested in is your local coordinates. Or, in other words, you have to convert coordinates from screen to local (here is
context.translate
).
Gotchas
IFRAME
andBUTTON[disabled]
stops propagation of mouse events. As a resultdd
will not callbegin
notupdate
handlers (Events and disabled form fields).
Resources
- https://github.com/STRML/react-grid-layout
- https://github.com/STRML/react-draggable
- https://github.com/STRML/react-resizable
- https://github.com/AlexeyBoiko/DgrmJS
- https://www.youtube.com/watch?v=IEZ2bdMbSwI
- http://dbushell.github.io/Nestable