Functions and more Canvas - JavaScript Programming Basics p.2

sentdex · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This video tutorial covers JavaScript programming basics, focusing on functions and canvas graphics, including drawing lines, circles, and shapes, and using variables and functions to simplify code.

Full Transcript

what's going on everybody yeah came back welcome to part 2 of the JavaScript basics tutorials in this part 2 we're gonna continue working with the canvas just really quickly just show some of the other things that we can do and then we're gonna get into functions so let's go ahead an get started so let's say we wanted to continue just drawing this you know line so where we left off we have this lovely piece of Contemporary Art and if we wanted to we could just continue drawing lines so for example I'm just gonna copy these two lines we don't need to move two because that's all we did like we did that to like move without actually drawing but if we actually want to draw we just line two and then stroke so let's say we did I don't know 70 and 55 here if we rerun that you'll see okay now we've got this you know angle added but but what we're really interested in is these blobs like I would really want to know how do I draw like a blob and what's a blob is a circle so so how do I draw a circle to the to the canvas well there's a method that is arc so in this case we would contact context arc and then the way that you look at this or think about this is arc in theory if you did a full arc it would be a full circle so what would the center of this if there if you did draw a full circle what would the center be so in this case we'll say a hundred a hundred now with art you don't have to draw a circle so arc is here for any time you want a curved line right you you might want to continue the curve all the way to make a circle but you also just might want a curved line so that's what arc kind of solves all those things and one one fell swoop so you would say what's the size over sorry what's the location like the XY 100 100 and then the next thing that we would pass here is what's the size and then you've got the starting angle in the ending angle so we're gonna say zero and then you would say basically you're going to think of this in terms of pies so one times a math dot pie is going to be half of a circle so let me just add a context dodge stroke and let's go ahead and run that and you'll see here so we drew two now we didn't draw it like this doesn't go to one hundred one hundred it's a bit beyond that but the center of this circle which would be like right there that would be one hundred one hundred so if we want to draw a full circle now math dot pi we could say two times math dot pi and we will run that and now we have this full circle okay pretty cool so now what I want to do is basically all we actually want is to have a circle for now so I'm going to delete everything up to begin path we're going to begin path we're on an arc the center is here cool and then we stroke so it's kind of like you know this would be your line - and then stroke so and then we don't need a move - in this case so okay so let's test that really quickly yes so now that's a pretty boring blob we would probably want to fill it so you can probably I think you can get away with this afterwards too you could do a context fill after the stroke or before the stroke I'm pretty sure yeah the other thing that we can do is let me just test this make sure yeah the other thing that we can do is change the actual color with context fill styles so we can say context fill style and in this case we'll go with a beautiful green so now we get our green circle now what if what if we wanted to have multiple blobs like this how would we actually do that and the way that we would wind up doing that you know you might think well I'll just haha I'll just copy and paste I'm an expert computer person and you you could do that but you probably don't want to do that right like we definitely just copy and paste but that's we use functions or at least things like functions so like we used Verret we can use variables to get rid of copying and pasting to some extent mostly variables is there so like if you needed to change a value as you write you would just change it in one location or you can use variables if you want the program to like change your value based on some conditions with functions it's it is for one it's just gonna clean up clutter and stuff like that it's also going to make it easier on the programmer because imagine if you wanted to like change these lines like like suddenly you just let's just imagine you wanted to do something different with how you get a blob to the screen or what you're gonna do with that blob or whatever now you've got to come through to change every single one of these it's a pain in the butt and so anyway we definitely want to use functions or something like functions he will talk about classes in a little bit not in this video so the way that we're gonna do that is to with a function we'll just come here and let's just call this function we're gonna call it blob and then you have your parameters and then the function itself now what does a blob take well a blob is gonna take you know court like it basically it's this stuff right here we don't need to talk about the starting in the ending angle here because we're going to we're always going to use the same values here it's always going to make a full circle so we really just want the X the Y size in the color so that's exactly what I'm gonna write here XY size color then what we want to do is take this code here and I'm just gonna cut it paste it then I'm gonna highlight here and I am going to I was gonna try to have that there we go hit tab and tab it over so now that we've done that the next thing is we need to fix these variables in here so like right now that's just hard coded and this is part of what we wanted to fix so we're actually just gonna pass X Y sighs and then in here we're gonna just put color so now you've got this blob function and now we actually want the blob function to run so the way that we could do that is just literally call blob now sometimes you might write a function that has no parameters right so it looks like this and it does something besides what we were just done here so maybe these values are filled in right and one of the common mistakes I see from new programmers and myself included I've made this mistake was you call blob but you forget the parentheses or worse in your actual you know definitions of things you add parentheses which makes it run so here this is just a pointer to the blob function here this is running the blob function okay so let me add those bars back hopefully I could there we go nice so now we want to actually run the blob function so we are just going to pass in let's do 25 100 size will be 20 and color wool made green so first let's just test to make sure that works sure enough and now what I want to do is close the towns if you use my editor it would all be in the same tab okay so this is that few more 75 and we'll go with a 50 and then we'll put I can't I'm not gonna make 5 of them but we can do at least a few so we'll do 25 and then 100 and then this will be red and then this one's blue and awesome we were on it cool so that's that's functions that's why we're doing it just as a note don't forget to begin path if we don't begin path like what if I took this and just put it right out here let me just run that real quick so you notice how it looks pretty funky what's going on there well begin path also takes note of where not only where the stroke is happening but for fill so the last thing is gonna be filled here but every step of the way so let me just show one more thing where I close out this tutorial so I don't think we've talked about comments so if we want to get rid of something or we want to make a note to ourself like you know makes a green blob we use comments and the way you make a single-line comment is with two forward slashes and then the way you would make a multi-line comment is with a slash and then an asterisk and then multiple lines and I guess I should make multiple lines and there's an asterisk in a slash and then I'll close it off now comments aren't read by like JavaScript or rendered by the browser or anything they're there for either your notes or so someone else reading your code gets a better understanding of like what is happening in your code and then so basically pretty much those two things either just a note to yourself later like I want to add this or work on this next or or you're telling yourself how a function works or you know anything so anyways but you can also use it for debugging purposes like so in this case I just wanted to show that the fill will now be you know all read basically everywhere because of what we've done cool so yeah so the reason why I wanted to show that is just just as a reminder or not even really a reminder I haven't really totally made it absolutely clear I guess I did say begin pad starts and restarts but keep that in mind it's not just for a stroke it's for a fill it's for everything going on so you definitely want to make sure you re beginpath for every single blob or thing that you draw to the canvas okay well I think that we've made some really good progress here we've got multiple blobs on the screen the next thing we would want to start doing is moving some blobs right we want to get these blobs animated and moving around and stuff and bring them to life so in the coming videos that's exactly what we're going to be working on is how do we get these to actually move around and all that if you've got questions comments concerns whatever feel free to leave them below and a quick shout out to some of my channel mem Henry assumed our Rajneesh cheeky buns 13 and frank lloyd welcome back all of you guys for your third months you guys are amazing and that's it guys I will see you guys in another video

