This project provides a hands-on demonstration of JavaScript touch events. It aims to help developers understand how to capture and handle touch interactions on web pages. The demo visually represents touch points and showcases features like multi-touch, pinch-to-zoom, and draggable elements.
This project is automatically deployed to GitHub Pages. You can access the live demo at: Live Demo
(Note: You might need to enable GitHub Pages in your repository settings and ensure the deployment URL is correct.)
This demo illustrates several touch event capabilities:
-
Basic Touch Event Handling (
touchstart,touchmove,touchend,touchcancel):- Interaction: Simply touch anywhere on the screen (that isn't one of the specific feature elements below).
- Behavior:
touchstart: Creates a visual marker (a colored dot) at each touch point.touchmove: Updates the position of the visual marker to follow your finger.touchend: Removes the visual marker when you lift your finger.touchcancel: Removes the visual marker if the touch is interrupted (e.g., by a browser gesture).
-
Multi-touch Visualization:
- Interaction: Touch the screen with multiple fingers simultaneously in the general area.
- Behavior: Each distinct touch point will be represented by a uniquely colored dot, allowing you to see how multiple touches are tracked independently.
-
Pinch-to-Zoom Example:
- Interaction: Locate the text block labeled "Pinch to Zoom Me!". Use two fingers on this text.
- Behavior: Move your fingers closer together or further apart. The font size of the text will decrease or increase accordingly, demonstrating a common pinch gesture.
-
Draggable Element:
- Interaction: Find the light blue square labeled "Drag Me!". Touch and drag this square around the screen.
- Behavior: The square will follow your finger. Importantly, page scrolling is prevented while you are dragging this specific element, showcasing the use of
event.preventDefault(). The element is also constrained to stay within the viewport.
-
Clone the repository (if you haven't already):
git clone https://github.com/Justinjdaniel/learn-TouchEvents.git cd learn-TouchEvents -
Open the demo in your browser: Navigate to the
public/directory and open theindex.htmlfile in a web browser that supports touch events (most modern browsers do). For desktop development, browser developer tools often provide touch emulation.[!Tip] If you have Python installed, you can run
python -m http.serverfrom the project root and then navigate tohttp://localhost:8000/public/in your browser. Node.js users can usenpx live-serverfrom the root.
This project uses ESLint for JavaScript linting and Prettier for code formatting to maintain code quality and consistency.
- Node.js and npm (for running linters/formatters)
- Install dependencies (after cloning and for development):
npm install
- Check for linting errors:
npm run lint
- Fix linting errors automatically:
npm run lint:fix
- Format code:
npm run format