user2138152
user2138152

Reputation: 131

Javascript set <canvas> to window size

I am attempting to set a canvas element to the size of the current window.

I did so successfully with jquery here:

function windowsize() {
    WIDTH = $("#canvas")[0].width = ($(window).width());
    HEIGHT = $("#canvas")[0].height = ($(window).height());

}

windowsize();

I cannot accomplish the same thing in plain javascript, I have as follows:

function windowsize(){
    var ctx = document.getElementbyID("canvas");
    ctx.canvas.width = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
}
windowsize();

Here is a fiddle with the above: http://jsfiddle.net/Kinetic915/bWYy7/

I was also using the following page as a reference:

Resize HTML5 canvas to fit window

Thanks!

Upvotes: 0

Views: 2265

Answers (1)

Alex
Alex

Reputation: 76

Typo:

document.getElementbyID

should be:

document.getElementById

and ctx has no canvas property

Updated Example: http://jsfiddle.net/bWYy7/3/

Upvotes: 2

Related Questions