1. Installation

Pressure is really simple to install, you can use npm or bower, or head over to the github and download the repo itself. All you need is the pressure.min.js or the pressure.js file.

bower
bower install pressure --save
npm
npm install pressure --save

2. Setup

You can use pressure with browserify, debowerify, or anything that uses CommonJS to include packages. Pressure can be used normally if no module object exists.

// Regular
Pressure.set('#test', {
  change: function(force, event){
    console.log(force);
  }
});
// Browserify, Debowerify, CommonJS
var Pressure = require('pressure');

Pressure.set('#test', {
  change: function(force, event){
    console.log(force);
  }
});
// RequireJS
requirejs(['pressure'], function( Pressure ) {

  Pressure.set('#test', {
    change: function(force, event){
      console.log(force);
    }
  });

});

3. Usage

Pressure has a really simple method signature. The first argument is the element(s) you are targeting and the second argument is an object with optional callback functions. This lists all the element types that can be passed into Pressure, anything from a string selector, to a DOM node object.

// target all links
Pressure.set('a', {});
// pass in jQuery elements
var elements = $('.dogs');
Pressure.set(elements, {});
// pass in element list or single element
var elements2 = document.querySelectorAll('.cats');
Pressure.set(elements2, {});
var elements3 = document.getElementById('cat');
Pressure.set(elements3, {});
// element with the 'stuart' ID being selected and calling the 'change' callback
Pressure.set('#stuart', {
  change: function(force, event){
    // the force value is passed back as well as the full event
    this.innerHTML = force;
  }
});

This example uses all of the optional methods available to you in the callback object.

Pressure.set('#element', {
  start: function(event){
    // this is called on force start
  },
  end: function(){
    // this is called on force end
  },
  startDeepPress: function(event){
    // this is called on "force click" / "deep press", aka once the force is greater than 0.5
  },
  endDeepPress: function(){
    // this is called when the "force click" / "deep press" end
  },
  change: function(force, event){
    // this is called every time there is a change in pressure
    // 'force' is a value ranging from 0 to 1
  },
  unsupported: function(){
    // NOTE: this is only called if the polyfill option is disabled!
    // this is called once there is a touch on the element and the device or browser does not support Force or 3D touch
  }
});

4. jQuery Usage

Use the jquery.pressure.min.js file if you are using jQuery in your project. Using the Pressure jQuery library makes it simple to attach pressure events to jQuery elements.

$('a').pressure({
  start: function(event){
    // this is called on force start
  },
  end: function(){
    // this is called on force end
  },
  startDeepPress: function(event){
    // this is called on "force click" / "deep press", aka once the force is greater than 0.5
  },
  endDeepPress: function(){
    // this is called when the "force click" / "deep press" end
  },
  change: function(force, event){
    // this is called every time there is a change in pressure
    // 'force' is a value ranging from 0 to 1
  },
  unsupported: function(){
    // NOTE: this is only called if the polyfill option is disabled!
    // this is called once there is a touch on the element and the device or browser does not support Force or 3D touch
  }
});

5. Options

With Pressure, the third paramater is an optional object of options that can be passed in.

To set any of the config globally use the Config Helper.

Polyfill Support

Using the "polyfill" keyword, you can disable polyfill support for the element. The polyfill is enabled by default and is useful if the device or browser does not support pressure, it will fall back to using time. For example instead of force from 0 to 1, it counts up from 0 to 1 over the course of one second, as long as you are holding the element. Try some of the examples on the main page on a devices that does not support pressure and see for yourself how it works.

Click on me on any device :)
Pressure.set('#polyfill-example', {
  change: function(force, event){
    this.innerHTML = force;
  },
  unsupported: function(){
    alert("Oh no, this device does not support pressure.")
  }
}, {polyfill: false});

Polyfill Speed Up

If you are using the polyfill, you can use the "polyfillSpeedUp" speed to determine how fast the polyfill takes to go from 0 to 1. The value is an integer in milliseconds and the default is 1000 (1 second).

Click on me on any device :)
Pressure.set('#polyfill-speed-up', {
  change: function(force, event){
    this.innerHTML = force;
  }
}, {polyfillSpeedUp: 5000});
// takes 5 seconds to go from a force value of 0 to 1
// only on devices that do not support pressure

Polyfill Speed Down

If you are using the polyfill, you can use the "polyfillSpeedDown" speed to determine how fast the polyfill takes to go from 1 to 0 when the elemnt is released. The value is an integer in milliseconds and the default is 0 (aka off).

Click on me on any device :)
Pressure.set('#polyfill-speed-down', {
  change: function(force, event){
    this.innerHTML = force;
  }
}, {polyfillSpeedDown: 2000});
// takes 2 seconds to go from a force value of 1 to 0
// only on devices that do not support pressure

Device Detection

With Pressure, the third paramater is an optional object of options that can be passed in. The first option is device targeting. Using the "only" keyword, you can define if you want pressure to respond to ONLY touch, ONLY Mouse, or ONLY Pointer events.

Click Me On an iPhone 6s or iPhone 7
Pressure.set('#element-touch', {
  change: function(force, event){
    this.innerHTML = force + 'on an iphone';
  }
}, {only: 'touch'});
Click Me On a Mac with Force Touch
Pressure.set('#element-mouse', {
  change: function(force, event){
    this.innerHTML = force + 'on a Mac';
  }
}, {only: 'mouse'});
Click Me On a Wacom Table or device that supports Pointer Events
Pressure.set('#element-pointer', {
  change: function(force, event){
    this.innerHTML = force + 'on a Mac';
  }
}, {only: 'pointer'});

Prevent Select

Both Mac and iPhones have system wide features that they default to when force clicking on something (ex. defining a word on Mac or "peeking and popping" an image on iOS). Pressure prevents those actions from happening, however if you still want those actions to be possible on "Pressure" elements, you can pass in "preventSelect" as an option.

Pressure sets user-select: none; css property on all of the elements it is attached to. It does this to keep text from being selected while force touching. However, sometimes you may want this turned off. The "preventSelect" option also handles removing that."
Push the image hard on an iPhone6s or iPhone7 to "peek and pop" it
Pressure.set('#element-touch-prevent', {
  start: function(event){
    console.log('cool it started');
  }
}, {only: 'touch', preventSelect: false});
Force touch one of these words on a Force Touch trackpad to define it.
Pressure.set('#element-mouse-prevent', {
  start: function(event){
    console.log('cool it started');
  }
}, {only: 'mouse', preventSelect: false});

6. Helpers
Config:

You can use Pressure.config() to set default configurations for site wide setup. All of the configurations are the same as the options listed above.

Heads Up: If you have a config set, you can always overide the config on individual Pressure elements by passing in any of the options listed above to a specific Pressure block.
When using the jQuery Pressure library, use $.pressureConfig() rather than Pressure.config()
// These are the default configs set by Pressure
Pressure.config({
  polyfill: true,
  polyfillSpeedUp: 1000,
  polyfillSpeedDown: 0,
  preventSelect: true,
  only: null
});
Map:

You can use Pressure.map() to map a value from one range of values to another. It takes 5 params: Pressure.map(inputValue, inputValueMin, inputValueMax, mapToMin, mapToMax); Here is a good write up on how this works in the Processing framework: Map Function.

When using the jQuery Pressure library, use $.pressureMap() rather than Pressure.map()
Pressure.set('#element', {
  change: function(force, event){
    // this takes the force, given that the force can range from 0 to 1, and maps that force value on a 100 to 200 range
    this.style.width = Pressure.map(force, 0, 1, 100, 200);
  }
});