Convert Figma logo to code with AI

hammerjs logohammer.js

A javascript library for multi-touch gestures :// You can touch this

24,314
2,615
24,314
320

Top Related Projects

6,221

Unopinionated utilities for resizeable split views

8,726

The world's most versatile desktop notifications framework :earth_americas:

22,200

:ok_hand: Drag and drop so simple it hurts

16,591

:love_hotel: Cascading grid layout plugin

3,632

🚀 Performance focused, lightweight scroll animation library 🚀

Quick Overview

Hammer.js is a popular JavaScript library for adding multi-touch gestures to web applications. It provides a simple and intuitive API for recognizing various touch interactions, including taps, swipes, pinches, and rotations, across different devices and browsers.

Pros

  • Easy to use and implement with a clean, straightforward API
  • Supports a wide range of touch gestures and custom gesture recognition
  • Works across multiple devices and browsers, ensuring consistent behavior
  • Lightweight and performant, with minimal impact on application performance

Cons

  • Limited built-in support for complex multi-touch interactions
  • Some users report occasional issues with gesture recognition accuracy
  • Documentation could be more comprehensive, especially for advanced use cases
  • Maintenance and updates have slowed down in recent years

Code Examples

  1. Basic tap recognition:
const element = document.getElementById('myElement');
const hammer = new Hammer(element);

hammer.on('tap', function(ev) {
    console.log('Tap detected');
});
  1. Recognizing swipe gestures:
const hammer = new Hammer(document.body);

hammer.on('swipe', function(ev) {
    console.log('Swipe direction:', ev.direction);
});
  1. Pinch zoom recognition:
const hammer = new Hammer(document.body);
hammer.get('pinch').set({ enable: true });

hammer.on('pinch', function(ev) {
    console.log('Pinch scale:', ev.scale);
});

Getting Started

  1. Install Hammer.js using npm:
npm install hammerjs
  1. Import and initialize Hammer.js in your JavaScript file:
import Hammer from 'hammerjs';

const element = document.getElementById('myElement');
const hammer = new Hammer(element);

hammer.on('pan', function(ev) {
    console.log('Pan detected');
});

hammer.on('swipe', function(ev) {
    console.log('Swipe detected');
});

hammer.on('pinch', function(ev) {
    console.log('Pinch detected');
});

This basic setup allows you to start recognizing pan, swipe, and pinch gestures on the specified element. You can customize and add more gesture recognizers as needed for your application.

Competitor Comparisons

6,221

Unopinionated utilities for resizeable split views

Pros of Split

  • Focused on split views and resizable panels, making it more specialized for this specific use case
  • Lightweight and simple to implement for splitting layouts
  • Supports both horizontal and vertical splits out of the box

Cons of Split

  • Limited to split view functionality, lacking the broader touch gesture support of Hammer.js
  • Smaller community and ecosystem compared to Hammer.js
  • Less frequent updates and maintenance

Code Comparison

Split example:

Split(['#left', '#right'], {
  sizes: [25, 75],
  minSize: 100,
  gutterSize: 10,
  cursor: 'col-resize'
});

Hammer.js example:

var hammer = new Hammer(myElement);
hammer.on('pan', function(ev) {
  console.log(ev.deltaX, ev.deltaY);
});

Key Differences

Split is specifically designed for creating resizable split views in web applications, while Hammer.js is a more general-purpose touch gesture library. Split is ideal for projects that require split layouts, such as code editors or file managers. Hammer.js, on the other hand, offers a wide range of touch gestures and is better suited for applications needing complex touch interactions.

Split has a smaller footprint and is easier to implement for its specific use case, but it lacks the versatility of Hammer.js. Hammer.js has a larger community, more frequent updates, and broader browser support, making it a more robust choice for general touch gesture handling.

8,726

The world's most versatile desktop notifications framework :earth_americas:

Pros of Push.js

  • Focused specifically on browser notifications, making it simpler for this use case
  • Supports both desktop and mobile browsers
  • Smaller file size, leading to faster load times

Cons of Push.js

  • Limited to notification functionality, unlike Hammer.js's broader touch gesture support
  • Less active community and fewer contributors
  • Fewer integrations with other libraries and frameworks

Code Comparison

Push.js (Sending a notification):

Push.create("Hello world!", {
    body: "How's it hanging?",
    icon: '/icon.png',
    timeout: 4000,
    onClick: function () {
        window.focus();
        this.close();
    }
});

Hammer.js (Adding a tap gesture):

var hammertime = new Hammer(myElement);
hammertime.on('tap', function(ev) {
    console.log('tap!');
});

While both libraries serve different purposes, this comparison highlights their core functionalities. Push.js focuses on creating browser notifications, while Hammer.js specializes in touch gestures. The code examples demonstrate the simplicity of using each library for its primary purpose.

22,200

:ok_hand: Drag and drop so simple it hurts

Pros of Dragula

  • Focused specifically on drag-and-drop functionality, making it simpler to implement for this use case
  • Lightweight and has minimal dependencies, resulting in a smaller footprint
  • Provides seamless touch support without additional configuration

