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 install pressure --save
npm install pressure --save
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);
}
});
});
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
}
});
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
}
});
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.
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.
Pressure.set('#polyfill-example', {
change: function(force, event){
this.innerHTML = force;
},
unsupported: function(){
alert("Oh no, this device does not support pressure.")
}
}, {polyfill: false});
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).
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
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).
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
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.
Pressure.set('#element-touch', {
change: function(force, event){
this.innerHTML = force + 'on an iphone';
}
}, {only: 'touch'});
Pressure.set('#element-mouse', {
change: function(force, event){
this.innerHTML = force + 'on a Mac';
}
}, {only: 'mouse'});
Pressure.set('#element-pointer', {
change: function(force, event){
this.innerHTML = force + 'on a Mac';
}
}, {only: 'pointer'});
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.
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."Pressure.set('#element-touch-prevent', {
start: function(event){
console.log('cool it started');
}
}, {only: 'touch', preventSelect: false});
Pressure.set('#element-mouse-prevent', {
start: function(event){
console.log('cool it started');
}
}, {only: 'mouse', preventSelect: false});
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.
$.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
});
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.
$.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);
}
});