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

Pixel manipulation

Canvas items when rendered are represented as bitmap data and as such the canvas API comes with a way for us to manipulate data on the bitmap level. By the end of this lesson you will know how to fetch the pixel data and manipulate it.

Keynotes:

  • Pixel manipulation.
  • Canvas data pixel by pixel.
  • Inverting an image.

Video Transcript

00:00Pixel manipulation is uniquely Canvas inside of HTML5,
00:05and in this lecture,
00:07we'll learn how to manipulate pixels.
00:10Literally, what we're going to do is we're going to get
00:12the pixel data from Canvas itself,
00:14and pixel by pixel will change
00:16each pixel to draw something else.
00:19What we're actually going to do is we're going to take
00:20our image and we're going to invert
00:22its colors to create a film effect of our image.
00:27Let's see it in action.
00:28It's a really fantastic topic.
00:31Let's jump right into it,
00:32and I pre-configured already in advance
00:35the createPixels function and plugged it
00:37in with a context, width, and height.
00:40I'm going to go ahead and create a new image,
00:42and what I really want to do is I want to make
00:46sure that I'm going to add it first to
00:47the stage before we talk about anything else.
00:49I'm going to call the onLoad callback
00:51and create its callback,
00:55and also make sure that I'm going to set
00:57the source of our image,
00:59and I already have it pre-configured,
01:01but it's literally sitting inside of our,
01:03I have your end image.
01:04We've worked with this image already in
01:06section one of this title,
01:07and it's literally just our
01:09default canvas not working message.
01:13Now, if you remember from the lecture we were
01:15talking about adding images,
01:17all we really have to do is call the context,
01:20drawImage, and then send
01:22inside the image as the first parameter.
01:24In the next two parameters,
01:26the x and y coordinates where we want it to sit,
01:29and the last is the width and
01:30height we want the image to have.
01:32In my case, I'm just going to go ahead and force
01:34resize it to the width and
01:36height of our full canvas size.
01:39By doing this, if I go back into my application,
01:42and click here on refresh,
01:43we'll see that now we'll have that image,
01:45and it'll be smashed into position in our canvas.
01:48I just wanted to say a side note that if you go and look into
01:51the documentation that is provided in
01:54the references,
01:56you'll see that you could put in a few more properties that
01:59will enable you to crop images.
02:01Go ahead and check that out.
02:03But in this lecture, what I want to do is I want to
02:05actually talk about pixel manipulation.
02:09Now, to be able to do that,
02:10I'm going to go ahead after I've
02:11added the image onto the stage,
02:13and I'm going to call a new variable called image data.
02:17What I want to do with it,
02:18I want to go to my context,
02:19and I want to get image data,
02:21which literally is going to give us
02:23back a really weird data object
02:26that contains all the information we need about our image.
02:30I'm going to go ahead here and say, hey,
02:31I want to get everything that's in from 0.0 to,
02:35let's say, I want to do to half of the width,
02:39and I want to do the full height,
02:42literally creating a rectangle that
02:46takes a large portion of our screen.
02:49Not only do I want to do that,
02:51I'm going to also save another variable here.
02:53So let me just put here another comma,
02:55and I'm going to call this pixel data,
02:58because really what I want to get is the pixel information,
03:00and to do that, I'm going to go our image data
03:02that we've just created and approach the data variable,
03:05which contains this massive array
03:08that has inside of it just numbers.
03:10Now, we're going to look through them
03:11and understand how they work in literally right now.
03:14So last but not least,
03:16I'm going to also save the length of this pixels.
03:18So I'm going to save the length of this array
03:20so I don't have to approach it a few times.
03:23So I'm going to go ahead here and just call our pixel data
03:26and ask for it its length.
03:30Okay, so now I have all the helper variables that I need.
03:33It's time for me to go ahead and loop through
03:35to show you how I could create a really cool effect.
03:38So I'm going to go ahead here,
03:39looping through all the items inside of this array,
03:42as long as our index is smaller than the pixel length.
03:47And as long as that is smaller,
03:48we're going to grow by one each time.
03:51Now, let's talk about how this works.
03:54And by the way, I don't want to grow by one,
03:55I actually want to grow by four.
03:57And the reason is very simple.
04:00Actually between each eye, so if I have from zero,
04:03I have position zero, one, two,
04:06I kind of want to skip position three right now,
04:08which will control the actual alpha channel.
04:12And I'm going to go ahead here and what I want to do
04:14is I just want to manipulate the three
04:16out of the four channels.
04:18These three channels represent red, green, and blue.
04:21Now, all the data, all this pixel data is split
04:24into every pixel has four pieces of information.
04:28It's red, green, blue, and alpha channel.
04:30So what I want to do is I want to go pixel by pixel
04:34and I want to manipulate it.
04:35So in pixel data zero or eye in this case,
04:39I want to take the number that's there,
04:40which is going to be a value between zero and 255,
04:44which represents all the variety of reds
04:47that the computer could have,
04:49or this browser, or this color pixel data could have.
04:53And what I want to do is I want to subtract that value
04:55from one another, creating a negative of that value.
04:59I'm going to do the exact same thing another two times
05:03because I want to control Oto not only the red channel,
05:06I want to control Oto the green channel,
05:09and I want to control Oto the blue channel, RGB.
05:13So I'm going to go ahead here to eye plus one
05:15and literally do the exact same thing to eye plus two,
05:19eye plus zero, one, and two.
05:22Now three, I'm going to ignore, as we said,
05:24that's our alpha channel
05:25and I don't really want to manipulate it currently.
05:27Once I made all the manipulations that I want to,
05:30as I did right here, I could go ahead into my context
05:33and I could say put image data
05:36and I could go ahead and send in this image data,
05:40which is our pixel data or really our image data.
05:43And I'm going to go ahead and send that image data in
05:46after it's been manipulated through its data.
05:49And I could say, where do I want to position it?
05:50I want to position it at zero, zero,
05:52which would correlate to where I took it from.
05:55Now, if everything worked according to plan,
05:57if I go back into my application and click here on refresh
06:00and let me just fix here,
06:02syntax error identifier starts immediately
06:05after numeric literal.
06:07So let's go ahead here.
06:08Our error here is around 563
06:12and line 563, 563, here we go.
06:18So first of all, I have here a dot by mistake
06:20instead of a comma, that must have done that.
06:22Let's go ahead and give this one more shot.
06:26And here we go.
06:27I just created the negative of our item
06:30in the region that I wanted to create a negative,
06:32literally taking the pixel data and manipulating it.
06:36There's a lot of other ways to manipulate data and text
06:39and create really cool effects,
06:40such as grayscale or sepia and so forth,
06:43or any other effect that you would want to create,
06:46such as a really intricate transition between two images.
06:51So you could learn a lot more about that in my book,
06:53HTML5 Canvas Data Visualization,
06:56which will be a link in the references
06:58where we talk about that a lot.
06:59Or you could look at some other links down below
07:02and that covers pixel manipulation for this title.
07:06In this lecture, we learned about pixel manipulation
07:09and I urge you to check our references in the source file
07:12where you'll see a lot more types of pixel manipulation.
07:17In the next lecture, we're gonna move our attention
07:19to the art of clipping
07:20and we'll learn how to clip our art one into each other.
07:24Very, very cool stuff.

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!