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
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    HTML5 Context Menus

    One of the hidden gems within the HTML5 spec is context menus. The HTML5 context menu spec allows developers to create custom context menus for given blocks within simple menu and menuitem elements. The menu information lives right within the page so...

  • By
    Introducing MooTools LinkAlert

    One of my favorite Firefox plugins is called LinkAlert. LinkAlert shows the user an icon when they hover over a special link, like a link to a Microsoft Word DOC or a PDF file. I love that warning because I hate the surprise...

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!