Giter Club home page Giter Club logo

pressure's Introduction

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 parameter 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 device 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 is "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 override 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);
  }
});

pressure's People

Contributors

cha147 avatar davidshimjs avatar dependabot[bot] avatar gitter-badger avatar marcusmolchany avatar miw0 avatar stuyam avatar zetlen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pressure's Issues

Let Force Touch reach force of 1

The force touch value only gets up to around 0.9997...... or 0.9995.... it varies. But 3d touch and the shim go from 0 to 1. Force touch should return 1 if the force is over 0.999 so that you can do a check for 1 as the max force value and every situation will go from 0 to 1.

Pressure JS doesn't handle ZTE correctly

I have a ZTE Z957, I think it's also known as the 2016 ZTE Grand X 4.
Operating system is Android 6.0.1
Browser is Chrome 65

  • When I visit https://pressurejs.com/ the page doesn't say that it has fallen back to the polyfill.
  • The pressure value does not change over time, instead I get a number when I touch and it resets to 0 when I release or slide my finger a millimetre away.
  • I do get different numbers when I touch with different pressures (i.e. lightly touching with the tip of my finger, tapping instantly jamming my finger into the screen, and touching with my thumb gives different results)
  • However the highest value (jamming my finger into the screen) is always below 0.2.
  • The "Force Touch (mouse)" area shows an error.
  • The "3D Touch (touch)" area exhibits the long press behaviour of the polyfill.
  • The "Pointer Events (pointer)" area behaves the same as "Targets All" (the behaviour I described before)

Here's a gif of me lightly touching, touching, jamming, using my thumb, holding my finger on the 3D touch, and then on the Pointer Events.
screen recording

Chrome Android and OSX device emulation mode register touches as deep/force press

Steps to reproduce:

Open pressurejs.com in Chrome on OSX and in DevTools turn on device emulation and set it to iPhone 6. Scroll to bottom and tap on test 5 and the pop up that should only trigger with a force touch displays.

Same results on Chrome Android using a Samsung Galaxy S4 and Nexus 5. On Nexus 5 all the tests get triggered.

Testing prior to v2.0.0 release

Mac
Safari: ✅
Chrome: ✅
Firefox: ✅

(I have an iPhone 7 so all other device test will be on the simulator which does support 3D Touch)
iOS 9
iPhone 6:
Safari: ✅
iPhone 6s:
Safari: ✅

iOS 10
iPhone 6:
Safari: ✅
iPhone 6s:
Safari: ✅
iPhone 7:
Safari: ✅
Chrome: ✅

When should the prevent Default be set?

I originally set the preventDefault css and other things on first touch, but then most of the time that means the first click or touch will not have those things applied. Should prevent default be applies immediately?

I currently apply it on first click to first determine if pressure is supported or not first. But maybe that doesn't make the most sense and maybe it should be set immediately?

Also should the defaults be separated on mobile vs desktop?

Add documentation for the `css` parameter of public methods

I noticed you add a user-select: none; rule to the Pressure element, which prevents users from being able to select text. Hammer.js does this as well, but explains in the docs why they do it, what the benefits are, and also gives developers a way to disable that functionality.

If you pass false as the third parameter to something like Pressure.set() this will prevent user-select: none; from being set, which I prefer, and others might as well. Could be useful to add in the documentation.

Add Pressure.config

Add pressure config options so that you can configure pressure with site wide options.

50-100% being thought as 0-50%

I assume this is not very important, but I still want to say something just in case. Sometimes when you push the touchpad all the way down, and then lightly release, it'll start thinking pushing all the way down is only 50% This happens very rarely..


Please provide information about the device you are seeing the issue on
Device is a Early 2015 Macbook Pro
Operating system is macOS Sierra 10.12.6
Browser is Safari 10.1.2

Polyfill speed option

If you are using the polyfill option, should you be able to set the speed of with the polyfill goes from 0 to 1. Currently it takes 1 second to go from 0 to 1. I am thinking right now that I will not allow this as an option, but if anyone thinks that it would be useful I would be happy to hear the argument for it 😄

Cordova + WKWebView + UIWebView w/ pressure

Looking into more of the cordova web view settings and seeing if there is anything I can find that would stop it, but it just returns the unsupported callback. I'd love to utilize this library in my hybrid app for a few things. Yours is the first library to include a callback like this so I wasn't sure if the other libs weren't working or if I was just crazy haha.

I'll do some digging and report back what I find, if anyone else has an idea of why the cordova web view (iOS 9, uses UIWebView same as safari) wouldn't work definitely chime in.


