All Projects → stuyam → Pressure

stuyam / Pressure

Licence: mit
👇💥 JavaScript library for handling Force Touch, 3D Touch, and Pointer Pressure.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Pressure

Dialog Polyfill
Polyfill for the HTML dialog element
Stars: ✭ 2,152 (-22.73%)
Mutual labels:  polyfill
Jekyll Spaceship
🚀 A Jekyll plugin to provide powerful supports for table, mathjax, plantuml, mermaid, emoji, video, audio, youtube, vimeo, dailymotion, soundcloud, spotify, etc.
Stars: ✭ 196 (-92.96%)
Mutual labels:  polyfill
Pagedjs
Display paginated content in the browser and generate print books using web technology
Stars: ✭ 228 (-91.81%)
Mutual labels:  polyfill
Jquery Contextmenu
jQuery contextMenu plugin & polyfill
Stars: ✭ 2,148 (-22.87%)
Mutual labels:  polyfill
Stickyfill
Polyfill for CSS `position: sticky`
Stars: ✭ 2,252 (-19.14%)
Mutual labels:  polyfill
Xmlhttprequest
XMLHttpRequest.js - Standard-compliant cross-browser XMLHttpRequest object implementation
Stars: ✭ 211 (-92.42%)
Mutual labels:  polyfill
Webp Hero
browser polyfill for the webp image format
Stars: ✭ 171 (-93.86%)
Mutual labels:  polyfill
Poly Flif
📷 A poly-fill for the FLIF image format.
Stars: ✭ 243 (-91.27%)
Mutual labels:  polyfill
Datalist Polyfill
Minimal and dependency-free vanilla JavaScript polyfill for the awesome datalist-functionality
Stars: ✭ 197 (-92.93%)
Mutual labels:  polyfill
Core Js
Standard Library
Stars: ✭ 15,854 (+469.26%)
Mutual labels:  polyfill
Rangeslider.js
🎚 HTML5 input range slider element polyfill
Stars: ✭ 2,153 (-22.69%)
Mutual labels:  polyfill
Polyfill Intl Icu
This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.
Stars: ✭ 2,287 (-17.88%)
Mutual labels:  polyfill
Tickedoff
Tiny library (<200B gzip) for deferring something by a "tick"
Stars: ✭ 213 (-92.35%)
Mutual labels:  polyfill
Hxphotopicker
图片/视频选择器 - 支持LivePhoto、GIF图片选择、3DTouch预览、在线下载iCloud上的资源、编辑图片/视频、浏览网络图片 功能 Imitation wx photo/image picker - support for LivePhoto, GIF image selection, 3DTouch preview, Download the resources on iCloud online, browse the web image function
Stars: ✭ 2,363 (-15.15%)
Mutual labels:  3d-touch
React Native Fetch Blob
This project was started in the cause of solving issue facebook/react-native#854, React Native's lacks of Blob implementation which results into problems when transferring binary data.
Stars: ✭ 2,593 (-6.89%)
Mutual labels:  polyfill
Object Fit Images
🗻 Polyfill object-fit/object-position on <img>: IE9, IE10, IE11, Edge, Safari, ...
Stars: ✭ 2,050 (-26.39%)
Mutual labels:  polyfill
Share Api Polyfill
A polyfill for the sharing that can be used in desktop too, so your users can shere in their twitter, facebook, messenger, linkedin, sms, e-mail, print, telegram or whatsapp.
Stars: ✭ 210 (-92.46%)
Mutual labels:  polyfill
Webxr Polyfill
Use the WebXR Device API today, providing fallbacks to native WebVR 1.1 and Cardboard
Stars: ✭ 251 (-90.99%)
Mutual labels:  polyfill
Eslint Plugin Compat
Lint the browser compatibility of your code
Stars: ✭ 2,743 (-1.51%)
Mutual labels:  polyfill
Details Element Polyfill
<details>
Stars: ✭ 221 (-92.06%)
Mutual labels:  polyfill

Pressure.js

Join the chat at https://gitter.im/yamartino/pressure npm https://www.npmjs.com/package/pressure npm https://www.npmjs.com/package/pressure

Pressure Example

Pressure is a JavaScript library for handling both Force Touch and 3D Touch on the web, bundled under one library with a simple API that makes working with them painless.

Head over to the documentation for installation instructions, supported devices, and more details on pressure.js.

Install

Download pressure.min.js or pressure.js files from GitHub or install with npm or bower

npm

npm install pressure --save

bower

bower install pressure --save

Setup

Use pressure in the global space:

Pressure.set('#id-name', {
  change: function(force){
    this.innerHTML = force;
  }
});

OR use it with browserify or CommonJS like setups:

var Pressure = require('pressure');

Pressure.set('#id-name', {
  change: function(force){
    this.innerHTML = force;
  }
});

Usage

NOTE: the "this" keyword in each of the callback methods will be the element itself that has force applied to it

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 will always be a value from 0 to 1 on mobile and desktop
  },
  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
  }
});

jQuery Usage

NOTE: the "this" keyword in each of the callback methods will be the element itself that has force applied to it

$('#element').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 will always be a value from 0 to 1 on mobile and desktop
  },
  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
  }
});

Options

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

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.

Pressure.set('#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 (on by default), you can see 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('#example', {
  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 (on by default), you can see the "polyfillSpeedDown" speed to determine how fast the polyfill takes to go from 1 to 0 when you let go. The value is an integer in milliseconds and the default is 0 (aka off).

Pressure.set('#example', {
  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

Only run on Force Touch trackpads (mouse)

Set the option only to the type you want it to run on 'mouse', 'touch', or 'pointer'. The names are the types of events that pressure will respond to.

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

Only run on 3D Touch (touch)

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

Only run on Pointer Supported Devices (pointer)

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

Change the preventSelect option

The preventDefault option in "true" by default and it prevents the default actions that happen on 3D "peel and pop" actions and the Force "define word" actions as well as other defaults. To allow the defaults to run set preventDefault to "false"

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

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.map()

// These are the default configs set by Pressure
Pressure.config({
  polyfill: true,
  polyfillSpeedUp: 1000,
  polyfillSpeedDown: 0,
  preventDefault: 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);
  }
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].