上下文没有定义,javascript

时间:2014-08-01 14:48:40

标签: javascript box2dweb

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);
  }

}

所以我有上面的代码,我得到一个参考错误上下文没有定义。 这是为什么?我的意思是我写道:

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

但看起来它不起作用......为什么?我错过了什么?是不是.getContext写得不正确?

0 个答案:

没有答案