Giter Site home page Giter Site logo

touchy.js's Introduction

Touchy.js

Because some things just need to be touched.

Touchy.js is a simple light-weight (1.98 kb minified+gzip) JavaScript library for dealing with touch events in the browser. With no dependencies, just add the script to your page and start hacking.

Quick example

// The HTML element that to watch for touches
var touchMe = document.getElementById('touch-me');

// Touchy.js creates a single global object called 'Touchy'
var toucher = Touchy(touchMe, function (hand, finger) {
	// this === toucher
	// toucher.stop() : stop  watching element for touch events
	// toucher.start(): start watching element for touch events

	// This function will be called for every finger that touches the screen
	// regardless of what other fingers are currently interacting.

	// 'finger' is an object representing the entire path of a finger
	// on the screen. So a touch-drag-release by a single finger would be
	// encapsulated into this single object.

	// 'hand' is an object holding all fingers currently interacting with the
	// screen.
	// 'hand.fingers' returns an Array of fingers currently on the screen
	// including this one.
	// In this case we are only listening to a single finger at a time.
	if (hand.fingers.length > 1) {
		return;
	}

	// This callback is fired when the finger initially touches the screen.
	finger.on('start', function (point) {
		// 'point' is a coordinate of the following form:
		// { id: <string>, x: <number>, y: <number>, time: <date> }
	});

	// This callback is fired when finger moves.
	finger.on('move', function (point) {
		console.log('finger is moving');
	});

	// This callback is fired when finger is released from the screen.
	finger.on('end', function (point) {
		console.log('finger stopped moving');
	});

	// finger.lastPoint refers to the last touched point by the
	// finger at any given time.
});

Multi-touch example

var touchMe = document.getElementById('touch-me');

Touchy(touchMe, {
	one: function (hand, finger) {
		// Full touchy style event system, run only when exactly one finger
		// on screen.

		// In these cases 'hand' is only alive for the duration of touches that
		// have the exact number of simulataneous touches (unlike in the
		// previous example).
	},

	two: function (hand, finger1, finger2) {
		// Only run when exactly two fingers on screen
		hand.on('move', function (points) {
			// 'points' is an Array of point objects (same as finger.on point object)
		});
	}

	// 'three', 'four', 'five' are supported as well.
	// 'any' is the same as the previous example.
});

Event handling

var touchMe = document.getElementById('touch-me');

Touchy(touchMe, function (hand, finger) {
	function startFinger () {
		// ...
	}

	finger.on('start', startFinger);  // Attach startFinger to the start event

	finger.off('start', startFinger); // Detach startFinger from the start event

	finger.once('start', startFinger); // One-time-use handler for start event

	finger.trigger('start', arg1, ..); // Trigger the start event with arguments
});

Mouse simulation

var touchMe = document.getElementById('touch-me');

// When the second invocation argument is set to true Touchy will pick up mouse
// events along with touch events. This is good for testing on desktop.
Touchy(touchMe, true, callback); // For all finger events
Touchy(touchMe, true, { one: ..., two: ... }); // For multi-touch finger events

Plugin support

// Define a plugin
Touchy.plugin('myPlugin', function (elem, settings) {
	// Write your plugin here.
	// This will be executed each time the plugin is applied to an element.

	// 'elem' is the element being touched
	// 'settings' is the the object passed during usage of the plugin

	// Return an object to setup Touchy for the element.
	// This is equivalent to Touchy(elem, { one: ..., two: ... });
	return { one: ..., two: ... };
});

// Use a plugin
var touchMe = document.getElementById('touch-me');
Touchy(touchMe, {
	myPlugin: { these: 'are', your: 'settings' }
});

jQuery wrapper

var touchMe = document.getElementById('touch-me');
Touchy(touchMe, { one: ..., two: ... });

// This is equivalent to the following jQuery code:
$('#touch-me').touchy({ one: ..., two: ... });

touchy.js's People

Contributors

jairajs89 avatar

Stargazers

Steven Eberling avatar LeroyK avatar Manoj avatar 404 avatar Manoj Baruah avatar  avatar Logan King (DarkComet) avatar J Wong avatar  avatar Jordan Cauley avatar egeres avatar lswweb avatar  avatar Acampbell avatar  avatar Harry Yu avatar D.Terry avatar suzheng avatar 李志林 avatar  avatar Cat  avatar Jan Dinter avatar  avatar  avatar  avatar 余腾靖 avatar Corcules avatar Aleksandr Pushkarev avatar kuang avatar Dara avatar  avatar feibenren avatar  avatar Dave C avatar Blago Eres avatar  avatar Joshua Toenyes avatar feilong avatar zfdai avatar Venkat Kiran Bandi  avatar  avatar Nitesh avatar Arjun Menon avatar codingAlong avatar Hosein avatar Rajkumar Magar avatar Hasan Oezdemir avatar FanJoin avatar Aniket Naik avatar seyyah avatar Changer He avatar zarkshao avatar  avatar LEI avatar  avatar 龙方淞 avatar bee avatar  avatar huā huā avatar DarisX avatar hayato avatar David Ervideira avatar 刘勇 avatar Valentin Vichnal avatar Jingyi Zeng avatar Zency avatar  avatar  avatar 曹文忠 avatar Yoon.Tso avatar buer avatar Moose avatar lucy avatar  avatar Carsten Witt avatar 张艳 avatar misCC avatar netop://ウエハ avatar Aleen avatar fnsoxt avatar Bung avatar Angus H. avatar Coin avatar  avatar 文蔺 avatar Gautam Mittal avatar Guillermo Greising avatar Greg Skiano avatar Jeno Liu avatar Luca Colonnello avatar ryan avatar Leah Suter avatar stuart.shi avatar Omar avatar Howard Nguyen avatar Wang Qiu avatar jackxiong avatar louis avatar KevinInsight avatar Michael Anthony avatar

