Intro to JS: Day 2

Goals for the day

Fixed time interval (Old method)

Fixed Timing (demo)
// inside move()
  ball.x += ball.dx/fps;
  ball.y += ball.dy/fps;

  // adjust max value of i to slow this down
  for (var i=0;i<10000000;i++) { Math.sqrt(2) }

Timing based on last_time

// in global scope
var last_time, elapsed_time, now;

// in the top of tick()
  now = new Date().valueOf();
  elapsed_time = (now-last_time)/1000;
  last_time = now;

//in move()
  ball.x += ball.dx*elapsed_time;
  ball.y += ball.dy*elapsed_time;

  // move padle                                                                                                              
  if (leftDown) {
    paddle.x -= paddle.dx*elapsed_time;
    paddle.x = Math.max(0,paddle.x);
  }
  if (rightDown) {
    paddle.x += paddle.dx*elapsed_time;
    paddle.x = Math.min(WIDTH-paddle.w,paddle.x)
  }

// in start()
  now = new Date().valueOf();
  last_time = new Date().valueOf();

Timing based on last_time (demo)

requestAnimationFrame(function)

// last line of tick!
  current_frame = requestAnimationFrame(tick);

function stop() {
  cancelAnimationFrame(current_frame);
} 

function start() {
  var current_frame = requestAnimationFrame(tick);
}

Demo: requestAnimationFrame(function)

// last line of tick!
  current_frame = requestAnimationFrame(tick);

function stop() {
  cancelAnimationFrame(current_frame);
}

function start() {
  var current_frame = requestAnimationFrame(tick);
}

Sprites

Sprites: Code

// new function brick(b);
var brick_sprites = document.createElement("img");
brick_sprites.src = "brick_sprites.png";
function draw_brick(b) {
  context.drawImage(brick_sprites,b.sx,b.sy,b.sw,b.sh,b.x,b.y,b.w,b.h);
}

//modified createBricks
function createBricks(o) {
  var out = [], x, y, col_max, color;
  var sw = 16;
  var sh = 8;
  var sprite_rows = [0,1,2,3,4,5,6,7];
  for (var row=0; row<o.rows; row++) {
    col_max = Math.floor(o.canvas.width / (o.w+o.separation));
    if (row%2 == 0) { col_max -=1; }
    y = (row+3)*(o.h + o.separation) + o.separation;
    for (var col=0; col<col_max; col++) {
      x = col*(o.separation + o.w) + o.separation;
      if (row%2 == 0) { x += o.w/2; }
      var sprite_row = sprite_rows[Math.floor((row+col)%sprite_rows.length)];
      sprite_col = Math.floor(Math.random()*5);
      var brick = {
        sx: sprite_col*sw,
        sy: sprite_row*sh,
        sw: sw,
        sh: sh,
        x: x, y: y, w: o.w, h: o.h, broken: false //old options
      };
      out.push(brick);
    }
  }
  return out;
}

//in draw instead of rect(b)
draw_brick(b)

Sprites: Demo

More Human Interaction: onmousemove

// Mouse Input
//-------------------

function onMouseMove(event) {
  paddle.x = event.layerX-paddle.w/2;
}
canvas.addEventListener("mousemove",onMouseMove);

More Human Interaction: tilt sensor

// Accelerometer
//-----------------
function tilt(bg) {
  var max_angle = 60;
  var angle = bg[1]+max_angle/2;
  paddle.x = angle/max_angle*WIDTH;
  paddle.x = Math.min(paddle.x,WIDTH - paddle.w);
  paddle.x = Math.max(paddle.x,0);
}

if (window.DeviceOrientationEvent) {
    window.addEventListener("deviceorientation", function () {
        tilt([event.beta, event.gamma]);
    }, true);
} else if (window.DeviceMotionEvent) {
    window.addEventListener('devicemotion', function () {
        tilt([event.acceleration.x * 2, event.acceleration.y * 2]);
    }, true);
} else {
    window.addEventListener("MozOrientation", function () {
        tilt([orientation.x * 50, orientation.y * 50]);
    }, true);
}

HTML5 Sound

function Sound(src,repeat) {
  var elements = [];
  for (var i=0;i<repeat;i++) {
    var audio = document.createElement("audio");
    var mp3 = document.createElement("source");
    mp3.src = src + ".mp3";
    audio.appendChild(mp3);
    var wav = document.createElement("source");
    wav.src = src + ".wav";
    audio.appendChild(wav);
    elements.push(audio);
  }
  var last_played = 0;
  function play() {
    last_played +=1;
    if (last_played == repeat) {
      last_played = 0;
    }
    elements[last_played].play();
  }
  function stop() { elements[last_played].stop(); }
  function set_volume(level) {
    for (var i=0;i<elements.length;i++) { elements[i].volume=level; }
  }
  return {
    play: play,
    stop: stop,
    set_volume: set_volume
  }
}

var beep = new Sound("beep",8);
beep.set_volume(0.5);

// anywhere you want a beep
  beep.play()

HTML5 Sound (demo)

repeat = 1
repeat = 8

Host it on Github

Thanks for taking this class!

What now?

/