Git Product home page Git Product logo

mouser.js's Introduction

Mouser.js · GitHub license npm version CI PRs Welcome

Mouser.js is a Javascript helper for mouse move animations. Import the function, pass a list of listeners, and they will recieve a vector with the current mouse position ranging from 0 to 1 relative to the reference, and the global position (gx and gy). {x: 0.23, y: 0.7, gx: 235, gy: 367}

  • Zero-Dependencies: With a size of ~18kb (unpacked).
  • Minimal: Mouser.js just registers the listeners and pass the vector to them. What you do with those values is totally up to you.
  • Use Anywhere: Mouser.js doesn't make assumptions about your technology stack, so you can use mouser.js wherever you want.

Installation

Mouser.js has been designed for minimal and efficient usage. To avoid memory leaking and ensure efficiency, Mouser.js works as a singleton. It will only return a new object, only if the params field is used, otherwise it will return the same instance. Users are in charge of clearing the events with the removeEventListeners method. (See example below).

You can use Mouser.js as a <script> tag from a CDN (TO-DO), or as a mouser.js package on npm:

npm install --save mouser.js
import mouser from 'mouser.js'

// your code

or

const mouser = require('mouser.js')

Examples

Here is the first example to get you started:

import mouser from 'mouser.js'

const domElement = document.querySelector('#id')

mouser({
  reference: domElement, // optional, defaults to window
  listeners: [console.log]
})

This example will print the vector {x: number, y: number, gx: number, gy: number} to the console whenever the effect is triggered. You'll notice that we used a simple console.log method, but you can pass to the listener any synchronous function...

Using it with React

import React, { useState } from 'react';
import mouser from 'mouser.js';

function Example() {
  const [v, setV] = useState({x: 0, y: 0}); // you can pass any rest state

  useEffect(() => {
    mouser({
      listeners: [setV]
    });
    return () => mouser.removeEventListeners(); // clear all registered events
  }, [setV]);

  return (
    <div>
      <p>x: {v.x} - y: {v.y}</p>
      <p>global x: {v.gx} - global y: {v.gy}</p>
    </div>
  );
}

API

Params

Optional object the you can pass when calling the mouser.js function

import mouser from 'mouser.js';

const helper = mouser({
    // element from which the range values will be applied
    reference: Document | Window | HTMLElement // defaults to window

    // array of functions that accepts the x,y vector
    listeners: ((v: {x: number, y: number}) => void)[] // defaults to []

    // rate to apply the update of the returned vector
    refreshRate: number // defaults to 0

    // value that will be returned ONCE when mouse is out the reference
    restState: { x: number, y: number } // defaults to {x: 0, y: 0}
})

Methods

import mouser from 'mouser.js';

const helper = mouser();

// replaces the current reference
helper.addReference(el: Document | Window | HTMLElement);

// sets the reference to the default (window)
helper.removeReference();

// adds a listener to the current array of listeners
helper.addListener(listener: (v: {x: number, y: number}) => void);

// IMPORTANT clears all event listeners to avoid memory leaks
helper.removeEventListeners();

Rest State Behaviour

While you can provide a rest state to the values relative to the reference, the global values, when effect is not active it will maintain the last recorded position.

NOTE: as we are still in alpha, the restState parameter behaviour might change in the feature

Showcase

Articles

PRs welcome!

Contributing

The main purpose of this repository is to continue evolving Mouser.js, making it faster and easier to use. Development of Mouser.js happens in the open on GitHub, and I am are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving Mouser.js.

Contributing Guide

Just send a PR or report an issue and I will be happy to review.

"Buy Me A Coffee"

Good First Issues

To help you get your feet wet and get you familiar with our contribution process, we have a list of good first issues that contain bugs which have a relatively limited scope. This is a great place to get started.

License

Mouser.js is MIT licensed.

Stadistics

Note

This project has been heavily inspired by the Webflow approach to mouse move animations.

mouser.js's People

Contributors

luisorbaiceta avatar

Stargazers

Roman avatar  avatar  avatar

Watchers

 avatar

mouser.js's Issues

optional rest state

When mouse move effect is not active (cursor is out of the reference), Mouser.js sets the return vector to the rest state. While this is a good default, It would be amazing if users could opt-out of this behaviour and let mouser.js preserve the last recorded position.

return global mouse position along with vector

Right now mouser just returns a vector that describes the mouse position ranging from 0 to 1 simulating a percentage across the screen, but it will be super useful to add the global position of the mouse too.

Motivation

Mouser.js could be use to achieve custom cursor implementation

add testing

To ensure correct collaboration and contributions, library should be correctly tested.

use cases for Mouser.js

It would be amazing to add some nice examples of mouse move animations implemented with Mouser.js to help users understand the main goal of the library

add support for window leave events

Right now, mouser.js does not set the rest state when leaving the browser when window is set as a reference. While this might be a desired behavior in some cases, It should default as described.

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.