rllynotfox
rllynotfox

Reputation: 65

Why window.innerWidth is wrong?

I'm trying to make a canvas JavaScript game, so i added this in code

window.addEventListener("resize", () => {
  console.log("resi2ze");
  canvasElement.width = window.innerWidth;
  canvasElement.height = window.innerHeight;
});

But when I resize browser window, canvas height is bigger than body height (for example, body height is 465px, and canvas height is 462px). How can I prevent it, so canvas height would be same as window height? image example

Upvotes: 0

Views: 631

Answers (1)

Konrad
Konrad

Reputation: 24661

Seems like the scrollbar itself is an issue, because it adds few pixels. I always add overflow: hidden to body so scrollbar doesn't show up.

Upvotes: 1

Related Questions