Skip to content

Motion sensor and web app deployment workshop demo code

Notifications You must be signed in to change notification settings

potatowagon/silly-app-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Motion Sensor and Web App demo

0) Fork and clone, or download this repo

Cloning

cd to/your/destination/folder
git clone https://github.com/potatowagon/silly-app-demo.git 

Add remote

git remote add origin https://github.com/<your-github-username>/silly-app-demo.git

copy your link from github's green clone button

1) Adjust Chrome flags

Android Chrome disables motion sensors and audio play from custom event triggers by default (Since chrome version 63). Enable them for your app to work.

In Chrome search bar go to

about:flags

Search for Generic Sensor and change to Enabled.
Search for Autoplay policy and change to No user gesture is required.

2) Code Snippets

HTML Button

Let's add a button!
Paste this code under /*INSERT HTML BUTTON HERE*/ in index.html

<button type="button" onclick="scream()">Tickle!</button>

Make Button scream

Clicking on the button, scream() function is called. Lets define it.

Paste this under /* INSERT SCREAM FOR BUTTON CLICK FUNCTION HERE*/ in index.js

function scream() {
    var scream = new Audio(randomPicker(app.audio));
    scream.play();
}

Linear Acceleration Sensor

This is your accelerometer. This detects linear acceration without counting in gravity.

Paste this code snippet under /*INSERT ACCELEROMETER HERE*/ in index.js

try {
    let sensor = new LinearAccelerationSensor({ frequency: 60 });
    sensor.start();

    sensor.onreading = () => {
        var event = new CustomEvent('devicemotion', {
            detail: {
                acceleration: {
                    x: sensor.x,
                    y: sensor.y,
                    z: sensor.z
                }
            }
        });
        window.dispatchEvent(event);
    }
    sensor.onerror = event => console.log(event.error.name, event.error.message);
}
catch (e) {
    console.log(e);
    app.usingGenericSensor = false;
}

More info on motion sensors

ADD AN EVENT LISTENER TO WINDOW

So window (your open chrome window) will catch the devicemotion Event thrown by sensor.

Paste this under /*ADD AN EVENT LISTENER TO WINDOW*/ in index.js

window.addEventListener('devicemotion', deviceMotionHandler, false);

Make app scream

Paste this code snippet under /*DO SILLY STUFF*/ in index.js

var scream = new Audio(randomPicker(app.audio));
scream.play();
sleep(2000);

3) Stage, Commit, Push

ONLY FOR GIT USERS

Stage

git add --all

Commit

git commit -m "your message"

Push your local changes to Github

git push origin master:master

4) Deploy to Azure

Dashboard > your-app > deployment options

Configure either dropbox or Github

alt text alt text

How to dropbox

About

Motion sensor and web app deployment workshop demo code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published