Code Monkey home page Code Monkey logo

premonish's Introduction

premonish

Predict which DOM element a user will interact with next.

View the demo

About

What does it do?

You give it a list of elements and it will try to predict when a user is about to mouse over one of those elements.

How does it work?

It's pretty naive, it just looks at the velocity and position of the mouse and tries to find the element that you are probably moving towards based on that.

Is it just a voronoi?

It uses a voronoi under the hood, but instead if looking at current mouse position it looks at expected mouse position. In my testing this seems strictly better than the voronoi / bubble cursor technique.

Why do I want this?

You could use it to perform some optimizations, maybe similar to instantclick.

Whats with the name?

The library has these premonitions, but they aren't always right.

Installation

$ npm install premonish

Usage

import Premonish from 'premonish';
const premonish = new Premonish({
  selectors: ['a', '.list-of' '.selectors', '.to', '#watch'],
  elements: [] // Alternatively, provide a list of DOM elements to watch
});

premonish.onIntent(({el, confidence}) => {
  console.log(el); // The DOM node we suspect the user is about to interact with.
  console.log(confidence); // How confident are we about the user's intention? Scale 0-1
});

API

Initialization

var premonish = new Premonish({
  selectors: [], // list of selectors
  elements: [] // A list of DOM elements
});

At least one of selectors or elements is required.

Predicting interaction

The onIntent callback will be called when premonish thinks that a user is likely going to interact with one of the watched elements.

premonish.onIntent(({el, confidence}) => {
  // el is the expected DOM element
  // confidence is a score from 0-1 on how confident we are in this prediction.
});

More information

The onMouseMove callback allows users to look at some of the internal calculations that premonish is making.

premonish.onMouseMove(({ position, velocity, expected }) => {
  // Each value is an object { x: number, y: number }.
  // `expected` is the approximate point premonish thinks the
  // user is moving the mouse to.
});

Stop watching

Call stop() when you are done using the library to cleanup the events it is using.

premonish.stop();

Author

Matthew Conlen ([email protected])

LICENSE

MIT

premonish's People

Contributors

imakewebthings avatar mathisonian avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

guoyu07

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.