device orientation & websocket api

32
DEVICE ORIENTATION & WEBSOCKET API Approfondimen, scoper ed esperimen

Upload: comparto-web

Post on 10-Jun-2015

738 views

Category:

Technology


0 download

DESCRIPTION

Esperimenti e demo basati sull'utilizzo delle Device Orientation API e dei WebSocket.

TRANSCRIPT

Page 1: Device Orientation & WebSocket API

DEVICE ORIENTATION & WEBSOCKET API

Approfondimenti, scoperte ed esperimenti

Page 2: Device Orientation & WebSocket API

LEVEL 0Basics

Page 3: Device Orientation & WebSocket API

COORDINATEFirst: Heading ~ Alpha

Page 4: Device Orientation & WebSocket API

COORDINATESecond: Attitude ~ Beta

Page 5: Device Orientation & WebSocket API

COORDINATEThird: Bank ~ Gamma

Page 6: Device Orientation & WebSocket API

COORDINATETryout

window.addEventListener("deviceorientation", traccia, false);

function traccia(evento){ document.querySelector('div.alpha span')

.innerHTML = Math.round(evento.alpha); document.querySelector('div.beta span')

.innerHTML = Math.round(evento.beta); document.querySelector('div.gamma span')

.innerHTML = Math.round(evento.gamma);}

Page 7: Device Orientation & WebSocket API

LEVEL 1Experimenting with heading

Page 8: Device Orientation & WebSocket API

A BIRD IN A CAGEHeading come rotazione

<body> <div> <img src="img/bird"> <img src="img/cage"> </div></body>

Page 9: Device Orientation & WebSocket API

A BIRD IN A CAGE

div{ width: 400px; height: 400px; position: relative; -webkit-transform-style: preserve-3d;}img{ -webkit-backface-visibility: hidden; display: block; position: absolute; top: 0px; left: 0px; width: 400px; height: 400px;}img:last-child{ -webkit-transform: rotateY(180deg);}

Page 10: Device Orientation & WebSocket API

A BIRD IN A CAGEHeading come rotazione

window.addEventListener ("deviceorientation", traccia, false);

function traccia(evento){ document.querySelector('div') .setAttribute('style','-webkit-transform: rotateY(' + evento.alpha + 'deg);');}

Page 11: Device Orientation & WebSocket API

ROTATING PICTUREQuestione di origin

-webkit-transform-origin: 225px 150px 400px;

Page 12: Device Orientation & WebSocket API

LEVEL 2Costruire un cubo

Page 13: Device Orientation & WebSocket API

A CUBEHTML and CSS

<div id="container"> <div class="square bottom"></div> <div class="square left"></div> <div class="square right"></div> <div class="square front"></div> <div class="square top"></div> <div class="square back"></div></div>

Page 14: Device Orientation & WebSocket API

A CUBEHTML and CSS

body{ -webkit-perspective: 800px;}div#container{ position: relative; width: 800px; height: 800px; -webkit-transform-style: preserve-3d; -webkit-transform: translateZ(-800px);}

Page 15: Device Orientation & WebSocket API

A CUBEHTML and CSS

div.square{ position: absolute; top: 0px; left: 0px; width: 800px; height: 800px;} div.square.bottom{ background: red; -webkit-transform: rotateX(90deg) translateZ(-400px);}

Page 16: Device Orientation & WebSocket API

A CUBEHTML and CSS

Page 17: Device Orientation & WebSocket API

A CUBELet’s animate it!

function traccia(evento){ document.getElementById('container').style['-webkit-transform'] = "translateZ(-800px)" + "rotateY(" + ( -evento.gamma ) + "deg) " + "rotateX(" + evento.beta + "deg) " + "rotateZ(" + ( evento.alpha ) + "deg) ";}

Page 18: Device Orientation & WebSocket API

A CUBECosa Succede se...

body{ -webkit-perspective: 800px;}div#container{ -webkit-transform: translateZ(800px);}

Page 19: Device Orientation & WebSocket API

A CUBEAggiungiamo una cubemap

