Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML

Jason Lee
Jason Lee
54 Points

HTML5 GAME - context.fillRect

Same question as everyone else: Rectangles won't draw and image doesn't show on canvas!

index.html: <!DOCTYPE html> <html> <head> <title>Game</title> <link rel="stylesheet" href="style.css"> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script src="game.js"</script> </body> </html>

style.css: body { background: #999999; } canvas { display: block; background: #ffffff; margin: 0 auto; }

game.js: var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d");

imgFrog = new Image(); imgFrog.src = "images/mikethefrog.png"; imgFrog.addEventListener("load",init, false);

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

var frogX = 65; var frogY = 65;

function init() { requestAnimFrame(update); }

function update() {

frogY = frogY + 3;
context.clearRect (0, 0, 400, 400);
context.drawImage(imgFrog, frogX, frogY, 100, 77);
context.fillRect( 10, 10, 40, 380, "#000000");
context.fillRect( 10, 10, 380, 40, "#000000");
context.fillRect( 10, 350, 380, 40, "#000000");
context.fillRect( 350, 10, 40, 380, "#000000");
context.fillRect( 180, 10, 40, 180, "#000000");

requestAnimFrame(update);

}

Steven Parker
Steven Parker
221,297 Points

It can be hard to see the issue without the complete project, particularly when images are involved. You can share everything at once if you make a snapshot of your workspace and provide the link to it.

1 Answer

Steven Parker
Steven Parker
221,297 Points

It works fine for me! After I replaced the missing frog image file, I see the black container made of rectangles and the frog image moves.

And you believe everyone else has this same issue?