Cons of Dragula

  • Limited to drag-and-drop interactions, while Hammer.js supports a wider range of touch gestures
  • Less customizable for complex gesture recognition scenarios
  • Smaller community and ecosystem compared to Hammer.js

Code Comparison

Dragula:

dragula([document.getElementById('left'), document.getElementById('right')])
  .on('drag', function (el) {
    el.className += ' is-moving';
  })
  .on('drop', function (el) {
    el.className = el.className.replace('is-moving', '');
  });

Hammer.js:

var hammertime = new Hammer(myElement);
hammertime.on('pan', function(ev) {
  console.log(ev.type +" gesture detected.");
});

Dragula focuses on drag-and-drop operations with a simple API, while Hammer.js provides a more comprehensive gesture recognition system. Dragula's code is more concise for basic drag-and-drop functionality, whereas Hammer.js offers greater flexibility for various touch interactions.

16,591

:love_hotel: Cascading grid layout plugin

Pros of Masonry

  • Specialized for grid layouts, offering advanced features for responsive, dynamic grids
  • Lightweight and focused on a specific use case, potentially leading to better performance
  • Extensive documentation and examples for various grid scenarios

Cons of Masonry

  • Limited to grid layouts, less versatile for general-purpose UI interactions
  • May require additional plugins or libraries for touch events and gestures
  • Less frequent updates and maintenance compared to Hammer.js

Code Comparison

Masonry:

var grid = document.querySelector('.grid');
var msnry = new Masonry(grid, {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Hammer.js:

var element = document.getElementById('myElement');
var mc = new Hammer(element);
mc.on("pan", function(ev) {
  console.log(ev.type +" gesture detected.");
});

Key Differences

  • Masonry focuses on creating dynamic grid layouts, while Hammer.js specializes in touch gestures and interactions
  • Masonry is more suitable for content-heavy websites requiring organized layouts, whereas Hammer.js is ideal for interactive web applications
  • Hammer.js offers a wider range of gesture recognitions, making it more versatile for various touch-based interactions

Use Cases

  • Choose Masonry for: Portfolio websites, image galleries, or content-rich layouts requiring organized grid structures
  • Choose Hammer.js for: Mobile web apps, interactive user interfaces, or any project requiring advanced touch gesture support
3,632

🚀 Performance focused, lightweight scroll animation library 🚀

Pros of Sal

  • Lightweight and focused on scroll animations
  • Easy to use with data attributes for configuration
  • No dependencies, vanilla JavaScript

Cons of Sal

  • Limited to scroll animations only
  • Less mature and smaller community compared to Hammer.js
  • Fewer advanced features and customization options

Code Comparison

Sal:

sal({
  threshold: 0.5,
  once: false,
});

Hammer.js:

var hammer = new Hammer(element);
hammer.on('pan', function(ev) {
  console.log(ev.type +" gesture detected.");
});

Key Differences

Sal is specifically designed for scroll animations, making it simpler to implement scroll-based effects. Hammer.js, on the other hand, is a more comprehensive touch gesture library that supports a wide range of interactions beyond scrolling.

Sal uses a declarative approach with data attributes, while Hammer.js relies on JavaScript for configuration and event handling. This makes Sal easier to use for simple scroll animations, but Hammer.js offers more flexibility for complex gesture recognition.

Hammer.js has a larger user base and more extensive documentation, which can be beneficial for larger projects or when dealing with complex touch interactions. Sal, being more focused, may be preferable for projects that only require scroll-based animations and want to keep the codebase lightweight.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

hammer.js NPM Version NPM Downloads Build Status

A JavaScript library for detecting touch gestures.

Installation

NPM

npm install --save hammerjs

or

Yarn

yarn add hammerjs

or

CDN

https://cdnjs.com/libraries/hammer.js/

Usage

hammer.js has a quick start option for gestures it already recognizes.

// Get a reference to an element.
var square = document.querySelector('.square');

// Create an instance of Hammer with the reference.
var hammer = new Hammer(square);

// Subscribe to a quick start event: press, tap, or doubletap.
// For a full list of quick start events, read the documentation.
hammer.on('press', function(e) {
  e.target.classList.toggle('expand');
  console.log("You're pressing me!");
  console.log(e);
});

If you want to recognize your own gestures, such as tripletap, then you'll have to use these steps:

// Get a reference to an element.
var square = document.querySelector('.square');

// Create a manager to manage the element.
var manager = new Hammer.Manager(square);

// Create a recognizer.
var TripleTap = new Hammer.Tap({
  event: 'tripletap',
  taps: 3
});

// Add the recognizer to the manager.
manager.add(TripleTap);

// Subscribe to the event.
manager.on('tripletap', function(e) {
  e.target.classList.toggle('expand');
  console.log("You're triple tapping me!");
  console.log(e);
});

Examples

Documentation

For further information regarding hammer.js, please read our documentation.

Contributions Github Issues Github PRs Slack

Feel encouraged to report issues or submit pull requests. When you're ready to do either, read our contribution guidelines. If you're looking for another form of contribution, we love help answering questions on our slack channel.

License

MIT

NPM DownloadsLast 30 Days