當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js deviceMoved()用法及代碼示例

函數deviceMoved()在設備中工作,當設備沿X、Y或Z三個軸移動超過閾值時調用。

它僅適用於手機等設備,它有助於檢測設備何時移動或旋轉超過閾值。

它可以用作移動設備中的傳感器,例如檢測運動、加速度、旋轉、航向和位置。

默認閾值設置為 0.5,可以使用 setMoveThreshold() 函數更改。

用法:



deviceMoved()

現在我們將在安卓手機上運行一些例子。

第一步:在手機中使用任意瀏覽器打開p5.js的在線網頁編輯器“https://editor.p5js.org/”

步驟2:在編輯器部分編寫以下代碼並運行它以查看輸出。

範例1:

Javascript


// Set value to zero
let value = 0;
  
// Set the draw function
function draw() {
  
    // When the device is moved
    // in the all direction then
    // the colour fill filled
    fill(value);
  
    // Set the shape of thr Graphics
    triangle(45, 100, 54, 5, 100, 100);
}
  
// Apply the deviceMoved function
function deviceMoved() {
  
    // Increment the value everytime by 10
    value = value + 10;
  
    // If the value become grater than 255
    // then reset the value to zero.
    if (value > 255) {
        value = 0;
    }
}

輸出:

範例2:

Javascript


// Set the variable as 0
var value = 0;
  
// Set the function
function setup() {
    createCanvas(windowWidth, windowHeight);
  
    // Set the background as value which
    // will be dynamic
    background(value);
}
  
// Set the draw function
function draw() {
  
    // Set the properties of text
    background(value);
    fill(0, 0, 255);
    textAlign(CENTER, CENTER);
    textSize(25);
  
    // Set the limit for value
    value = constrain(value - 2, 0, 200)
  
    // If the value is greater than 10
    if (value > 10) {
        text("Moving Device", width / 2, height / 2);
    } else {
        text("Device is Relaxed ", width / 2, height / 2);
    }
}
  
function deviceMoved() {
  
    // Now increase the value.
    value = constrain(value + 5, 0, 255)
}

輸出:




相關用法


注:本文由純淨天空篩選整理自_sh_pallavi大神的英文原創作品 p5.js deviceMoved() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。