You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement accurate running detection in Phone.svelte #1
Will need tweaking so the threshold is lower cause it is better to assume user is running than to make them have to work incredibly hard for the video not the pause.
This is working pretty well for me but could still be improved so I am leaving this open for some potentially smarter people.
Current Implementation:
letyGravity=0;letthreshold=0.3;letdeviceWasLastMovingUpwards=false;letdeviceWasMoving=true;letisDeviceMoving=false;functionhandleDeviceMotion(e){yGravity=e.accelerationIncludingGravity.y;}functionhandleDeviceOrientation(e){if(yGravity-10*Math.sin((e.beta*Math.PI)/180)>threshold){//We are moving the device updeviceWasLastMovingUpwards=true;}elseif(yGravity-10*Math.sin((e.beta*Math.PI)/180)<-threshold){//We are moving the device downif(deviceWasLastMovingUpwards){//We have taken a "step"isDeviceMoving=true;}deviceWasLastMovingUpwards=false;}else{isDeviceMoving=false;}if(isDeviceMoving&&!deviceWasMoving){//Send "playvideo" eventif(socket.readyState===WebSocket.OPEN){socket.send(JSON.stringify({partnerID: desktopWebSocketID,messageType: "playvideo"}));}}elseif(!isDeviceMoving&&deviceWasMoving){//Send "pausevideo" eventif(socket.readyState===WebSocket.OPEN){socket.send(JSON.stringify({partnerID: desktopWebSocketID,messageType: "pausevideo"}));}}deviceWasMoving=isDeviceMoving;}
Okay, current method works but sometimes will continue running when you set the phone down, as if the last state it detected was running.
Could have a timeout that checks if an event has been triggered after a certain amount of time and then if not, pause the video. Probably better to implement an actual accurate system instead. 🤷♂
I re-implemented it and it works much better, almost perfect on my device[s] but testing on an Iphone 7 using Firefox results in stuttering. Unsure whether this is an issue with that specific device/usecase. Will need to do more testing.
As a reference, the code for this is in src/accelerometer.js and it is a function that takes a threshold (how much does the user need to move the device) and the timeout (how long do we wait in between detections before checking again) and it finally takes a callback that will be triggered with true/false depending on whether the device is or is not moving.
Activity
Pjaerr commentedon Mar 27, 2020
Got this to work way better with guidance from: https://github.com/wxforever/stepCounter/blob/master/countstep.js
Will need tweaking so the threshold is lower cause it is better to assume user is running than to make them have to work incredibly hard for the video not the pause.
Pjaerr commentedon Mar 28, 2020
This is working pretty well for me but could still be improved so I am leaving this open for some potentially smarter people.
Current Implementation:
Relevant event listeners:
This all happens in
Phone.sveltePjaerr commentedon Mar 28, 2020
Okay, current method works but sometimes will continue running when you set the phone down, as if the last state it detected was running.
Could have a timeout that checks if an event has been triggered after a certain amount of time and then if not, pause the video. Probably better to implement an actual accurate system instead. 🤷♂
Pjaerr commentedon Apr 1, 2020
I re-implemented it and it works much better, almost perfect on my device[s] but testing on an Iphone 7 using Firefox results in stuttering. Unsure whether this is an issue with that specific device/usecase. Will need to do more testing.
Pjaerr commentedon Apr 1, 2020
As a reference, the code for this is in
src/accelerometer.jsand it is a function that takes a threshold (how much does the user need to move the device) and the timeout (how long do we wait in between detections before checking again) and it finally takes a callback that will be triggered with true/false depending on whether the device is or is not moving.Code change: https://github.com/Pjaerr/QuaRUNtine/blob/master/frontend/src/accelerometer.js