Mobile and Desktop Event Normalization with pointer.js

By  on  

The mobile revolution brought in a variety of new challenges, one being interaction and event handling.  We have a set of new touch events and the traditional desktop events, and click events work on both devices, but mousemove and touchmove aren't always in sync, and there are a number of other nuances you run into with different devices.  That's why Mozilla's uber-talented Web Developer Potch created pointer.js, a utility to help developers more easily manage events across devices.

pointer.js Documentation

The pointer.js documentation explains the project as well and simply as it should, so I wont reinvent the wheel.  Straight from the docs:

Types of Events

The following events are generated:

  • pointerdown: based on mousedown/touchstart
  • pointerup: based on mouseup/touchend
  • pointermove: based on mousemove/touchmove
  • pointerleave: based on mouseout/touchleave
  • pointerclick: a 'fast click' event based on a sequence of the above events. Additional heuristics are applied to determine whether or not to generate a pointerclick.

Event Objects

pointer events have the following custom properties:

  • maskedEvent: the event that triggered the pointer event.
  • touch: boolean- is maskedEvent a touch event?
  • mouse: boolean- is maskedEvent a mouse event?
  • x: page-normalized x coordinate of the event.
  • y: page-normalized y coordinate of the event.

Simple APIs are the best APIs, and Potch nailed this one.

pointer.js Usage

Here are a few example usages of pointer.js:

var element = document.getElementById("myElement");

element.addEventListener("pointerdown", function(detailedEvent) {
	// Do something
});

element.addEventListener("pointerleave", function(detailedEvent) {
	// Do something
});

element.addEventListener("pointerclick", function(detailedEvent) {
	// Do something
	if(detailedEvent.mouse) {
		// desktop
	}
	else {
		// mobile
	}
});

Potch wrote the JS such that you are adding custom event listeners in a traditional way via addEventListener.  I also love that Event object is true, not a new object that provides the original event as one property.  The additional information added to the object is useful in identifying the platform and which event was actually fired.

Event normalization was one of the original reasons for the first event frameworks...and they didn't even have to deal with mobile devices!  Potch has done well with this event normalization utility and I can't wait to use it on my next redesign!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

Incredible Demos

  • By
    JavaScript Copy to Clipboard

    "Copy to clipboard" functionality is something we all use dozens of times daily but the client side API around it has always been lacking; some older APIs and browser implementations required a scary "are you sure?"-style dialog before the content would be copied to clipboard -- not great for...

  • By
    Basic AJAX Requests Using MooTools 1.2

    AJAX has become a huge part of the modern web and that wont change in the foreseeable future. MooTools has made AJAX so simple that a rookie developer can get their dynamic pages working in no time. Step 1: The XHTML Here we define two links...

Discussion

  1. Peter Galiba

    Sad, but the pointer.js do not support Pointer events developed by Microsoft for Windows 8 and used in IE10+. Without that supported, I would not call it pointer.js as it does not support pointers, not even multiple pointers. In MS terms a pointer could be mouse, touch or pen. For more details about pointer events see http://msdn.microsoft.com/en-us/library/ie/hh673557(v=vs.85).aspx

  2. @Peter +1. Without windows 8 support i can’t use it at all. Sadly.

  3. Fançois

    What are IE10 and IE8 ?
    Pretty sure IE8 must die.
    With all the problems generated by Micrisoft, you should develop for the web, and not for Microsoft.

    Built web for next users, not just for you and your customers.
    You’ll be happy to make a better world/web ;)

    • François

      woups.

      s/IE8/Windows8

  4. yeah… do you say this to your clients or your boss as well? “Sorry, i develop only for the Web, not for Microsoft” – sorry but this is just stupid.
    While i agree Microsoft is making our lives not easier, it’s the largest platform. Not supporting it is not an option – at all.

  5. Shameless self-promotion alert… if you’re looking for a Pointer Events polyfill that adheres more closely to the specification, I’ve written one called Points.js: https://github.com/Rich-Harris/Points

    It’s not perfect, but the specification as it stands is un-polyfillable (the Points.js readme goes into why, and what the limitations are).

    The W3C’s Pointer Events working group is also working towards a more spec-compliant ‘official’ polyfill. Also, the Polymer Project’s polyfill is well worth a look http://www.polymer-project.org/platform/pointer-events.html

  6. The article should contain information about the Pointer Events standard (http://www.w3.org/TR/pointerevents/) and how pointer.js relates to that standard. From what see from the README on GitHub, pointer.js is in the process of being rewritten to support that standard. This information should be mentioned in the article. Leaving it out risks causing confusion.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!