Watchers

Victor Baumann avatar  avatar Martin Kas avatar Stevenson Gossage avatar Nikki Koole avatar  avatar James Cloos avatar Dinesh Kr. Choudhary avatar hunslater avatar Guillermo Greising avatar Krister Kari avatar Ilia Akhmadullin avatar Bruse Johnas avatar Michael Anthony avatar  avatar luqingxuan avatar  avatar  avatar  avatar  avatar

touchy.js's Issues

Touchend event not triggered

When I try to use Touchy on my Transformer Prime, the touchend event is never triggered, unless I only do a simple click-like touch. Once I move the finger, the touch never ends and the hand grows more and more fingers the more touchstart events I trigger.

Using mouse simulation my code works fine though.

I've tried initializing Touchy with

Touchy(touchField, true, {
    one: function(hand, finger){
        console.log(hand.fingers.length);
    }
});           

to make sure that only one finger is used but that won't help either. The hand grows more and more fingers with every new touchstart and never is a touchend event triggered.

Performance degradation on continuous unreleased touches

Problem:

After touching an activated element and constantly moving your finger without releasing for a minute or so, performance starts to degrade.

Cause:

Touchy stores data for a touches entire lifespan until it is released. So upon release from the screen memory is cleared and performance is once again normal.

Solution:

Simply don't store the entire touch lifespan and expose the data as it is generated so that developers can decide on their own wether or not they wish to store it.

consider mouseup when mouse leave an element

Hi,
when using the mouse:
mousedown starts triggering the move.
But if we leave the document without releasing the button, and release the button from outside the page, the page does not catch the release event, and when we come back to the element (with button released), the move are still triggered. This is quite confusing.
So, as a workaround, maybe you can cancel the move when mouse leaves an element.

Here is a quick patch I use to get a better behaviour

diff --git a/touchy.js b/touchy.js
index eee2f1e..75a4a62 100644
--- a/touchy.js
+++ b/touchy.js
@@ -386,6 +386,7 @@

    if ( this.handleMouse ) {
        bind(this.elem, 'mousedown' , this.mousedown() );
  •       bind(this.elem, 'mouseout'  , this.mouseup() );
        bind(this.elem, 'mouseup'   , this.mouseup()   );
        bind(this.elem, 'mousemove' , this.mousemove() );
    }
    

Host a demo please!!! :)

I want to see if this works; the demo is only in a downloadable zip file.

in order to try it with my iPad, I need to download it, upload it to my own host, then load up that url in my iPad. Can you set up a free hosting account somewhere to host the demo rather than just post it on github?

I can do this myself of course, but that means the next person has to do it too, and the next one....and the next one...

ad infinitum.

:)

error line 390

Hi,

on iPad, there is an error line 390 of touchy.js:

your code says:

this.fingers.splice(index, 1);

but this.fingers is undefined.

Instead, I wrote this:

mainHand.fingers.splice(index, 1);

and it solved the problem. But is it the right thing? And btw, why do you do a splice at this place?

Thanks for fixing this.

M.E.

Internet Explorer 8 incompatibility

Touchy is a great layer for implementing both touch (smartphones and tablets) and click (desktops) events in a webapp. I ran into a problem when testing a webapp of mine on Internet Explorer; the coordinates that the Touchy callback returns are 'undefined'.

Unfortunately Internet Explorer 8 and older versions still have a market share of over 20 percent, such a large group of users is hard to ignore. It would be great if Touchy supports Internet Explorer 8 as well!

feature idea/request: delegating events to a containerelement

Hi!

Binding Touchy.js on 40+ elements on iOS (iPhone 4) casues my webapps UI to hang for a few seconds. Using event delegation should speed things up.

    Touchy(element, callback(hand, finger) {} );

could become:

    Touchy(containerelement, cssSelector, callback(hand, finger, element) {} );

or something like

    Touchy(containerelement, cssSelector, callback(hand, finger) {
      // finger.element
    } );

Debugging on desktop

Hi, I was wondering if you had planed a way to test the App in the desktop? I.e. Faking the ipad movement with the mouse by holding keys or something? Thanks

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.