1

I'm running into problems loading my JS game onto a div, which ultimately should show the game below the header, however it's showing below the footer instead. Anyone know how I can fix this? Thanks in advance. How It's Supposed to Look

JS FILE

var animate = window.requestAnimationFrame ||
  window.webkitRequestAnimationFrame ||
  window.mozRequestAnimationFrame ||
  function(callback) { window.setTimeout(callback, 1000/60) };

var canvas = document.createElement('canvas');
var width = 400;
var height = 600;
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
var topScore = 0;
var bottomScore = 0;
var game1 = document.getElementById("game");

window.onload = function() {
  document.body.appendChild(canvas);
  animate(step);

};

var step = function() {
  update();
  render();
  animate(step);
};

var update = function() {
};

var render = function() {
  context.fillStyle = "#ffd31d";
  context.fillRect(0, 0, width, height);
};

HTML FILE

 <div class="game"></div>
 <script src="pong.js"></script>
1
  • You are appending to the body with document.body.appendChild(canvas);. Append somewhere else. You may want to use parentElement.insertBefore(canvas, beforeNode);. Commented Jun 12, 2020 at 0:32

1 Answer 1

1

Instead of

document.body.appendChild(canvas);

you need to append it to the div

game1.appendChild(canvas);

appending it to the body will just add it after everything else on the page.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I see the logic and that makes sense. However, it's still not appearing.
Do you have a git repo or another way to show me your code?
github.com/tatedewey/WildinOut Thanks for looking into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.