HTML5 Mobile

some experiences

Premise

Animations

a short one

Smaller Animations

jQuery vs. CSS3

//Old Way
$('.le-animate').animate({
  width: 300px,
  height: 400px,
  opacity: 1
}, 3000);
//New Way
.le-animate {
  width: 0px; height: 0px; opacity: 0;
  -webkit-transition: all 3s ease-in;
     -moz-transition: all 3s ease-in;
      -ms-transition: all 3s ease-in;
          transition: all 3s ease-in;
}

.le-animate.anim {
  width: 300px; height: 400px; opacity: 1;
}
$('.le-animate').addClass('anim');

BUT!

Galaxy Tab!

Canvas

For more complex animations, just use canvas, right? It's supported everywhere

Flying pigs (by @codepo8) on iOS

Flying pigs (by @codepo8) on IE 9 Mobile

Flying pigs (by @codepo8) on Android

In a nutshell

Orientation

also a short one

Landscape and Portrait

@media only screen and (orientation: portrait)
@media only screen and (orientation: landsacpe)
window.onorientationchange = function() {
  var orientation = window.orientation; // orientation in degrees
  switch(orientation) {
    case 0:   //iOS portrait
      ...
      break;
    case 90:  //iOS landscape
      ...
      break;
    case -90:
      ... 
      break;
  }
}
        

But ...

Better

window.onorientationchange = function() {
  var mql = window.matchMedia("(orientation: portrait)");
  if(mql.matches) {
    // do portrait stuff
  } else {
    // do landscape stuff
  }
}

Best

var mql = window.matchMedia("(orientation: portrait)");
mql.addEventListener(function(m) { 
  if(m.matches) {
    // do portrait stuff
  } else {
    // do landscape stuff
  }
})

Drawback: Is not implemented on all systems (iOS4, Android 2.3)

So in worst case, you have to rely on UA sniffing (urks) or some dirty, dirty tricks

HTML5 Audio

HTML5 Audio in general

HTML5 Audio on mobile devices

First rule: Don't think the support is wide-spread or stable, even if those lists out there say so

Forever alone

Be aware that you can only play one sound at a time, so if you want to have BG music and effects, forget it

THERE CAN BE ONLY ONE

And most important...

iOS devices (and most mobile webkit browsers, like Android) only allow playing sound directly after a user interaction. So

But, the iOS JS engine allows API calls if a user interaction happened, or if the file already has been downloaded completely

Common workaround

Example

<audio src="sprite.mp3" controls="none" id="myaudio" />
var maudio = document.getElementById('myaudio');

var soundSprite = [
  {start: 0, end: 3000},
  {start: 3500, end: 6789}             
];

element.addEventListener('touchstart', function(event) {
  maudio.play();
  playSoundFile(0);
})

function playSoundFile(idx) {
  maudio.currentPosition = soundSprite[idx].start;
  var x = setInterval(function() {
    if(maudio.currentPosition >= soundSprite[idx].end) {
      maudio.pause(); // There is no stop() in HTML5
      clearInterval(x);
    }
  }, 50);
}

(Almost) last words

Libs like SoundManager2 are great for exactly this task

Do not forget

@ddprrt | http://github.com/ddprrt
stefan.baumgartner@netural.com