Page 20: Device Orientation & WebSocket API

LEVEL 3WebSocket

Page 21: Device Orientation & WebSocket API

PREFERISCI CHUCK O DART?

Page 22: Device Orientation & WebSocket API

var socket = new WebSocket("ws://0.0.0.0:8080"); socket.addEventListener("open", registratiMaster, false);socket.addEventListener("message", stampaMessaggio, false); function registratiMaster(evento){ socket.send('register master'); }function stampaMessaggio(evento){ console.log(evento.data); var elemento = document.querySelector('span#' + evento.data); if (elemento != undefined) elemento.innerHTML = parseInt(elemento.innerHTML) + 1;}

IL MASTER

Page 23: Device Orientation & WebSocket API

ws.onmessage do |msg| case msg when 'register master' puts "the master is ready" @channel.subscribe { |m| ws.send m } else puts "sending #{msg} to master" @channel.push msg end ws.send msgend

IL SERVER

Page 24: Device Orientation & WebSocket API

var socket = new WebSocket("ws://<%= @ip %>:8080"); socket.addEventListener("open", prontoPerVotare, false);socket.addEventListener("message", nuovoMessaggio, false); function prontoPerVotare(){ ... document.querySelector('a').addEventListener('click', vota, false);} function vota(evento){ socket.send(evento.target.getAttribute('data-vote'));} function nuovoMessaggio(evento){ ...}

IL CLIENT

Page 25: Device Orientation & WebSocket API

LEVEL 4Uniamo i pezzi

Page 26: Device Orientation & WebSocket API

var socket = new WebSocket("ws://<%= @ip %>:8080"); socket.addEventListener("open", prontoPerMuovere, false);socket.addEventListener("message", nuovoMessaggio, false); function prontoPerMuovere(){ window.addEventListener ("deviceorientation", muovi, false);} function muovi(evento){ socket.send(JSON.stringify({ alpha: evento.alpha, beta: evento.beta, gamma: evento.gamma }));}

IL CLIENT

Page 27: Device Orientation & WebSocket API

function stampaMessaggio(evento){ var angoli = JSON.parse(evento.data); document.getElementById('container').style['-webkit-transform'] = "translateZ(-400px) " + "rotateZ(" + ( -angoli.gamma ) + "deg) " + "rotateX(" + ( -angoli.beta) + "deg) " + "rotateY(" + ( angoli.alpha - 180 ) + "deg)";}

IL SERVER Usando JSON

Page 28: Device Orientation & WebSocket API
Page 29: Device Orientation & WebSocket API

LEVEL 5molto molto molto sperimentale

Page 30: Device Orientation & WebSocket API

ws.onmessage do |msg| data = JSON.parse(msg); case data['cmd'] when 'register_master' @masterchannels << EM::Channel.new @masterchannels.last.subscribe { |m| ws.send m } ws.send( { cmd: :your_id, value: @masterchannels.size - 1 }.to_json ) puts "added master nr #{@masterchannels.size - 1}" when 'register_client' if !data['value'].nil? @clients << { master_id: ( id = data['value']), channel: EM::Channel.new } @clients.last[:channel].subscribe { |m| ws.send m } @masterchannels[id].push( { cmd: :create_id, value: @clients.size - 1 }.to_json ) ws.send( { cmd: :your_id, value: @clients.size - 1 }.to_json ) puts "added #{@clients.size - 1} to master #{id}" end when 'position', if !data['ship_id'].nil? @masterchannels[@clients[data['ship_id']][:master_id]].push msg end end

Page 31: Device Orientation & WebSocket API

function stampaMessaggio(evento){ var data = JSON.parse(evento.data); switch(data.cmd) { case 'your_id': document.querySelector('h1 > span').innerText = data.value break; case 'create_id': setupShip(data.value); break; case 'position': moveShip( document.querySelector('figure[data-id="' + data.ship_id + '"]'), data.value ); break; }}

Page 32: Device Orientation & WebSocket API

THANKS!www.compartoweb.com

@compartoweb ~ facebook.com/compartoweb