SlideShare a Scribd company logo
1 of 32
Download to read offline
DEVICE ORIENTATION &
   WEBSOCKET API
 Approfondimenti, scoperte ed esperimenti
LEVEL 0
 Basics
COORDINATE
First: Heading ~ Alpha
COORDINATE
Second: Attitude ~ Beta
COORDINATE
Third: Bank ~ Gamma
COORDINATE
                            Tryout



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);
}
LEVEL 1
Experimenting with heading
A BIRD IN A CAGE
      Heading come rotazione




<body>
    <div>
        <img src="img/bird">
        <img src="img/cage">
    </div>
</body>
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);
}
A BIRD IN A CAGE
                Heading come rotazione



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

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




-webkit-transform-origin: 225px 150px 400px;
LEVEL 2
Costruire un cubo
A CUBE
                HTML 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>
A CUBE
                       HTML and CSS



body{
    -webkit-perspective: 800px;
}
div#container{
    position: relative;
    width: 800px;
    height: 800px;
    -webkit-transform-style: preserve-3d;
    -webkit-transform: translateZ(-800px);
}
A CUBE
                          HTML 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);
}
A CUBE
HTML and CSS
A CUBE
                    Let’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) ";
}
A CUBE
                Cosa Succede se...




body{
    -webkit-perspective: 800px;
}
div#container{
    -webkit-transform: translateZ(800px);
}
A CUBE
Aggiungiamo una cubemap
LEVEL 3
WebSocket
PREFERISCI CHUCK O DART?
IL MASTER
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 SERVER

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 msg
end
IL CLIENT

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){
    ...
}
LEVEL 4
Uniamo i pezzi
IL CLIENT

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 SERVER
                    Usando JSON




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)";
}
LEVEL 5
molto molto molto sperimentale
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
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;
    }
}
THANKS!
         www.compartoweb.com
@compartoweb ~ facebook.com/compartoweb

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Device Orientation & WebSocket API

  • 1. DEVICE ORIENTATION & WEBSOCKET API Approfondimenti, scoperte ed esperimenti
  • 6. COORDINATE Tryout 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); }
  • 8. A BIRD IN A CAGE Heading come rotazione <body> <div> <img src="img/bird"> <img src="img/cage"> </div> </body>
  • 9. 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); }
  • 10. A BIRD IN A CAGE Heading come rotazione window.addEventListener ("deviceorientation", traccia, false); function traccia(evento){ document.querySelector('div') .setAttribute('style','-webkit- transform: rotateY(' + evento.alpha + 'deg);'); }
  • 11. ROTATING PICTURE Questione di origin -webkit-transform-origin: 225px 150px 400px;
  • 13. A CUBE HTML 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>
  • 14. A CUBE HTML and CSS body{ -webkit-perspective: 800px; } div#container{ position: relative; width: 800px; height: 800px; -webkit-transform-style: preserve-3d; -webkit-transform: translateZ(-800px); }
  • 15. A CUBE HTML 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); }
  • 17. A CUBE Let’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) "; }
  • 18. A CUBE Cosa Succede se... body{ -webkit-perspective: 800px; } div#container{ -webkit-transform: translateZ(800px); }
  • 22. IL MASTER 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; }
  • 23. IL SERVER 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 msg end
  • 24. IL CLIENT 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){ ... }
  • 26. IL CLIENT 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 })); }
  • 27. IL SERVER Usando JSON 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)"; }
  • 28.
  • 29. LEVEL 5 molto molto molto sperimentale
  • 30. 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
  • 31. 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; } }
  • 32. THANKS! www.compartoweb.com @compartoweb ~ facebook.com/compartoweb