Please provide information about the device you are seeing the issue on
Device is a iPhone 6S+
Operating system is iOS 9.1
Browser is UIWebView (not sure the versioning...)

Shim Support

Add an opt in option that would allow browsers that do not support Force or 3D touch to fall back to increasing a force pressure over time. For example go from 0 to 1 force value over the course of 1 second.

Jquery pressure

I think jquery.pressure.js has a misspell on the last line, this method return Jquery.pressure I think must be jQuery.pressure.


Please provide information about the device you are seeing the issue on
I found this error on the last release. you can see the error on the console when try to include this file

Light touch triggers timing fallback when it should track pressure

When I touch and hold lightly enough on the demos, the timing fallback triggers and a message shows saying force touch isn't supported, when it should be using force touch. Applying pressure then glitches between the pressure value and timing value.

I can't remember if it did that in the previous version, sorry =)


Device is an iPhone 6S
Operating system is iOS 10
Browser is Chrome

Mac Book Pro not working anymore

It looks like Apple updated Safari or something happened and the library is not working anymore.
Website says, 'NOT supported' if clicked on test button in Safari.

Return early in server environment

Currently, the code supplies false if no window or document are found when initiating. However, the code still runs with window and document set to false, causing errors when trying to call methods on false that do not exist (e.g. "Cannot use 'in' operator to search for 'ontouchstart' in false").

This occurs when used in a React app where the initial render is server side, since even if Pressure isn't set until componentDidMount, the import of Pressure triggers initialization, throwing errors. I'd suggest either returning early if there is no window or document, or else make Pressure be explicitly initialized (such as var pressure = Pressure();), at least in an NPM environment.

On Android it not works as excepted

On stock browser it not works at all, on Chrome mobile target changes from 0 to 1 immediately. But my js (alpha) polyfills best on these browsers but when comes to real 3d touch my js cant handle it.

http://tweenjs.github.io/es6-tween/forceify/


Please provide information about the device you are seeing the issue on
Device is a Samsung Galaxy Note2
Operating system is Android 4.4.2
Browser is Stock and Chrome browser

Error on handling multi touch events

I have the following error when I try to zoom with pinch, swipe or make any other multi touch event on element that Pressure is applied to.

TypeError: undefined is not an object (evaluating 'this.selectTouch(e).force')

in line this._changePress(this.selectTouch(e).force, e))

