@vbarbarosh/dd

1.3.0 • Public • Published

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/dd@1.0.6/dist/dd.js"></script>

Description

This abstraction comes from the following observations:

  1. Each interaction with the mouse has the following workflow:

    mousedown -> mousemove ... mousemove -> mouseup
    

    (here is context.begin, context.end, and context.move).

  2. Almost always what you are really looking for is how far a mouse was moved from the beginning (here is context.dx and context.dy).

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

Links

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i @vbarbarosh/dd

    Weekly Downloads

    24

    Version

    1.3.0

    License

    MIT

    Unpacked Size

    16.6 kB

    Total Files

    6

    Last publish

    Collaborators

    • vbarbarosh