From the course: Coding for Visual Learners: Learning JavaScript from Scratch
Unlock this course with a free trial
Join today to access over 24,400 courses taught by industry experts.
Variable scope - JavaScript Tutorial
From the course: Coding for Visual Learners: Learning JavaScript from Scratch
Variable scope
- [Instructor] Our shapes right now are not being positioned relative to the canvas size, meaning that if I'm to change the canvas size if I were to make the canvas a little bit larger the shape is not going to remain at the center of the screen. To have the shapes close to the center for any given canvas size, you can start up by using variables to set the widths and height values for the canvas as well. And then we can utilize those same variables to control the position of the shapes too. So let's try to do that. Inside the setup function, we are going to create two new variables called canvasWidth and canvasHeight with the values of 800 and 300. So here I'm going to create a variable called canvasWidth, which is going to be 800. And then I'm going to create the canvasHeight, which is going to be 300. And then I will be using these variables inside my function core. So the plan is we can be using the same…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.