p5.js deviceOrientation variable
Last Updated :
28 Apr, 2021
The deviceOrientation variable contains the orientation of the device. The value of this variable in the code will either be set landscape or portrait. It is used in mobile devices for detecting the orientation and can be used to change the sketch for another orientation if required. When no data is available, the variable will be set to undefined.
Syntax:
deviceOrientation
Example:
Javascript
let x = 0;
let y = 0;
let z = 0;
function setup() {
createCanvas(400, 400);
if (window.DeviceOrientationEvent) {
window.addEventListener( "deviceorientation" ,
onOrientationChange);
}
}
function draw() {
background(255, 255, 255);
angleMode(DEGREES);
rectMode(CENTER);
translate(width / 2, height / 2);
rotate(y);
let c = color( "green" );
fill(c);
rect(0, 0, 100, 200);
}
function onOrientationChange(e) {
x = e.x;
y = e.y;
z = e.z;
console.log( "X:" , x, "Y:" , y, "Z:" , z);
}
|
Output:
Share your thoughts in the comments
Please Login to comment...