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

mouse-signal

v2.1.0

Published

Turn mouse events for a region into a simple queryable data structure

Downloads

1

Readme

MouseSignal

Wrap mouse events emitted by the browser and transform them into a data structure that can be queried in an intuitive way.

Queries

Queries are simple. Ask the mouse signal instance for their mode and duration that a button has been held down for.

API

This package exports a single constructor. You can create a global listener and attach it to the body or create a local listener and attach it only to some specific dom node or both! The module also exports a function that you will need to call yourself in your "update-loop" for this system to behave as expected.

var ms = new MouseSignal(HTMLElement) // This is most often your document body

function update () {
  MouseSignal.update(dT, kbs) // dT is determined by you.  pass 0 if you don't care about downDuration for keys
  requestAnimationFrame(update)
}

Example usage

var ms = new MouseSignal(document.body)
var last = Date.now()
var current = Date.now()
var dT = current - last

for (var key in ms.eventListeners) {
  document.body.addEventListener(key, ms.eventListeners[key])
}

return function update (dT) {
  last = current
  current = Date.now()
  dT = current - last

  MouseSignal.update(dT, ms)

  if (ms.left.mode.JUST_UP && ms.left.downDuration >= 0) console.log('fire the missiles')

  requestAnimationFrame(update)
}

FAQ

Is there a similar module for handling the keyboard?

Yes there is. The sister module KeyboardSignal will handle keyboard events.

Why do I have to provide a dT parameter myself?

This application assumes you're running your code with a run-loop. It also assumes you likely already have a clock-like mechanism defined according to your preferences. Thus, you are expected to proude and provide the dT parameter if you would like to be able to track the duration a key has been held down. If you don't care about that feature, just pass 0 and the system will not track downDuration for keys.

Why do I have to bind the eventListeners myself?

I do not want to require you to tear this system down using a custom destructor or any other adhoc system like that. JS has no standardized approach to freeing resources and I don't wish to impose one on you. Finally, and more importantly, I want you to be free to choose HOW and WHEN to bind your event listeners. For example, in React.js or a similar Virtual DOM library, you may want to bind them in your React components and allow React itself to unbind them when that component is no longer being rendered. An example of this common case is included below.

import React from 'react'
import DOM from 'react-dom'

const ms = new MouseSignal(document.body)

DOM.render(<div {...ms.eventListeners} id="app-root"></div>, document.body)