What is hammerjs?
Hammer.js is a popular JavaScript library for handling touch gestures. It provides a simple way to recognize gestures made by touch, mouse, and pointer events, enabling developers to easily implement interactive features such as swiping, pinching, rotating, and pressing on web pages.
What are hammerjs's main functionalities?
Swipe Gesture
This code sample demonstrates how to detect left and right swipe gestures on an element. It initializes Hammer.js on a target element and listens for 'swipeleft' and 'swiperight' events.
var myElement = document.getElementById('myElement');
var mc = new Hammer(myElement);
mc.on('swipeleft swiperight', function(ev) {
console.log(ev.type + " gesture detected.");
});
Pinch Gesture
This example shows how to enable and detect pinch gestures. It sets up pinch gesture recognition on an element and logs the scale of the pinch gesture.
var myElement = document.getElementById('myElement');
var mc = new Hammer(myElement);
mc.get('pinch').set({ enable: true });
mc.on('pinch', function(ev) {
console.log("Pinch gesture detected with scale: " + ev.scale);
});
Rotate Gesture
This code snippet enables rotation gesture detection on an element. When a rotate gesture is detected, it logs the rotation angle.
var myElement = document.getElementById('myElement');
var mc = new Hammer(myElement);
mc.get('rotate').set({ enable: true });
mc.on('rotate', function(ev) {
console.log("Rotate gesture detected with angle: " + ev.rotation);
});
Other packages similar to hammerjs
interact.js
Interact.js is a JavaScript library for drag and drop, resizing, and multi-touch gestures with inertia and snapping. It's similar to Hammer.js in providing touch gesture support but also offers additional functionalities like drag-and-drop and element resizing.
touchswipe
TouchSwipe is a jQuery plugin for handling touch events like swipe, tap, pinch, and more. It's similar to Hammer.js in terms of providing touch gesture recognition but is specifically designed for use with jQuery.
Hammer.js 2.0.6
Support, Questions, and Collaboration
Documentation
Visit hammerjs.github.io for detailed documentation.
var stage = document.getElementById('stage');
var mc = new Hammer.Manager(stage);
var Rotate = new Hammer.Rotate();
mc.add(Rotate);
mc.on('rotate', function(e) {
var rotation = Math.round(e.rotation);
stage.style.transform = 'rotate('+rotation+'deg)';
});
An advanced demo is available here: http://codepen.io/runspired/full/ZQBGWd/
Contributing
Read the contributing guidelines.
For PRs.
- Use Angular Style commit messages
- Rebase your PR branch when necessary
- If you add a feature or fix a bug, please add or fix any necessary tests.
- If a new feature, open a docs PR to go with.
Building
You can get the pre-build versions from the Hammer.js website, or do this by yourself running
npm install -g grunt-cli && npm install && grunt build