0

hello,

what is that?

everytime it works, but this time it does not

$(function() {  //jquery doc ready

    world = createWorld(); // box2DWorld  
    ctx = $('#game').getContext('2d'); // 2  
    var canvasElm = $('#game');  

    return;

    canvasWidth = parseInt(canvasElm.width);  
    canvasHeight = parseInt(canvasElm.height);  
    initGame(); // 3  
    step(); // 4  
// 5  
    window.addEventListener('keydown',handleKeyDown,true);  
    window.addEventListener('keyup',handleKeyUp,true);  
});

What is wrong here? WHy no get context?
and html:

<html>
    <body>


<head>
<!--[if IE]><script src="lib/excanvas.js"></script><![endif]-->  
 <!--<script src="libs/prototype-1.6.0.2.js"></script>  -->

 <!-- <script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script> -->
 <script type="text/javascript" src="libs/jquery-1.7.1.min.js"></script>
<!-- box2djs -->  
  <script src="libs/Box2dWeb-2.1.a.3.min.js"></script>  

  <script src='js/box2dutils.js'></script>  
  <script src='js/game.js'></script>  
</head>

  <canvas id="game" width="600" height="400"></canvas>  
  </body>
 </html>
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by McLaren
0
var canvasElm = document.getElementById('game');  
    ctx = canvasElm.getContext('2d'); // 2  

Found out that without jqueyr it works. This does not make sence. What is the differecnec between

document.getElementById('game');

and

$('#game')

?

0

When using jQuery, the $('#game') selector selects ALL elements with the game ID and does not give you the actual DOM element to allow you to do this. jQuery exposes the actual DOM elements by numeric index to allow regular DOM/Javascript functions:

var canvasElm = $('#game')[0];

0

Sorry just looked at the rest of your code and noticed you use the cavasElm for other things as well so you'd be better off using the index in your actual getContext call:

ctx = $('#game')[0].getContext('2d');
var canvasElm = $('#game');

or

var canvasElm = $('#game');
ctx = canvasElm[0].getContext('2d'); // 2
0

thanks. now how to not forget this next time :D but at least I will maybe remember this thread :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.