p5.js deviceShaken() Function
The function deviceShaken() function is called when the device total acceleration changes accelerationX and accelerationY values is more than the threshold value.
The default threshold is set to 30, and it can be changed using setShakeThreshold() function.
It can be used as a sensor in mobile devices like detecting motion, acceleration, rotation, heading, and location.
Syntax:
deviceShaken()
Now we will run some examples on android phones.
Step 1: Open online web editor of p5.js on the mobile phone using any browser “https://editor.p5js.org/”
Step 2: Write the following code in the editor section and run it to see the output.
Example 1:
Javascript
var value = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
background(value);
}
function draw() {
background(value);
fill(0, 0, 255);
textAlign(CENTER, CENTER);
textSize(25);
value = constrain(value - 2, 0, 200)
if (value > 10) {
text( "Shaking Device" , width / 2, height / 2);
} else {
text( "Device is Silent " , width / 2, height / 2);
}
}
function deviceShaken() {
value = constrain(value + 5, 0, 255)
}
|
Output: When we shake the device then we will get the output
Example 2:
Javascript
let value = 0;
function draw() {
fill(value);
rect(50, 50, 100, 100);
}
function deviceShaken() {
value = value + 10;
if (value > 255) {
value = 0;
}
}
|
Output:
Reference: https://p5js.org/reference/#/p5/deviceShaken
Last Updated :
21 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...