未定义上下文,javascript
但它看起来好像不起作用。。。为什么?我错过了什么?是不是。getContext编写得不正确?您能告诉我为什么getContext返回未定义的值吗?您的HTML中有带未定义上下文,javascript,javascript,box2dweb,Javascript,Box2dweb,但它看起来好像不起作用。。。为什么?我错过了什么?是不是。getContext编写得不正确?您能告诉我为什么getContext返回未定义的值吗?您的HTML中有带id=“game”的元素吗?在代码中声明canvas\u holder后,如果编写console.log(canvas\u holder),控制台中会输出什么?您正在使用哪个浏览器?我的画布标签上有一个id=“game”。如果我写console.log(canvas_holder);它将正确输出我的画布标记。我正在使用最新的mozil
id=“game”
的元素吗?在代码中声明canvas\u holder
后,如果编写console.log(canvas\u holder),控制台中会输出什么代码>?您正在使用哪个浏览器?我的画布标签上有一个id=“game”。如果我写console.log(canvas_holder);它将正确输出我的画布标记。我正在使用最新的mozilla(虽然在ubuntu上)好的,我取得了一些进展。我犯的一个错误是var can_height=$(“#canvas”).height();应该是var can_height=$(“#游戏”).height();然而,现在我没有得到任何类型的错误,但由于某种原因,没有呈现任何内容。。。。
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");