Hammer.js is a JavaScript library designed for detecting and handling multi-touch gestures such as tap, doubletap, press, pan, swipe, pinch, and rotate in web applications. It aims to provide a unified API across touch, mouse, and pointer events, offering a lightweight solution with no external dependencies and a small footprint (7.34 kB minified + gzipped for v2.0.8). However, the main `hammerjs` package has not seen active development since its last stable release, version 2.0.8, published over 10 years ago on April 22, 2016. While it was a popular choice for gesture recognition, its maintenance status means that users often look to forks like `@egjs/hammerjs` for continued support or migrate to newer, actively maintained gesture libraries.
npm install hammerjsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to initialize Hammer.js on an element, add a rotation recognizer, and listen for the 'rotate' event to dynamically transform the element. It also includes a basic tap event example.
Review the official Hammer.js v2.0 documentation carefully for API changes. Existing v1.x code will require substantial refactoring.
For new projects, evaluate alternatives like `@use-gesture` or native DOM touch events. For existing projects, consider forks like `@egjs/hammerjs` if you require continued maintenance or specific features like ESM support.
Implement dynamic imports or conditional loading to ensure Hammer.js is only initialized client-side. For Webpack, `bundle-loader` can be used. Ensure all gesture-related logic runs after the component has mounted (e.g., `componentDidMount` in React or `ngAfterViewInit` in Angular).
Carefully configure recognizer options (like `direction`, `threshold`) to avoid conflicts. Use `Hammer.Manager` for complex scenarios to manage recognizers effectively. Ensure `mc.destroy()` is called or `mc.off()` is used to remove listeners when elements are removed from the DOM or components unmount.
Always ensure you are referencing the correct, pre-built distribution file (`hammer.js`) and not raw source files when including the library directly in HTML or bundling.
Conditionally load Hammer.js only on the client-side using dynamic imports or ensure its initialization is deferred until the browser environment is available.
Verify that your HTML script tag or module bundler is pointing to the correct Hammer.js distribution file, usually `node_modules/hammerjs/hammer.js`.
Install the type definitions package: `npm install --save-dev @types/hammerjs`.
Explicitly enable disabled recognizers (e.g., `mc.get('pinch').set({ enable: true });`). Adjust `direction` (e.g., `Hammer.DIRECTION_ALL`) and `threshold` options for recognizers to resolve conflicts and ensure proper detection. For complex interactions, use `recognizeWith` or `requireFailure`.No dependency data recorded yet.