Working with Text
Although canvas main usage is to draw at times you might need to add text into canvas to do that we will take advantage of a few build int methods and properties of canvas 2d API.
Keynotes:
- Adding text into canvas.
- The canvas text capabilities.
- Getting a text box.
Video Transcript
00:00Welcome back to the last section in this title.
00:03And this section is gonna be, well,
00:05its main topic will be manipulating the canvas itself.
00:09That's gonna be the thematic topic hidden behind.
00:13What are we gonna do in this section?
00:14In this section, we'll learn how to work with text,
00:16something we avoided throughout this title.
00:19We're gonna learn about pixel manipulation
00:20by revisiting images and seeing how to manipulate them.
00:25We're gonna also look at how to clip art,
00:27which is really, really cool and advanced
00:29and not supported in all browsers currently
00:32while this recording is being made.
00:35We're gonna also look at how to transform the canvas itself,
00:38which is kind of complicated, but don't worry,
00:41that's why you have me.
00:42And we'll finish up with scaling, rotating
00:44and translating the full canvas itself.
00:48So let's jump right into our first lecture,
00:50which is working with text.
00:51And through it, we'll learn how to add text
00:53into our canvas, how to see,
00:56we'll see the main text, sorry,
00:59our main text capabilities in canvas,
01:01and we're also gonna leverage the knowledge
01:03that we could fetch from canvas about the size of our text
01:06to position and size our text element within.
01:10So let's jump right into it.
01:13After all the complicated things we've been through
01:15throughout the last few sections,
01:17it's time for us to focus on simple things,
01:19but really important, and that's text.
01:22Now, what I wanna do is I'm gonna create your copy.
01:24In this case, this is my Twitter account name,
01:27and you're welcome to follow me on Twitter at zero2geek.
01:31Now, I wanna show you how to create this type of copy.
01:34It is really not that complicated,
01:35but there's some steps involved
01:37into integrating copy into canvas.
01:40So let's go ahead and get started.
01:41And I went ahead already and created
01:43the create filtered text function
01:45and pre-configured our normal variables
01:48as we have throughout the lectures.
01:51And I'm gonna go right ahead,
01:52and what I wanna do is I'm gonna go to my context,
01:55and I'm gonna just go straight ahead and stroke text.
01:59And I'm gonna put inside whatever text I want,
02:02and let me go ahead and just create a helper variable
02:04because I'm gonna use it a couple of times.
02:07And I'm gonna call this just text,
02:08and I'm gonna set it to be at zero2geek.
02:12Beautiful.
02:13So let me send that text in as the first parameter.
02:16My second parameter is gonna be the location or position
02:20where I want my text to be.
02:22For now, let me just set it at 50-50.
02:24I'm gonna go ahead and save this and run my application.
02:27Now, by default, we're gonna have a certain font size,
02:30and we're gonna have a certain color.
02:32The content stroke text is not a function.
02:35That's because I have a slight typo.
02:37And let me go ahead and fix that.
02:40Stroke text.
02:45And this is not context, context, that's good.
02:49Let's give it another shot.
02:50Click our refresh, and here we go.
02:52So our text is already in,
02:53but obviously it's not the size we wanted it to be.
02:56So the next step that we really wanna do
02:58is we wanna go to our text and say to it,
03:01hey, by the way, the font that we wanna use,
03:05and we're gonna set that with the font parameter,
03:08well, we kinda want it to be 40 pixels, maybe,
03:11and we want it to be a type of font of a serif.
03:14Now, in Canvas, you do not embed fonts.
03:17So if you wanted to embed fonts,
03:18you have to approach CSS, embed the fonts,
03:21and then work with those embedded fonts.
03:23In this case, I'm going to whatever font the user has,
03:27and I'm saying that I wanna type a serif font,
03:29which is 40 pixels.
03:30I don't know which font exactly is gonna be used
03:33because that's gonna be based on the user's computer
03:35and not my own inclination.
03:38Now, besides stroke text,
03:40there's also another function very similar,
03:42similar to everything that we've done previously,
03:44which is called fill text,
03:46which will actually fill the text out.
03:48And again, so far we didn't configure our colors,
03:51and we didn't configure anything really specifically
03:54to the font besides its size and its type.
03:58But I'm gonna go ahead and show you a few more things
04:00because really, in general,
04:01most of the things that you would do with fonts
04:03is the same as you would do with any other item,
04:05besides defining your text alignment.
04:08Right now, our text alignment,
04:09and I'm gonna go ahead here and also very quickly,
04:12just go to my width, divide two,
04:15and my height, divide by two,
04:17width divided by two,
04:20height divided by two,
04:22and I'm also gonna go ahead here and subtract
04:24maybe four pixels to create that overlay effect
04:27that we were having before
04:28when you were looking at it before earlier.
04:30And you'll see that now we'll have your other text,
04:32and it's gonna have a relatively ugly overlay right now,
04:34but that's fine.
04:35We could change the colors if we wanted to,
04:36but I'm gonna keep it this way for now.
04:39Notice that what happened when I set my text align
04:41to nothing, it kinda changed our layout a bit,
04:44but I'm gonna go ahead here and set it to be center.
04:47Now, there's a few, there's left, right, center.
04:51There's, I don't remember all by heart,
04:54but just go into the documentation
04:55which I'll provide inside of your source files,
04:57and just go ahead there and look at all the different options
05:00to your text align.
05:01So I'm gonna go ahead here and save that,
05:03click here on refresh,
05:04and now our content, our xy coordinates,
05:06would be in the center point.
05:08Now, beyond that, I could also define,
05:11beyond defining that, I could also define
05:12the center point of the text itself.
05:15Do we want it to be on the top?
05:17Do we want it to be on the bottom?
05:18Do we want it to be in the center?
05:19To do that, I'm gonna use the text baseline.
05:22And with the text baseline, I'm gonna go, baseline,
05:25I'm gonna go ahead and set it to be in the top.
05:26And what that means is that our coordinate,
05:29notice how where we are right now.
05:31And when I click here on refresh,
05:32and notice how this is above the center,
05:34so our point of our line starts here,
05:37you could say your y coordinate, your zero coordinate,
05:40or your point where our item is actually being positioned
05:43is somewhere right here.
05:45But by changing that configuration,
05:47you'll see that the copy is now gonna go to the bottom.
05:49I could have set it to be in the center as well.
05:51So again, look into the documentation
05:53to see all the different various options that you could do.
05:55And that's really all I wanted to talk about
05:58when it comes to text,
05:59but I just wanna do one more thing
06:00before we wrap things up.
06:02Now, I really wanna size my text to fit the whole canvas,
06:05or almost all the canvas.
06:07But because I don't know what font
06:09is gonna be used in the end,
06:10I don't know what size the font needs to be
06:12for that to work.
06:13To be able to control that and to configure that,
06:17I'm gonna go ahead and create here a little bit
06:19of probably not the best logic in the world.
06:22Although it's not that far from being the best logic,
06:25I'm gonna set here a variable that will be defined
06:28as let's say 48 in the beginning.
06:31And then I'm gonna go ahead and create here a loop
06:33that will run no matter what.
06:34And I'm gonna ask while hour,
06:37while hour, while hour,
06:40width of our text is smaller than the size of our screen.
06:44So I'm gonna need also a text box.
06:46And the text box is literally
06:49is something I could get from my context.
06:52And I could ask for it to define for me
06:54the size of the text if I would put text inside
06:57by calling the measure text function
07:00and sending into it the text that I want to write.
07:03Once I've done that,
07:04the text box is gonna have two properties of width
07:06and height that I could then use to decide,
07:08is it a good spot or not?
07:10So I'm gonna go ahead here and what I wanna do
07:11is I'm gonna also take this context
07:13to define the size of our font
07:15because we need to know the size.
07:16And I'm gonna go ahead and dynamically change the size
07:20each time the loop goes through
07:22and then defining the size.
07:24I'm also gonna go ahead and upgrade the size each time
07:28as I'm not gonna use it again afterwards,
07:30I'm fine with leaving this the way it is.
07:32Or if I wanted to,
07:33I could have gone here and set it up here
07:35and then started at maybe 47
07:38if I decided to start at 47.
07:40And I'm gonna go ahead for my while here
07:42and what is the logic here?
07:44I wanna ask if my T box width
07:46is smaller than our width times dot nine.
07:51Now, notice the problem here.
07:53The problem here is that it's gonna go one beyond that.
07:56So if I wanted to change my logic in any certain way,
07:59maybe I would do my while at first,
08:01but really the point of here is just to show
08:04that we're gonna dynamically find our size
08:06that we're happy with.
08:08So I'm gonna set that size here, which is size minus one,
08:11or it's actually the size itself, fair enough.
08:15Once I got my size,
08:16I'm gonna go ahead here and save this
08:17and just go ahead and click here on refresh.
08:19Oh, and also make sure, no, all the rest seems fine.
08:22And I'm gonna go ahead here and click here on refresh.
08:24And now my copy itself,
08:26if I don't have your error, which is console,
08:30oh, console, save, refresh.
08:35And I should get here my font sitting on the full spectrum,
08:37the full size of, in this lesson,
08:40we really learned everything we need to know
08:42about text in Canvas.
08:44In the next lecture, we'll go ahead back to images
08:46where we'll start manipulating their pixels.
