Skip to content

Instantly share code, notes, and snippets.

@duduindo
Last active March 19, 2021 04:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duduindo/93e89a508ebf786b2aae40cbfae03adc to your computer and use it in GitHub Desktop.
Save duduindo/93e89a508ebf786b2aae40cbfae03adc to your computer and use it in GitHub Desktop.
Web APIs que você provavelmente não sabia que existiam - Zeno Rocha - https://youtu.be/851mizGnr_Y

Evento Visibility Change

Exemplos:

Com document:

document.addEventListener("visibilitychange", function() {
  console.log( document.visibilityState );
});

Com window:

window.addEventListener("visibilitychange", function() {
  switch(document.visibilityState) {
	  case 'prerender':
		  console.log('Tab is pre-rendering');
		  break;
	  case 'hidden':
		  console.log('Tab is hidden');
		  break;
	  case 'visible':
		  console.log('Tab is focused');
		  break;
  }
});

Casos de uso: Exemplos do Zeno no vídeo: https://youtu.be/851mizGnr_Y?t=456 (OBS: Tempo certo)

  • Pausar/Retomar carrousel
  • Pausar/Retomar vídeos
  • etc...

Web Share

Exemplos:

shareButton.addEventListener('click', () => {
   navigator.share({
     title: document.title,
     text: 'Hello World',
     url: window.location.href,
   })
   	.then(() => console.log('Successful share'))
   	.catch(error => console.log(error));
});

Print

Imgur

Online State

window.addEventListener('online',  updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);

function updateOnlineStatus(e) {
	console.log(e.type);
}

Download speed

navigator.connection.downlink // 9.1 (Trabalho)

Device Orientation

window.addEventListener('deviceorientation', e => {
	let tiltLR = e.gamma;
	let tiltFB = e.beta;
	console.log('Gamma:', e.gamma);
	console.log('Beta:', e.beta);
	console.log('Alpha:', e.alpha);
	
	// Exemplo do Zeno
	// Tempo do vídeo: https://youtu.be/851mizGnr_Y?t=883
	logo.style.transform = `rotate(${tiltLR}deg) rotate3d(1, 0, 0, ${tiltFB * -1}deg) `;
});

Caso de uso:

  • Imagem 3D do Facebook

Ambient Light

Evento que retorna o sensor de luz do device

window.addEventListener('devicelight', function(event) {
  console.log(`${event.value} lux`);
});

Caso de uso:

Battery Status

OBS: API Obsoleto

let batteryIsCharging = false;

navigator.getBattery().then(function(battery) {
  batteryIsCharging = battery.charging;

  battery.addEventListener('chargingchange', function() {
    batteryIsCharging = battery.charging;
  });
});

WEB VR

Realidade Virtual

Shape detection

Detecta rostos

const faceDetector = new FaceDetector();

faceDetector.detect(image).then(faces => {
	console.log('Face found:', faces.length);
}).catch(err => {
	console.error(err);
});

Web Assembly

Gamepad

Controlar a página web usando controle

Retirado do vídeo:

window.addEventListener('gamepadconnected', () => {
	const gp = navigator.getGamepads()[0];

	console.log('ID:', gp.id);
	console.log('Axes:', gp.axes.length);
	console.log('Buttons:', gp.buttons.length);
});
window.addEventListener('gamepadconnected', () => {
	const gp = navigator.getGamepads()[0];

	if (gp.buttons[0].pressed) {
		console.log('X');
	}
	
	requestAnimationFrame(gameloop);
});

Retirado do MDN:

window.addEventListener("gamepadconnected", function(e) {
  console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
    e.gamepad.index, e.gamepad.id,
    e.gamepad.buttons.length, e.gamepad.axes.length);
});

Web Components

  • Templates
  • HTML imports
  • Custom elements
  • Shadom DOM

Progressive Web Apps

  • Push notifications
  • App manifest
  • Offline support
  • Payment request
  • Service workers
  • Background sync

fim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment