Difference between revisions of "P5js get Battery Level"

From Digipool-Wiki
Jump to: navigation, search
(Created page with "<pre> // Get Battery Level // Read the current battery level with P5*js var bl; function setup() { frameRate(2); createCanvas(400, 400); fill(0); textAlign(CENTER);...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 +
Siehe W3C Battery Status API — [https://w3c.github.io/battery/#dom-navigator-getbattery LINK]
 +
 
<pre>
 
<pre>
  
Line 25: Line 27:
 
}
 
}
  
 +
</pre>
 +
 +
<br>
 +
 +
<pre>
 +
 +
// Get Battery Level and Charching Status
 +
// Read the current battery level with P5*js
 +
 +
var bl, bc;
 +
 +
function setup() {
 +
  frameRate(2);
 +
  createCanvas(400, 400);
 +
  fill(0);
 +
  textAlign(CENTER);
 +
}
 +
 +
function draw() {
 +
  navigator.getBattery().then(function (battery) {
 +
    bl = battery.level;
 +
    bc = battery.charging;
 +
  });
 +
 +
  background(255);
 +
  textSize(24);
 +
  text("Your current battery level is", width / 2, height / 2 - 35);
 +
  textSize(64);
 +
  text(bl * 100 + " %", width / 2, height / 2 + 35);
 +
  textSize(24);
 +
  if(bc){
 +
    text('charching', width / 2, height / 2 + 80);
 +
  }else{
 +
    text('not charching', width / 2, height / 2 + 80);
 +
  }
 +
}
  
 
</pre>
 
</pre>

Latest revision as of 14:00, 8 May 2021

Siehe W3C Battery Status API — LINK


// Get Battery Level
// Read the current battery level with P5*js

var bl;

function setup() {
  frameRate(2);
  createCanvas(400, 400);
  fill(0);
  textAlign(CENTER);

  navigator.getBattery().then(function (battery) {
    bl = battery.level;
  });
}

function draw() {
  background(255);
  textSize(24);
  text("Your current battery level is", width / 2 , height / 2 - 35);
  textSize(64);
  text(bl * 100 + " %", width / 2, height / 2 + 35);
}



// Get Battery Level and Charching Status
// Read the current battery level with P5*js

var bl, bc;

function setup() {
  frameRate(2);
  createCanvas(400, 400);
  fill(0);
  textAlign(CENTER);
}

function draw() {
  navigator.getBattery().then(function (battery) {
    bl = battery.level;
    bc = battery.charging;
  });

  background(255);
  textSize(24);
  text("Your current battery level is", width / 2, height / 2 - 35);
  textSize(64);
  text(bl * 100 + " %", width / 2, height / 2 + 35);
  textSize(24);
  if(bc){
    text('charching', width / 2, height / 2 + 80);
  }else{
    text('not charching', width / 2, height / 2 + 80);
  }
}