npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mr-tap

v0.1.2

Published

A tap library, abstracts away from mouse/touch providing source agnostic sync access to the user input.

Downloads

3

Readme

mr-Taps

Taps provide source agnostic sync access to input. Either it comes from mouse and/or touch, it is the same API. It assumes multiple instances of taps making your code multi-touch by design.

Providing sync access instead of event based, for best usage in real-time applications.

GitHub license

:rocket: Install

module: https://cdn.jsdelivr.net/npm/mr-tap/dist/mr-tap.module.min.js
nomodule: https://cdn.jsdelivr.net/npm/mr-tap/dist/mr-tap.min.js

npm install mr-observer
<script type='module' src='mr-tap.module.min.js'></script>
<script nomodule src='mr-tap.min.js'></script>

Use built files from dist directory for browser. It will load ES8+ version if it is supported (~94%). There are two versions: nomodule (global scope) and module (for import).

// create taps interface
const taps = new Taps();

function update() {
    requestAnimationFrame(update);

    // update taps on each frame
    taps.update();

    // access all taps
    for(const tap of taps) {
        // iterate through all taps
    }
}
requestAnimationFrame(update);

:scroll: API Documentation

Usage

Creating:

const taps = new Taps();

Updating:

function update() {
    requestAnimationFrame(update);

    // update taps on each frame
    taps.update();

    // application logic
}
requestAnimationFrame(update);

Accessing:

All taps:

// Taps - is an iterator
for(const tap of taps) {
    // all taps
}

Specific state taps, e.g. clicks:

for(const tap of taps.click) {
    // taps that are only clicks
}

Unique to mouse properties are also available:

for(const tap of taps) {
    if (tap.mouse && tap.button === 0) {
        // applies to only taps originated from mouse
        // and only from left mouse button
    }
}

Examples

Examples are multi-touch by design.

Clicking on objects:

for(const tap of taps.click) {
    const object = pickObject(tap.x, tap.y);
    if (! object) continue;
    object.interact();
}

Rendering taps:

// drawCircle(x, y, radius)

for(const tap of taps) {
    drawCircle(tap.x, tap.y, 32);
}

Selecting multiple objects using rect (RTS style):

// drawRect(left, top, right, bottom)

// draw selection rect
for(const tap of taps.drag) {
    drawRect(tap.sx, tap.sy, tap.x, tap.y);
}

// select objects on dragend
for(const tap of taps.dragend) {
    const objects = objectsInRect(tap.sx, tap.sy, tap.x, tap.y);
    // selected some objects
}

Dragging objects:

// pick an object from tap start position
// and remember against tap object
for(const tap of taps.dragstart) {
    const object = pickObject(tap.sx, tap.sy);
    if (! object) continue;

    tap.object = object;
    // remember difference of position between tap and object center
    tap.offsetX = object.x - tap.sx;
    tap.offsetY = object.y - tap.sy;
}

// position objects
for(const tap of taps.drag) {
    // tap might have no object associated
    if (! tap.object) continue;
    // position with offset to prevent object snapping
    tap.object.setPosition(tap.x + tap.offsetX, tap.y + tap.offsetY);
}

Throwing objects:

// pick objects
// position objects

// throw an object
for(const tap of taps.dragend) {
    // tap might have no object associated
    if (! tap.object) continue;
    // set linear velocity
    // multiply speed by deltaTime
    tap.object.setLinearVelocity(tap.dx * dt, tap.dy * dt);
}

Tap

Each tap is instantiated by the Taps interface, and provided through iterators. Tap is agnostic to the input source: mouse or touch. And behaves identical. Also, due to iterator pattern, it provides sync access instead of event driven, and is multi-touch by design.

Tap has states with two branching scenarios:

Click: start > click + up
Drag: start > dragstart + drag > drag > dragend + drag + up

States:

start - every tap starts with a start state.

click - some taps if not moved from initial position and have not been held for too long, on up state, will be considered click.
dragstart - some taps if moved from initial position or have been held for some time, will enter drag state, and once be in dragstart state.
drag - once tap is in dragstart state, it will be in drag state till the end of a tap.
dragend - if tap is in drag state, on up state instead of click it will be in dragend state.

up - every tap ends with up state.

Drag State

Tap enters drag state if moved far from start position, or held for long enough. It is possible to change default settings of 20 pixels and 200 millisecsonds:

taps.dragDistance = 20; // pixels
taps.dragDelay = 200; // milliseconds

Building

Builds library into two versions (normal and module) using Rollup, Babel and Terser.
Source file: src/index.js
Built versions normal (dist/mr-tap.min.js) and module (dist/mr-tap.module.min.js):

npm install
npm run build