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

pan

v1.1.6

Published

Touch enabled implementation of WHATWG drag and drop mechanism.

Downloads

25

Readme

NPM version Bower version

Touch enabled implementation of WHATWG drag and drop (aka HTML drag and drop) mechanism.

new Pan(targetElement);

This will give targetElement a draggable property and set an event listener for dragstart, drag and dragend; it will also set an event listener for touchstart, touchmove and touchend. These events are re-emitted through the eventEmitter and allow you to animate handle in response to drag/touch events.

HTML drag and drop spec does not define a method for styling or animating the handle in response to the drag event, e.g. there is no way to control element opacity while dragging.

The following mechanism is used to get the full control of the visual feedback:

In the event of dragstart and touchstart:

  1. Make a clone (handle) of the target element.
  2. Take the target element out of the flow (i.e., hide).
  3. Insert the handle element in place of the target element.

In the event of drag and touchmove:

  1. Make a reference to the handle element available to the event object.

In the event of dragend and touchend:

  1. Remove the handle element.
  2. Restore the target element.

As a result, you have full control over the visual feedback.

There is a reference to the handle element in the Event Object.

  • Access to all of the DOM events for drag and touch.
  • Lightweight and performs good.

To make element draggable:

var targetElement,
    pan;

targetElement = document.querySelector('#target-element');
pan new Pan(targetElement);

pan.eventEmitter.on('start', function (e) {
    console.log(e);
});

pan.eventEmitter.on('move', function (e) {
    e.handle.style.transform = 'translate(' + e.offsetX + 'px,' + e.offsetY + 'px)';
});

pan.eventEmitter.on('end', function (e) {
    console.log(e);
});

This will make the #target-element element draggable using CSS3 transformations.

The result of Pan() is an object with a single property (eventEmitter) used to emit events.

  • Basic element dragging using CSS transformations.
  • Events logged in the console.log.

The code for all of the examples is in the examples folder.

Raise an issue if you are missing an example.

You can listen for individual drag and touch DOM events:

targetElement.addEventListener('dragstart', dragStart);
targetElement.addEventListener('drag', dragMove);
targetElement.addEventListener('dragend', dragEnd);

targetElement.addEventListener('touchstart', dragStart);
targetElement.addEventListener('touchmove', dragMove);
targetElement.addEventListener('touchend', dragEnd);

You can use the eventEmitter object that will normalize drag and touch events to:

pan.eventEmitter.on('start', dragStart);
pan.eventEmitter.on('move', dragMove);
pan.eventEmitter.on('end', dragEnd);

The listener of the eventEmitter is passed a single eventObject object.

pan.eventEmitter.on('move', function (eventObject) {
    
});

| Name | Value | | --- | --- | | offsetX | Distance from the starting point on X axis. | | offsetY | Distance from the starting point on Y axis. | | target | Target that received the event. | | handle | Element used to represent the target element when dragging. | | type | Event name. |

Using Bower:

bower install pan

Using NPM:

npm install pan-drag

The old-fashioned way, download either of the following files:

  • https://raw.githubusercontent.com/gajus/pan/master/dist/pan.js
  • https://raw.githubusercontent.com/gajus/pan/master/dist/pan.min.js