Original Description

Welcome to part 2 of the JavaScript basics tutorials. In this tutorial, we're going to continue working with the canvas as well as learning how to use functions. Text-based tutorials: https://jsprogramming.net/functions-canvas Channel membership: https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ/join Discord: https://discord.gg/sentdex Support the content: https://pythonprogramming.net/support-donate/ Twitter: https://twitter.com/sentdex Facebook: https://www.facebook.com/pythonprogramming.net/ Instagram: https://instagram.com/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video tutorial teaches the basics of JavaScript programming, including functions and canvas graphics, and provides hands-on examples of drawing shapes and using variables and functions to simplify code. By following this tutorial, viewers can learn how to create interactive canvas graphics and improve their JavaScript programming skills. The tutorial covers key concepts such as functions, variables, and drawing on canvas, and provides practical examples of how to use these concepts in real-

Key Takeaways
  1. Draw a line on the canvas using the stroke method
  2. Draw a circle on the canvas using the arc method
  3. Fill a shape on the canvas using the fill method
  4. Change the color of a shape on the canvas using the fillStyle method
  5. Create a function called blob that takes parameters X, Y, size, and color
  6. Pass X, Y, size, and color to the blob function
  7. Call the blob function with the desired parameters
  8. Use beginPath to start drawing a shape and fill to fill the shape
💡 Using functions and variables can simplify code and make it easier to change and reuse, and beginPath is an important method for drawing shapes on the canvas.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →