Return to Video

Using the event properties (Video Version)

  • 0:01 - 0:03
    I've got this webpage with a
    picture of Winston.
  • 0:03 - 0:07
    It's getting cold here,
    and Winston is getting old,
  • 0:07 - 0:09
    so he wishes he had a beard.
  • 0:09 - 0:11
    I could draw the beard myself.
  • 0:11 - 0:14
    But I think it'd be way cooler if
    the user could draw the beard
  • 0:14 - 0:16
    on Winston themselves,
    however they want it to look--
  • 0:16 - 0:20
    a long beard, some stubble,
    whatever they're into.
  • 0:20 - 0:23
    So how could we do that?
  • 0:23 - 0:29
    One way is to add an event listener
    for the `mouseMove` event on the image,
  • 0:29 - 0:33
    so that our function would get called
    whenever the user moved their mouse
  • 0:33 - 0:34
    over the face.
  • 0:34 - 0:38
    Let's set that up, using
    what we just learned.
  • 0:38 - 0:41
    The first step is to find
    the element, the image.
  • 0:41 - 0:43
    [typing]
  • 0:47 - 0:50
    ..."face", since I had that nice ID.
  • 0:50 - 0:54
    The second step is to define
    the callback function.
  • 0:54 - 0:58
    And we're going to start with a simple one
    just to make sure that it worked,
  • 0:58 - 1:02
    and we'll make it do more later.
  • 1:02 - 1:05
    [typing]
  • 1:12 - 1:13
    Okay.
  • 1:13 - 1:18
    And the final step is to
    connect this to this,
  • 1:18 - 1:22
    to make sure that this gets called
    when this gets the `mouseMove` event.
  • 1:22 - 1:28
    So we'll write
    `face.addEventListener("mousemove",`
  • 1:28 - 1:32
    and then pass it the name
    of the function, `onMouseMove`.
  • 1:32 - 1:37
    Now, pause the talk-through, and try
    moving your mouse over the face.
  • 1:37 - 1:38
    Do you see the message?
  • 1:40 - 1:44
    Okay, hopefully you saw that
    we've got that event working.
  • 1:44 - 1:47
    But it's not what we want
    our event listener to do.
  • 1:47 - 1:50
    We want it to draw, not to write text.
  • 1:50 - 1:53
    How can we draw in a webpage?
  • 1:53 - 1:59
    We could bring in the `` tag
    and draw pixels on it, like we do
  • 1:59 - 2:01
    with ProcessingJS programs here.
  • 2:01 - 2:07
    But all we're drawing is a beard,
    which is really a bunch of black dots.
  • 2:07 - 2:12
    So we could just create a `` for
    each dot, and position that ``
  • 2:12 - 2:14
    with absolute positioning.
  • 2:14 - 2:18
    Here, let me show you with
    one beard follicle.
  • 2:18 - 2:25
    So I'll make a `` with class `beard`,
    and then I've got some nice CSS
  • 2:25 - 2:30
    to style that beard,
    which I'll just stick in here.
  • 2:30 - 2:32
    And let's just fix that up.
  • 2:32 - 2:36
    So you can see our beard is a
    black background, it's 5 by 5 pixels,
  • 2:36 - 2:41
    it's got this 2 pixel border radius
    to make it a little bit round,
  • 2:41 - 2:45
    and it's using absolute
    positioning scheme.
  • 2:45 - 2:48
    Currently, the ``
    shows up under the image.
  • 2:48 - 2:51
    How do we get it to show up
    on top of the face?
  • 2:51 - 2:55
    We're using absolute positioning,
    so that means that we should use
  • 2:55 - 3:00
    the `top` and `left` properties
    to say where it should actually
  • 3:00 - 3:02
    get positioned,
    now that it's absolute.
  • 3:02 - 3:08
    So let's say `top: 80px;`,
    and then `left:15px;`.
  • 3:08 - 3:09
    Beautiful.
  • 3:09 - 3:14
    Now that we've got that working in HTML,
    let's make it work in JavaScript,
  • 3:14 - 3:19
    so that the user dynamically creates this
    `` every time they move their mouse.
  • 3:19 - 3:23
    We'll go back down to our
    JavaScript callback function.
  • 3:23 - 3:26
    The first thing is to create a ``,
  • 3:26 - 3:31
    which we can do with our
    `document.createElement()` function--
  • 3:31 - 3:34
    it's going to be a `div`.
  • 3:34 - 3:40
    Second thing to do is to add that class:
    `beard.className = "beard";`.
  • 3:40 - 3:42
    So we've got this ``,
    it's floating off in space.
  • 3:42 - 3:47
    Final step, append it to the body.
  • 3:47 - 3:52
    All right, so now we've basically done
    in JavaScript what we had done in HTML,
  • 3:52 - 3:55
    so we can delete this HTML.
  • 3:55 - 3:59
    Now you should pause the talk-through,
    and try clicking Winston.
  • 3:59 - 4:00
    See what happens.
  • 4:02 - 4:05
    Did you see the beard follicle show up?
  • 4:05 - 4:07
    And did you try clicking multiple times?
  • 4:07 - 4:11
    If you did, you probably noticed that
    your second click did nothing--
  • 4:11 - 4:14
    or at least it appeared to do nothing.
  • 4:14 - 4:18
    That's because every `` has
    the same `top` and `left` properties,
  • 4:18 - 4:21
    so new ones just pile
    on top of the old ones.
  • 4:21 - 4:25
    We want the `` to show up
    wherever the user's mouse is instead.
  • 4:25 - 4:30
    How do we actually find out
    where the user's mouse is?
  • 4:30 - 4:34
    As it turns out, the browser
    records a lot of information
  • 4:34 - 4:38
    every time any user event happens,
    like where it happens.
  • 4:38 - 4:41
    And the browser gives you
    that information every time
  • 4:41 - 4:43
    it calls your event listener function.
  • 4:43 - 4:47
    But where, how, can we get this
    amazing information?
  • 4:47 - 4:51
    I'll type one letter, to give you a hint.
  • 4:51 - 4:56
    That `e` here, is the event
    information object.
  • 4:56 - 5:00
    And the browser sends it as the first
    argument whenever it calls
  • 5:00 - 5:02
    an event listener callback function.
  • 5:02 - 5:06
    We didn't need it before, so I didn't
    bother writing out the arguments list.
  • 5:06 - 5:09
    But now that we're going to use it,
    I'm giving it a name, to make it
  • 5:09 - 5:12
    easy to reference inside the function.
  • 5:12 - 5:15
    Remember that in JavaScript, a function
    can be called with any number
  • 5:15 - 5:19
    of arguments, even if the function
    doesn't use or refer to any of them.
  • 5:19 - 5:23
    So we were always getting this
    information, we just didn't know it.
  • 5:23 - 5:28
    Now I'm going to log out `e`:
    `console.log(e)`.
  • 5:28 - 5:32
    Pause the talk-through, click Winston,
    and check your console.
  • 5:32 - 5:34
    You should see the
    event object logged out,
  • 5:34 - 5:37
    and you can skim through
    all the properties on it.
  • 5:39 - 5:43
    There are two event properties in
    particular that we're very interested in:
  • 5:43 - 5:46
    `clientX` and `clientY`.
  • 5:46 - 5:49
    They record the x and y of the event,
    and that's what we're going to use
  • 5:49 - 5:51
    to position the beard.
  • 5:51 - 6:02
    Let's set the top of the beard equal to
    e.clientY, plus "px", for the units.
  • 6:02 - 6:10
    And set the left equal to
    e.clientX, plus "px" for units.
  • 6:10 - 6:14
    Now pause and try mousing over.
  • 6:14 - 6:16
    Draw Winston a beard.
  • 6:18 - 6:19
    Pretty cool, huh?
  • 6:19 - 6:23
    I bet you can imagine all sorts of fun
    painting apps that you can make,
  • 6:23 - 6:26
    using clientX and clientY.
  • 6:26 - 6:29
    And as you saw in the console,
    there were a bunch of other properties
  • 6:29 - 6:31
    on the event object
    that you could use as well.
  • 6:31 - 6:35
    The most useful, I think, are
    the keyboard-related ones.
  • 6:35 - 6:38
    So that you can find out
    what keys the user was pressing
  • 6:38 - 6:41
    when the event happened,
    and use that to make a
  • 6:41 - 6:44
    keyboard-accessible interface,
    or a really fun game.
  • 6:44 - 6:47
    Oh, and one more thing:
  • 6:47 - 6:50
    you don't have to call this argument `e`.
  • 6:50 - 6:57
    That's typical, but some developers
    call it `evt`, or `event`.
  • 6:57 - 7:01
    It doesn't matter what you call it,
    as long as it's referring to
  • 7:01 - 7:04
    the first argument that the browser
    passes to your function,
  • 7:04 - 7:09
    and as long as you then
    refer to it that way later as well.
  • 7:09 - 7:13
    If you're having trouble with it,
    make sure you use console.log
  • 7:13 - 7:16
    to help you debug what's going on.
  • 7:16 - 7:17
    When you're a web developer,
  • 7:17 - 7:22
    the console is pretty much
    your best friend ever-- use it!
Title:
Using the event properties (Video Version)
Description:

more » « less
Video Language:
English
Duration:
07:23

English subtitles

Revisions