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>

Recommended Answers

All 4 Replies

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')

?

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];

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.