Code Monkey home page Code Monkey logo

ezgesture's Introduction

EZGesture(~1KB GZipped)

npm (tag) GitHub

Easily add gestures functionality with simple native DOM events

Installation

NPM

npm i ezgesture

CDN:

<!-- Full bundle -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script>
<!-- Drag only -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/drag.min.js"></script>
<!-- Pinch only -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pinch.min.js"></script>
<!-- Longpress only -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/longpress.min.js"></script>

Quick Start

// drag events
EZG.enableDragEvents(elm, options?: {threshold: 0})
elm.addEventListener("ezgdragstart", onDragStart)
elm.addEventListener("ezgdragmove", onDragMove)
elm.addEventListener("ezgdragstop", onDragMove)

// pinch events
EZG.enablePinchEvents(elm, options?: {distanceThreshold: 0, angleThreshold: 0})
elm.addEventListener("ezgpinchstart", onPinchStart)
elm.addEventListener("ezgpinchmove", onPinchMove)
elm.addEventListener("ezgpinchend", onPinchEnd)

// longpress events
EZG.enableLongPressEvents(elm, {duration: 700})
elm.addEventListener("ezglongpress", onLongPress)

Full Demo

Source code available in demos folder

Usage

Drag Events

To enable drag events:

EZG.enableDragEvent(elm, options?)

You can also supply threshold in options to set minimum distance to trigger events

Events

  • ezgdragstart - cancelable with e.preventDefault()
  • ezgdragmove - cancelable. will stop calculating last offset.
  • ezgdragstop

Events parameters: Use e.detail to access these event parameters:

// Initial drag position
startX, startY

// Last drag position
lastX, lastY

// Mouse delta from initial drag position
offsetX, offsetY

// Mouse delta from last mouse position
movementX, movementY

// it can be Touch or Mouse Event
// so you could use properties like ctrlKey or altKey
originalEvent

Pinch Events

To enable pinch events:

EZG.enablePinchEvent(elm, options?)

Available options:

  • distanceThreshold: minimum distance to trigger event
  • angleThreshold: minimum angle(radians)

ezgpinchstart

cancelable: Yes, with e.preventDefault()

parameters:

startTouches
originalEvent

ezgpinchmove

cancelable: Yes, It will stop calculating lastXXX parameters such as lastOffset and lastDist

parameters:

  • dx, dy: difference between the two touches
  • da: angle difference from last movement
  • dist: distance between two fingers
  • offset: distance difference from last touch
  • angle: angle between two fingers (radians)
  • midX, midY: center position of two fingers
  • dir: direction of the pinch. -1=pinch in, 1=out, 0=no direction change
  • startTouches
  • lastTouches
  • lastOffset
  • lastDist
  • lastAngle

ezgpinchend

cancelable: No

parameters:

  • startTouches
  • lastTouches
  • lastOffset
  • lastDist
  • lastAngle

Longpress Events

To enable longpress events:

EZG.enableLongPressEvents(elm, {duration: ms})

The default duration is 700ms. and it has only one parameter originalEvent

Integration with other frameworks

Svelte

You could also listen to these events with framework that uses native dom events. For example in svelte you could use:

<div use:enablePinchEvents={{options}} on:ezgpinchmove={onPinch}></div>

Example

Browser Support

Should support most of latest 5 years browsers. if you want more support you could through polyfills

ezgesture's People

Contributors

qurafi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ezgesture's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.