Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
229,744 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
229,744 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?