02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Creating Canvas elements dynamically

Up until now we worked with the canvas element we added onto the stage manually. In this lecture we will learn how to dynamically add new canvas elements into our HTML. This is the last DOM specific lecture in this title as from here on words we will be deep into the world of the canvas API.

Keynotes:

  • Adding multiple canvas onto our page.
  • Creating a canvas generator.
  • Working with the DOM to create new canvas elements.

Video Transcript

00:00To conclude this section, let's go ahead
00:02and create our Canvas elements dynamically
00:06using the JavaScript API itself.
00:09So what we're gonna do is we're gonna add
00:11multiple canvases onto our page.
00:13This is gonna be used from here on
00:15throughout every single lecture in this title.
00:18We're gonna create a Canvas generator,
00:21which we'll work with by using the DOM elements
00:23to create new Canvas elements.
00:26And that's gonna be our foundation
00:28where we're gonna just throw in new canvases
00:30each lecture following after that
00:32using this helper function that we're creating.
00:35So let's jump right into it.
00:38All right, our next step, what we wanna do
00:40is we wanna create dynamically Canvas elements.
00:44The reason why we wanna do that technically
00:46is because we wanna be able to add more than one flag,
00:48an undefined amount of flags.
00:50Beyond that, we also wanna show you
00:52the process of creating a new Canvas element from scratch.
00:57So let's go ahead and do that,
00:58and I'm gonna go right below our doCanvas not supported,
01:02and I'm gonna create a new function,
01:04which is basically gonna be called createCanvas,
01:07and it's gonna accept two properties,
01:09a width and a height, two arguments.
01:12Now, what do we wanna do?
01:13We wanna create a new Canvas element, which is very simple.
01:16I'm just gonna call the, oopsie,
01:18I'm gonna call the document, createElement,
01:21and send into it the Canvas
01:23to be able to create a new Canvas element.
01:26That's all I really had to do to create the element,
01:28but the next few steps will be
01:30to define that width and height,
01:31so I'm gonna set there that Canvas.width
01:35to be the width that we're sending in,
01:37and the Canvas.height to be the height
01:39that we're sending in as well.
01:42What do we need to do next?
01:43The next step for us, the literal explicit next step,
01:47is to go ahead and define oto
01:50to add it actually into the container somewhere.
01:53In our case, what I wanna do,
01:55I wanna add it into our Canvas wrapper,
01:57so I'm gonna go ahead and call that original function
02:02that we've created, which was called byId,
02:04to receive that Canvas wrapper,
02:07and once I receive that Canvas wrapper,
02:08I'm just gonna go ahead and append child
02:12and just add that HTML element, that DOM element
02:16into the DOM itself, into the HTML.
02:19That's all I had to technically do to create the item,
02:22but I'm gonna go ahead and also return that Canvas,
02:25or if I wanted to cut down a little bit
02:28in the process of the user needing to configure things,
02:31I could go ahead and also then define it directly
02:33to be the context, but I'm gonna actually,
02:36I'm gonna fetch that information down the line,
02:38mainly because I wanna retrieve
02:40that width and height dynamically,
02:42so what I wanna do is I wanna send this Canvas
02:45as a property of the createPaulo,
02:47and then let it know which Canvas
02:50is it gonna be working with.
02:52Now, before we do that, I'm just gonna go ahead
02:54and create here a couple of Canvases
02:56just to test it out and see that it works,
02:58so inside of our doCanvas area,
03:00I'll create one Canvas that is 50 by 50.
03:03Now, I warn you in advance that it's not gonna look
03:06the way we thought it would,
03:07and I'll show you why not immediately after we do this,
03:10so let me just set this to be 250 by 50.
03:13Okay, so one of them will be 50 by 50,
03:15and the other will be 250 by 50.
03:17If I go back into my browser and I click on Refresh,
03:20we'll see that, oddly enough, I created two new shapes
03:23that were in the exact same size as before.
03:26Now, if you recall in a previous lecture,
03:28we said that the CSS itself is stretching our Canvas.
03:32To be able to avoid that,
03:33I'm gonna go ahead and just delete that extra definitions
03:36in our Canvas of that width and height,
03:38so we're not influenced by that at all,
03:41and I'm gonna go ahead into my browser
03:43and click on Refresh,
03:44and now we should see the items in their right size.
03:46Now, because they're in the same height,
03:49it's kind of an invisible one of the two,
03:51so I'm gonna go ahead here and also add maybe
03:53a padding or a margin.
03:54Let's try it with a padding of 10 pixels,
03:57or actually margin, I think, would do better
04:01because we wanna see that color difference,
04:03and if I go ahead here and click on Refresh,
04:05we'll see that now I'm gonna have the two Canvas elements
04:07that I was hoping to get.
04:10Beautiful, so we've created the structure.
04:12All that's left for us to do is now to plug it in
04:15to our logic, so I'm gonna go ahead into my code.
04:17What I wanna do is I wanna send as a property
04:19to that create Paulo, the Canvas element
04:22that it's going to be creating with,
04:24so if I go ahead here,
04:25I'm just gonna create a Canvas element
04:27and literally just create it
04:29and send it directly into create Paulo,
04:31and I'm gonna go ahead here and just change here
04:34the width and height that I wanted,
04:36so if my original width and height
04:38of our original Canvas was 400 by 250,
04:41let's go half of that size,
04:43so let's make it 200 by 125,
04:46so I'm gonna go ahead here and just send a 200 by 125,
04:50so that should give us a flag that is a little smaller.
04:53Once we created that step,
04:55and let me just delete the extra Canvases
04:56that we're creating,
04:58once we created that step,
04:59I'm gonna go into the create Paulo
05:01and make sure that that property that's being sent in
05:04is gonna be a Canvas,
05:05and all that's left for me to do
05:07is to extract this definition of a Canvas out,
05:10and all the rest should remain the same
05:11because we're still working
05:12with the same variable name as Canvas,
05:15but this time we're sending it in dynamically,
05:17so if we go ahead and save this and click on refresh,
05:19hopefully we'll get the flag of Paulo,
05:21a half of that size,
05:23near an empty space right now,
05:27and there we go,
05:27we just created a smaller version, a smaller flag,
05:31it's sitting right next to our main Canvas.
05:33In the next lecture, we'll create a new lecture,
05:36and each time we're gonna be working with the main Canvas,
05:39but when I'm done in between lectures,
05:41I'm just gonna change the logic
05:43to define our Canvas to be with a new Canvas element
05:47with the create Canvas,
05:48so as we create the next lecture,
05:50which we'll see in the next lecture,
05:51we'll first send in the current one that exists,
05:53but then every time in between lectures,
05:55I'll just create a new Canvas
05:57and always work with the larger space
06:00inside of the main lecture.
06:04In this lecture, we created Canvases dynamically
06:06and created the baseline for us to be able
06:09to create Canvases on the fly whenever we need them.
06:13In the next lecture, we're gonna create shapes using paths,
06:17and that's gonna be our next section, really,
06:20because the whole section is gonna be dedicated
06:22to shapes and paths.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!