It`s probably in pressure/dist/pressure.js:316
or pressure/src/adapters/adapter_3d_touch.js:25

The code that uses Pressure

Pressure.set(self.slotsContainer, { //DOM node
startDeepPress: function (event) {
//handle event
}
});


Device is a 2016 IPhone 6S Plus MN2W2RU/A
Operating system is iOS 10.2.1
Browser is Safari 10.0

Pressure Is Not Supported On iPhone 6S Plus

Just wanted to let you know, that I can let that button on the website say "not supported", when pressing very very lightly on an iPhone 6S Plus. I guess that's related to the force actually being 0.0000. I suggest to change the check from (I assume) if (force) to if (force == null) or something similar.

Prevents scroll on touch device

I cannot scroll when touching an element with a pressure listener. Confirmed also on http://pressurejs.com/ on test elements though does not occur on red button (support check) at top.

In my case I'm using startDeepPress and endDeepPress and both cause this behavior.


Please provide information about the device you are seeing the issue on
Device is a iPhone6S
Operating system is iOS 9.3.1
Browser is iOS Safari and iOS Chrome 49.0.2623.109

Full support for Apple Pencil

While it detects the pressure of the Apple Pencil if I don't use my finger at all, it does not work if I tap the button with my finger first. After that, the Pencils pressure detection is no longer supported until I refresh the page.


Please provide information about the device you are seeing the issue on
Device is a 2016 iPad Pro 9,7"
Operating system is iOS 9.3.1
Browser is Mobile Safari

Android pointer event bug

@davidshimjs I am moving the conversation of the Android bug to a proper issues.

Some android devices support the pointer event. http://caniuse.com/#search=pointer You can encounter the error when using these android devices. This is the reason why re-ordering of the conditions.
@davidshimjs

I am not sure that I follow. In theory the android devices that do not support pointer event it will skip trying to use pointer events on them. If not then the way we check for those events is wrong.

Can you elaborate on the error?

On some android devices 'force' is being returned as 1 only in change method.

On some android devices, the force parameter for the change method always returns as 1 and 1 only - has this happened to anyone else? (devices tested were Sony Xperia Android 6.0.1 and a Samsung device). I also tried testing on a Wileyfox Swift 2 - android - chrome, which doesn't seem to have triggered at all.

I'm unable to get direct access to these devices right now, but if you need more information from me let me know and I'll try and get a hold of them with the specs etc.

Thanks @stuyam.


Please provide information about the device you are seeing the issue on
Device is a Sony Xperia
Operating system is Android 6.0.1
Browser is Chrome

Firefox console warning: _mutating the [[Prototype]] of an object will cause your code to run very slowly

Hello,

Thanks for this script. It is great!

Using Pressure.js with Firefox 47.0.1 64 bit on Windows 10, you can see this warning in the console:

mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create

The function that is causing problems is the one called "_inherits". I guess it is because it is trying to do this:

subClass.proto = superClass;

Could that warning be fixed, please? Despite that fact that it is not so serious and it should still work even with the warning, it does not look nice.

What I am doing now to prevent this warning is to detect whether "webkitmouseforcewillbegin" event is supported by the browser and if so, I lazy load the script.

Cheers,
Joan

Force Touch version running on Simulator

All 3 test examples, including the "force touch only" run on the simulator on an iPhone 6s running OS 9.3. The "force touch only" example should fail. Does this have to do with the simulator itself?

for some reason it is not working

hi .. first of all I would like to thank you for your work .. looks great in the examples but for some reason non of these working with me .. jquery-3.1.1

     $('#divpic').pressure({
        start: function(event){
          alert("deep pressure start");
        },
        end: function(){
          alert("deep pressure start");
        },
        startDeepPress: function(event){
          alert("deep pressure start");
        },
        endDeepPress: function(){
          alert("deep pressure end");
          //var $this = $(this);
          //var modalval = this.ref.value;
          //$("#myModalpic"+modalval).modal();
        },
        change: function(force, event){
          alert("deep pressure end");
        },
        unsupported: function(){
          alert("unsupported browser");
        }
      });

Please provide information about the device you are seeing the issue on
Tested on Chrome latest version, Firefox latest version
Safari iPhone 7 plus , chrome on 7plus

iPhone 7 - iOS 10.3.2 - touchforcechanged event never fired in standalone mode

Related Stack Overflow question:
https://stackoverflow.com/questions/45003908/iphone-7-ios-10-3-2-touchforcechange-event-never-fired

In shorts: as in the title: the 'touchforcechanged' event is never fired when a website saved to the springboard is launched in standalone mode :/

-> I first thought the issue was coming from your lib, but after re-implementing things myself without any polyfill, it seems it's iOS mobileSafari that bricks it .. or is it a feature ?

navigator.appVersion:
5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1


Device is a [?year] [iPhone] [7]
Operating system is [iOS] [10.3.2]
Browser is [MobileSafari] [Mobile/14F89 Safari/602.1]

Code clean up.

Need some general code clean up and pass through before I release.

Android devices Polyfill not working

On some Android devices it goes from 0 to 1 just by touching it, fixed it on my project by adding the only with "touch" if I found out that the user is using a mobile device, I believe that the android phone is crashing with the pointer event, if you try the widgets on the pressure.js page you will find that "Targets All" and "Targets ONLY POINTER events" have this weird effect from 0 to 1 but on the "ONLY Force touch" it does work. tested it on a Moto G4 play and a shitty Chinese Android phone(I use it a lot for testing, if something runs on it then it will run anywhere)


Please provide information about the device you are seeing the issue on
Device is a Moto g4 play
Operating system is Android 5.0 and 6.0
Browser is Chrome

Considering Pointer Events Pressure for pressure in Chrome

Hi Stuart! We've been looking for a solution to make reading pressure across browsers and platforms less of a pain than it currently is and this is exactly it! Thank you for this fantastic library!

Now to our question/issue:
We're working with wacoms mostly and it seems that since Chrome 55/56 pointer events are now supported by default, delivering pressure from http://caniuse.com/#feat=pointer (at least verified with wacom on OS X).
Is there a chance PE can be considered to read pressure within pressure.js?

There are a few gotchas though:

  • Chrome: not sure why the Macbook Force Trackpad only reads as 0 and 0.5 via pointer events
    (using https://rbyers.github.io/eventTest.html)
  • Firefox: For wacom users there seems to be no other fallback other than the old wacom NPAPI plugin to get pressure unless the flags 'layers.async-pan-zoom' and 'dom.w3c_pointer_events.enabled' are enabled (hoping this will soon land as a stable feature)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.