Return to Video

Special ProcessingJS functions

  • 0:00 - 0:03
    At this point, you might realize
    that you've been defining
  • 0:03 - 0:07
    a particular function every time
    you wanted to animate your program--
  • 0:07 - 0:09
    the `draw` function.
  • 0:09 - 0:13
    To remind you, here's our
    animated car program again.
  • 0:13 - 0:17
    It has this variable named x,
    it starts off at 11.
  • 0:17 - 0:23
    And then inside the `draw` function,
    it draws the car at that variable x,
  • 0:23 - 0:27
    and then it adds three
    to that variable x each time.
  • 0:27 - 0:29
    And that results in a car
  • 0:29 - 0:32
    that's moving three pixels
    across the screen continuously.
  • 0:32 - 0:35
    So, that's how this works.
  • 0:35 - 0:38
    But now that you've learned
    how to make your own functions,
  • 0:38 - 0:42
    you might be wondering,
    what about this `draw` function?
  • 0:42 - 0:44
    Why is it always named `draw`?
  • 0:44 - 0:46
    Is it a custom function?
  • 0:46 - 0:49
    And those are very good questions.
  • 0:49 - 0:52
    You see, in the ProcessingJS library,
  • 0:52 - 0:56
    the `draw` function is one of
    a few predefined functions
  • 0:56 - 1:01
    that give our programs more control
    over what's happening with the canvas.
  • 1:01 - 1:05
    A predefined function is one
    that has already been defined
  • 1:05 - 1:07
    by the ProcessingJS library.
  • 1:07 - 1:11
    But it usually starts off
    as an empty definition.
  • 1:11 - 1:17
    For example, in the ProcessingJS library,
    there's code that looks like this:
  • 1:17 - 1:19
    `var draw = function() `
  • 1:19 - 1:23
    and then it's just empty,
    completely empty.
  • 1:23 - 1:29
    Now, we load the ProcessingJS library
    into every program here on Khan Academy,
  • 1:29 - 1:31
    So you never see this code.
  • 1:31 - 1:34
    But trust me, it exists.
  • 1:34 - 1:41
    Now I'll comment that code out,
    since ProcessingJS already does it for us.
  • 1:41 - 1:45
    Now when you redefine `draw`,
    in your own program,
  • 1:45 - 1:50
    this new definition overrides
    the old empty defintion.
  • 1:50 - 1:52
    And now the `draw` function
  • 1:52 - 1:55
    actually does exciting things,
    like drawing a car.
  • 1:55 - 2:01
    Now the question is, why does the `draw`
    function get called over and over?
  • 2:01 - 2:06
    Well, there's also code
    in the ProcessingJS library
  • 2:06 - 2:11
    that sets a browser timer and
    recalls the draw function repeatedly,
  • 2:11 - 2:14
    over and over and over.
  • 2:14 - 2:19
    We have to name the function `draw`,
    because that's the name of the function
  • 2:19 - 2:22
    that ProcessingJS is calling repeatedly.
  • 2:22 - 2:28
    If we rename this, like let's say
    we rename it to `drawCar`.
  • 2:28 - 2:34
    And first we get an undefined error, so
    we could say `var drawCar`.
  • 2:34 - 2:37
    So now you can see, if we
    rename this to `drawCar`,
  • 2:37 - 2:40
    we don't see any animation any more.
  • 2:40 - 2:45
    And that's because this function
    isn't being called repeatedly any more,
  • 2:45 - 2:47
    because it's not named draw.
  • 2:47 - 2:51
    So we have to put the code
    that we want repeatedly called
  • 2:51 - 2:56
    inside a function spelled `draw` exactly.
  • 2:56 - 3:01
    So I'll just do it again,
    and I'll just call `drawCar` from here.
  • 3:01 - 3:03
    Aha! We've got it back.
  • 3:03 - 3:07
    All right, so it needs to be called draw,
  • 3:07 - 3:13
    and this also means that you shouldn't
    name your own custom functions `draw`,
  • 3:13 - 3:18
    unless you want them to be
    treated specially and called over and over.
  • 3:18 - 3:24
    And also remember, you can't have
    multiple functions named `draw`.
  • 3:24 - 3:28
    Only the last definition will count.
  • 3:28 - 3:31
    If we had a `rect` inside here--
  • 3:31 - 3:35
    So now we can see that our car
    isn't being drawn any more,
  • 3:35 - 3:40
    and only the rect is being drawn instead,
    because only the last defintion counts.
  • 3:40 - 3:43
    So let's get rid of that one.
  • 3:43 - 3:48
    Now the draw function is not the
    only predefined function
  • 3:48 - 3:50
    that has this special behavior.
  • 3:50 - 3:53
    There are also a bunch of functions
  • 3:53 - 3:57
    for responding to mouse
    interactions and keypresses.
  • 3:57 - 4:02
    Let's say we want to have a program
    that draws a colored ellipse
  • 4:02 - 4:05
    wherever the user moves the mouse.
  • 4:05 - 4:09
    So we could do that with
    a function like this.
  • 4:09 - 4:16
    [typing]
  • 4:20 - 4:23
    All right, oh, beautiful.
  • 4:23 - 4:27
    Now, this function is being called
    over and over,
  • 4:27 - 4:31
    even when the user isn't
    moving the mouse, like right now.
  • 4:31 - 4:35
    And this program, it works, it does
    what we want it to do,
  • 4:35 - 4:38
    it's painting these nice ellipses
    all over the screen.
  • 4:38 - 4:42
    But as it turns out, there's
    a better way to do the same thing
  • 4:42 - 4:44
    that's more efficient.
  • 4:44 - 4:52
    So we can change the `draw` function
    to `mouseMoved`, and let's see.
  • 4:52 - 4:55
    And it still works.
  • 4:55 - 4:58
    You see, our environment checks programs
  • 4:58 - 5:01
    to see if they've defined
    a `mouseMoved` function,
  • 5:01 - 5:05
    and if so, it calls that function
    whenever the user moves the mouse.
  • 5:05 - 5:10
    But it won't be called
    if the user isn't moving the mouse.
  • 5:10 - 5:13
    So before, we were calling
    the code in the `draw` function
  • 5:13 - 5:16
    when we didn't need to, over and over.
  • 5:16 - 5:20
    And now, we're only calling
    this code in `mouseMoved`
  • 5:20 - 5:23
    when the mouseX or mouseY
    have actually changed.
  • 5:23 - 5:27
    So our program is more efficient,
    and that's a good thing.
  • 5:27 - 5:31
    Generally, if you only want to
    change your program output
  • 5:31 - 5:35
    when the user moves the mouse,
    then it's better to have that code
  • 5:35 - 5:37
    inside the `mouseMoved` function.
  • 5:37 - 5:41
    And there are a bunch more
    predefined functions like that,
  • 5:41 - 5:45
    which you can see more examples of
    in the documentation.
  • 5:45 - 5:49
    Like `mousePressed`, `mouseReleased`,
    `keyPressed`, and more.
  • 5:49 - 5:54
    So remember, if you want to use
    a special predefined function,
  • 5:54 - 5:59
    like `mouseMoved` or `draw`,
    spell it correctly, and use it correctly.
  • 5:59 - 6:03
    If you don't, then make sure you
    give your own custom functions
  • 6:03 - 6:06
    a new, unique name.
Title:
Special ProcessingJS functions
Description:

more » « less
Video Language:
English
Duration:
06:08

English subtitles

Revisions