0
function moveCamera() {

  var canvas_holder = document.getElementById("game");
  var context = canvas_holder.getContext("2d");

  // camera code
  // START
  // Viewport height
  var eye = $("#container").height();

  // canvas height
  var can_height = $("#canvas").height();

  // Initial pos. of car. Multiplied by 30 to convert it into pixels.
  // CHECK BETTER FOR CONVERTION 
  var car_pos = (car.GetWorldCenter().y)*30;
  var half_eye = (eye/2);

  // Initial Settings of camera
  if ((car_pos > half_eye) && (can_height - car_pos) > half_eye){
    context.translate(-(car_pos-half_eye),0);
  } else if ((can_height - car_pos) < half_eye){
    context.translate(-(can_height - eye), 0);
  }
  var pos = new Array();
  var length;

  // initial position of object is stored
  pos[0] = (car.GetWorldCenter().y)*30;

  //move camera
  //vector is defined which will store the current y-coordinate
  var p = new b2Vec2();
  p = (car.GetWorldCenter().y)*30;
  pos.push(p);
  var length = pos.length;

  // in pexels. finds out the distance moved in one timestep
  var s = (pos[length-1]-pos[length-2]);

  // checks if object is not in eye/2. Thus can be translated
  if ((half_eye < (can_height - p)) && (p > half_eye)){

    // canvas is translated
    context.translate(-s,0);
  }

}

so i have the code above and i get a reference error context is not defined. why is that? i mean i did wrote:

var canvas_holder = document.getElementById("game");
var context = canvas_holder.getContext("2d");

but it looks like it not working... why? what am i missing? can it be that .getContext is not written properly?

user3676606
  • 13
  • 1
  • 6
  • would you be kind enough to tell me why getContext returns undefined? – user3676606 Aug 01 '14 at 14:53
  • Do you have an element with `id="game"` in your HTML? After you've declared `canvas_holder` in the code, what is output in the console if you wrote `console.log(canvas_holder);`? – Andy Aug 01 '14 at 14:55
  • Which browser are you using? – runTarm Aug 01 '14 at 14:56
  • my canvas tag has an id="game". if i write console.log(canvas_holder); it will output correctly my canvas tag. i am using latest mozilla (although on ubuntu) – user3676606 Aug 01 '14 at 15:01
  • ok i made some progress. one mistake i made is that var can_height = $("#canvas").height(); should be var can_height = $("#game").height(); However now i don't get any type of error but for a reason nothing is rendered.... – user3676606 Aug 02 '14 at 09:24

0 Answers0