window.innerWidth vs innerWidth
Swebd posted 12 days ago

Hi Chris

First of all my compliments for the construction of the videos and website!

I have a question about innerWidth.

At the top of the code, use

canvas.width = window.innerWidth;

but for example to empty the canvas you only use innerWidth without window.

ctx.clearRect (0,0, innerWidth, innerHeight);

Did I miss a declaration at the top of the innerWidth code? For me it also works without declaration from innerWidth.

So my question is why you sometimes use window.innerWidth and sometimes only innerWidth?

Thank you in advance for your answer!

chris posted 11 days ago

Hey Swebd, great question!

So, the reason I use innerWidth sometimes instead of window.innerWidth is that by default, you can access any window property within a JS file as long as it's being read by the browser.

You technically don't need window at all, as long as you know the property name associated with the window object, you can take out the window portion altogether.

The main reason you'd want to keep that window. prefix is for clarity purposes. In my case, I already know innerWidth and innerHeight are window properties, so I don't really include it in most of my projects unless I absolutely have to.

Swebd posted 11 days ago

I understand! Thanks for the quick explanation and keep it up!

dbaxi posted 7 hours ago


Want to participate?

Create a free Chris Courses account to begin

Sign In
Cheat Sheet Preview

Download The Official Chris Courses HTML5 Canvas Cheat Sheet

Essential canvas syntax at your fingertips.

Download Cheat Sheet

Providing the lift to launch your development career

© 2020 Chris Courses. All rights reserved.