< Return to Video

How to build a Spinning Pacman - Talk.CSS #50

  • 0:00 - 0:11
    [Music]
  • 0:01 - 0:14
    okay we're gonna shut up no wait okay
  • 0:11 - 0:14
    hear me now yes
  • 0:14 - 0:21
    I just realized that my mind was muted
  • 0:18 - 0:25
    by my kid Aaliyah so that's why you
  • 0:21 - 0:27
    don't hear me until now okay anyway
  • 0:25 - 0:30
    since you can hear me my toe is gonna be
  • 0:27 - 0:31
    but be about this how to build this
  • 0:30 - 0:35
    spinning Pegman shit but what this
  • 0:31 - 0:38
    exactly is this inning pac-man shit so
  • 0:35 - 0:39
    one day I was just trying to find
  • 0:38 - 0:41
    something the beautiful my JavaScript
  • 0:39 - 0:46
    cost and then someone came away an idea
  • 0:41 - 0:49
    that is that looks something like this
  • 0:46 - 0:51
    which is pretty cool so I was thinking
  • 0:49 - 0:55
    okay I want to do the mouth thing and
  • 0:51 - 0:59
    then they did the picture stuff and I
  • 0:55 - 1:02
    didn't really want to work with like
  • 0:59 - 1:05
    pictures on inside my JavaScript course
  • 1:02 - 1:09
    so I decided to build spinning pac-man
  • 1:05 - 1:12
    so I talked Pegman and then I mean it
  • 1:09 - 1:16
    spins it follows the mouse and I add it
  • 1:12 - 1:21
    yeah so we just gonna add a music to it
  • 1:16 - 1:25
    to make it more fun yeah can I help it
  • 1:21 - 1:28
    so that's what this is for by the way by
  • 1:25 - 1:30
    the way interesting thing this just this
  • 1:28 - 1:32
    phone that I'm using here for this
  • 1:30 - 1:35
    spinning thingy
  • 1:32 - 1:38
    it's called Caesar dressing I have no
  • 1:35 - 1:41
    idea how I found it but I found it and
  • 1:38 - 1:44
    it it's kind of funny so I think it was
  • 1:41 - 1:47
    kind of nice introduction to fonts okay
  • 1:44 - 1:49
    so anyway to build this spinning pacman
  • 1:47 - 1:53
    shit right first of all I need to build
  • 1:49 - 1:56
    a pacman so the pen where's my mouth you
  • 1:53 - 1:58
    can't have a mouth on key so anyway on
  • 1:56 - 2:01
    the left side you see the pac-man which
  • 1:58 - 2:05
    is built with SVG combination of layers
  • 2:01 - 2:06
    not too hot so going on a circle and
  • 2:05 - 2:11
    probably a triangle you build a pacman
  • 2:06 - 2:14
    but the first thing to do is to know add
  • 2:11 - 2:17
    some shadows to it so this is why I used
  • 2:14 - 2:19
    dropped at all so instead of boxer load
  • 2:17 - 2:21
    we can stop shadow allows you to rep you
  • 2:19 - 2:23
    as Ouija elements as you like to it
  • 2:21 - 2:26
    makes it looks nicer you can see the
  • 2:23 - 2:28
    drop shadow in the eyes as well so that
  • 2:26 - 2:31
    kind of looks nice just want to point
  • 2:28 - 2:33
    this out before I continue no the thing
  • 2:31 - 2:37
    about this spinning Pac Man thing like
  • 2:33 - 2:43
    if you I don't think you saw it here but
  • 2:37 - 2:47
    let's see if I go am i sharing can you
  • 2:43 - 2:51
    see my screen now I mean the the browser
  • 2:47 - 2:53
    1 & Co cuz moving around looking very
  • 2:51 - 2:56
    confused okay you know I think I said
  • 2:53 - 3:00
    the wrong thing so let me share the
  • 2:56 - 3:00
    entire thing so cuz I need to switch
  • 3:01 - 3:05
    okay so you see this this this spinning
  • 3:04 - 3:08
    thing so basically we're trying to be
  • 3:05 - 3:12
    I'm showing how to build a spinning
  • 3:08 - 3:15
    thingy right so it follows the mouse the
  • 3:12 - 3:18
    first thing then is to be able to write
  • 3:15 - 3:21
    the CSS you know to go here or go here
  • 3:18 - 3:23
    or go here and or go here go here
  • 3:21 - 3:25
    depends on where you want the mouse to
  • 3:23 - 3:28
    go so basically I specialize into it
  • 3:25 - 3:32
    directions the I just saying
  • 3:28 - 3:34
    north-northwest is software soft
  • 3:32 - 3:37
    yourself blah blah blah blah all the way
  • 3:34 - 3:39
    so this iterations the default by
  • 3:37 - 3:42
    default if you put everything on a
  • 3:39 - 3:45
    normal horizontal plane is facing to the
  • 3:42 - 3:48
    right or rather fitting to the east and
  • 3:45 - 3:50
    that is rotation of 0 degrees if you
  • 3:48 - 3:54
    wanna rotate downwards its rotation of
  • 3:50 - 3:57
    you know 45 degrees to the middle and
  • 3:54 - 4:00
    then 90 degrees to the bottom if you
  • 3:57 - 4:02
    rotate upwards it so flow easy it goes
  • 4:00 - 4:04
    like minus 45 degrees at minus 90
  • 4:02 - 4:06
    degrees but that's where the easy part
  • 4:04 - 4:10
    ends so you have this 5 now we need the
  • 4:06 - 4:12
    other three what happens you want to say
  • 4:10 - 4:13
    like the bottom left corner if you
  • 4:12 - 4:16
    cannot just do like rotate
  • 4:13 - 4:20
    135 degrees because you'll get the
  • 4:16 - 4:23
    upside down magnet right so what you can
  • 4:20 - 4:26
    do is to first flip the penguin with
  • 4:23 - 4:27
    scale X or still Y depending on what you
  • 4:26 - 4:30
    want
  • 4:27 - 4:34
    and you apply a rotation then again the
  • 4:30 - 4:37
    the way transform works is if depending
  • 4:34 - 4:38
    on what you do first it follows in order
  • 4:37 - 4:41
    so if you rotate then ski or its cure
  • 4:38 - 4:44
    they rotate the answer changes according
  • 4:41 - 4:47
    to - - how you write it
  • 4:44 - 4:48
    so pay attention to this one when you if
  • 4:47 - 4:50
    you want to make something like this
  • 4:48 - 4:52
    then are you gonna hit and going
  • 4:50 - 4:54
    calculate all the different possible
  • 4:52 - 4:57
    values of like if you if you still X if
  • 4:54 - 4:59
    you see Y so this is the the range of
  • 4:57 - 5:02
    values will come back to this one in a
  • 4:59 - 5:06
    bit because we need this info to carry
  • 5:02 - 5:08
    on building this thing then the second
  • 5:06 - 5:11
    thing we know is if you know if you want
  • 5:08 - 5:16
    something to follow the mouse you need
  • 5:11 - 5:18
    to go to the hips you need to like put
  • 5:16 - 5:21
    okay this is that point for this object
  • 5:18 - 5:23
    and then you want it to follow the mouse
  • 5:21 - 5:27
    so you go from the center point and then
  • 5:23 - 5:30
    calculate where the mouse is and to get
  • 5:27 - 5:32
    the angle which you really need we need
  • 5:30 - 5:35
    trigonometry what is it real
  • 5:32 - 5:38
    I think it's got triggers remember you
  • 5:35 - 5:41
    need two opposite distance and the
  • 5:38 - 5:43
    adjacent distance so this is these are
  • 5:41 - 5:45
    the two values we need to be able to
  • 5:43 - 5:48
    make this thing then the rest is no
  • 5:45 - 5:51
    JavaScript where D so just you've got to
  • 5:48 - 5:56
    follow along you can know go to this
  • 5:51 - 6:00
    copán celebre KH pants yes da all Y and
  • 5:56 - 6:07
    V X okay I'm just gonna leave it aside
  • 6:00 - 6:12
    it's too small so forget it okay and
  • 6:07 - 6:18
    then I am going to move to here close my
  • 6:12 - 6:20
    keynote sorry I need that slight later
  • 6:18 - 6:22
    okay so basically we're back here with
  • 6:20 - 6:24
    this spinning pac-man thing I'm going to
  • 6:22 - 6:27
    comment out so Nicole
  • 6:24 - 6:29
    [Music]
  • 6:27 - 6:32
    all right we are at a stage where
  • 6:29 - 6:33
    there's nothing nothing is being written
  • 6:32 - 6:36
    so the first thing we need to do is to
  • 6:33 - 6:38
    find this middle of the pac-man to find
  • 6:36 - 6:41
    the middle of the payment is easy you
  • 6:38 - 6:43
    see for each here because eventually I'm
  • 6:41 - 6:45
    gonna do like nine eight pac-man sent in
  • 6:43 - 6:49
    a robot right now I only have one and
  • 6:45 - 6:52
    Dom I comment out the rest it's here
  • 6:49 - 6:55
    like I just deleted the rest but if you
  • 6:52 - 6:58
    if we do this that we have more okay
  • 6:55 - 7:01
    so there's only one tech man in the Dom
  • 6:58 - 7:04
    now so this just reduces this books for
  • 7:01 - 7:08
    one so first we want to get the middle
  • 7:04 - 7:11
    so and we can use the gap on inclined
  • 7:08 - 7:12
    rect which gave if you top left bottom
  • 7:11 - 7:15
    right and then the width and height so
  • 7:12 - 7:17
    you can get the center by doing the
  • 7:15 - 7:19
    average of the left and the right and
  • 7:17 - 7:22
    why center by doing the top and the
  • 7:19 - 7:27
    bottom average then if you want to get
  • 7:22 - 7:31
    the mounds location like this this the
  • 7:27 - 7:33
    most heap location right is Mouse X and
  • 7:31 - 7:36
    mouse why it comes from the event and
  • 7:33 - 7:38
    notice I'm using pointer move event so
  • 7:36 - 7:41
    initially you can also do this with
  • 7:38 - 7:45
    mouse move right but mouse move it
  • 7:41 - 7:48
    doesn't work with touch devices pointer
  • 7:45 - 7:52
    move is a event that builds on top of
  • 7:48 - 7:54
    Mouse move and also touch events so we
  • 7:52 - 7:56
    can handle both Mouse and touch events
  • 7:54 - 7:59
    at the same time no idea why I'm playing
  • 7:56 - 8:01
    JavaScript here in this in the CSS thing
  • 7:59 - 8:03
    but I thought it would be interesting
  • 8:01 - 8:07
    cuz pointer movers are requires certain
  • 8:03 - 8:10
    CSS stuff which I will go into in a bit
  • 8:07 - 8:16
    I want to talk about the rotation thing
  • 8:10 - 8:19
    first now to get the difference in you
  • 8:16 - 8:22
    know we have this opposite and adjacent
  • 8:19 - 8:27
    thing that we need we can allocate
  • 8:22 - 8:29
    either in this case I locate the Y to be
  • 8:27 - 8:33
    opposite value and then the Delta X to
  • 8:29 - 8:36
    be the adjacent value so to find this
  • 8:33 - 8:39
    value I just - Mouse X with the center
  • 8:36 - 8:40
    and you know Mouse wife who's the center
  • 8:39 - 8:43
    so I get
  • 8:40 - 8:48
    positive and negative values I think I
  • 8:43 - 8:49
    can do debug and show you just to whoa
  • 8:48 - 8:58
    okay
  • 8:49 - 9:22
    this cannot be OCD OCD
  • 8:58 - 9:24
    okay so Tata X okay yeah so we can see
  • 9:22 - 9:27
    we can see the the positive the x and y
  • 9:24 - 9:30
    values that we'll be using for this
  • 9:27 - 9:33
    calculation of the thing an order of the
  • 9:30 - 9:36
    angle now what happened next was we need
  • 9:33 - 9:41
    to calculate the angle and when I was
  • 9:36 - 9:47
    googling around on NBN I realized that
  • 9:41 - 9:49
    no there are two different functions we
  • 9:47 - 9:54
    can use this called 1s got meth a 10 and
  • 9:49 - 9:57
    one's got meth a 10 - and they both give
  • 9:54 - 9:59
    different results so it was quite
  • 9:57 - 10:01
    interesting I chose I the the key is
  • 9:59 - 10:02
    choose either one of them it doesn't
  • 10:01 - 10:04
    really matter
  • 10:02 - 10:07
    it then it calculates radians and then
  • 10:04 - 10:10
    you have to convert the radians back
  • 10:07 - 10:14
    into degrees which is been by this
  • 10:10 - 10:16
    formula over here on mdn and i just
  • 10:14 - 10:21
    plugged it in and we can calculate the
  • 10:16 - 10:23
    angle so that's that's the CSS spot
  • 10:21 - 10:26
    there we want to show the angle thing
  • 10:23 - 10:30
    right so here we have the angle you can
  • 10:26 - 10:31
    see here we here with zero degrees which
  • 10:30 - 10:36
    is exactly what we want
  • 10:31 - 10:39
    moving down 45 to 90 and if you move
  • 10:36 - 10:43
    past you see 1 and 35 they move a little
  • 10:39 - 10:46
    bit further it goes to 180 then it goes
  • 10:43 - 10:49
    in the negative 180 so noticing these
  • 10:46 - 10:51
    values will help us decide which
  • 10:49 - 10:55
    transforms we are use
  • 10:51 - 10:57
    math a 10-2 I think goes to my math 810
  • 10:55 - 10:59
    without the tool goes to 90 degrees so
  • 10:57 - 11:01
    that would have to change we have to
  • 10:59 - 11:01
    change our formula if we want to use
  • 11:01 - 11:04
    that one
  • 11:01 - 11:07
    so going back to here we see that hey we
  • 11:04 - 11:11
    use 135 degrees
  • 11:07 - 11:16
    so where's 1 3 5 8 then there's a
  • 11:11 - 11:18
    therefore values rotate 45 - 45 - 135 or
  • 11:16 - 11:22
    you know this yes so I decided to use
  • 11:18 - 11:25
    what they want 3 5 in ski - KY - one
  • 11:22 - 11:29
    step as it feels like the easiest thing
  • 11:25 - 11:39
    then I'm just going to do this so let's
  • 11:29 - 11:42
    not do the math absolute lymphocyte this
  • 11:39 - 11:45
    is what yes if you don't do the absolute
  • 11:42 - 11:49
    it goes into a negative value it goes
  • 11:45 - 11:52
    the other way around which basically at
  • 11:49 - 11:56
    90 degrees at 90 degrees we want to swap
  • 11:52 - 11:59
    over because 90 degrees 91 this is kind
  • 11:56 - 12:00
    of like - hundred plus degrees or
  • 11:59 - 12:03
    something I can't remember what the
  • 12:00 - 12:06
    exact formula was thinking through my
  • 12:03 - 12:10
    head but it's some we have to check
  • 12:06 - 12:12
    whether the angle value is more than 90
  • 12:10 - 12:14
    if there's one an ID we flip it over
  • 12:12 - 12:19
    with scare Y then there's the the part
  • 12:14 - 12:21
    where we do work on this 3 this this few
  • 12:19 - 12:24
    faces before 90 degrees
  • 12:21 - 12:27
    oh yeah here it's 90 degrees is here
  • 12:24 - 12:30
    once they go past 90 we need to flip so
  • 12:27 - 12:33
    we can work on this this view before
  • 12:30 - 12:38
    night degrees we want to use the normal
  • 12:33 - 12:40
    unfit Pegman then after - 90 degrees we
  • 12:38 - 12:42
    also want to flip which is why we need a
  • 12:40 - 12:45
    mess not absolute thinking and that's
  • 12:42 - 12:50
    how I built this Pegman thing and then
  • 12:45 - 12:52
    we just need to know copy/paste more of
  • 12:50 - 12:57
    this if you want more payments and it's
  • 12:52 - 13:02
    it ok just having fun with the headers
  • 12:57 - 13:04
    and stuff mMmmm okay music music the
  • 13:02 - 13:08
    music is not here eyes
  • 13:04 - 13:15
    screen ah okay but I can't I can go back
  • 13:08 - 13:15
    to the sighs mm was it this one this one
  • 13:16 - 13:28
    really you know fear not check out here
  • 13:23 - 13:31
    yeah okay I'm not keen on fear I just
  • 13:28 - 13:33
    don't have the time wait fine
  • 13:31 - 13:35
    next time when you can come on a night
  • 13:33 - 13:40
    we can talk about phones oh my god
  • 13:35 - 13:40
    that's so many opinions okay okay ready
  • 13:40 - 13:43
    [Music]
  • 13:46 - 13:52
    [Music]
  • 13:48 - 13:54
    anyone have any questions like when I do
  • 13:52 - 13:56
    this line or you're either a question so
  • 13:54 - 14:02
    I just think I do online
  • 13:56 - 14:05
    I feel anything without coupling the
  • 14:02 - 14:08
    message yeah you I think that's how he
  • 14:05 - 14:10
    was soon as but I don't even have a pair
  • 14:08 - 14:15
    counting the free accounts freeloading
  • 14:10 - 14:15
    off my car um anyway
Title:
How to build a Spinning Pacman - Talk.CSS #50
Description:

more » « less
Duration:
14:14

English subtitles

Revisions