< Return to Video

7.15: Drag and Drop a File - p5.js Tutorial

  • 0:02 - 0:06
    In this video I wannna look at a feature
    of web browsers and something you can
  • 0:06 - 0:10
    do in JavaScript in general but also
    this thing is built in to p5.js library
  • 0:10 - 0:14
    and in a rather nice way that I enjoy
    and this feature is dragging a file
  • 0:14 - 0:19
    into the browser and making use of that
    file and that file could be an image
  • 0:19 - 0:23
    it could be a text file, it could be
    some sort of data file; data is a thing
  • 0:23 - 0:26
    that am gonna get into to in some future
    videos, which there's different kinds of
  • 0:26 - 0:29
    data formats so there's lot of potential
    to what you could do if you ask the
  • 0:29 - 0:34
    user to say hey, give me an image and I'll
    do something that's really exciting
  • 0:34 - 0:37
    and interesting in the browser with that
    image or give a hundred images
  • 0:37 - 0:41
    and even that. So let's look at how that's
    that works, so first of all what I mean
  • 0:41 - 0:44
    by that and I guess I probably don't need
    this diagram (laughs) but am gonna
  • 0:44 - 0:48
    draw it anyway; is on the webpage am
    gonna make an area on the page
  • 0:48 - 0:52
    right? Because am gonna have a
    canvas and I want an area on the
  • 0:52 - 0:58
    page where if I have some sort of file
    if I drag that file and release it there
  • 0:58 - 1:02
    and if this is an image I wanna see that
    image then suddenly appear and be
  • 1:02 - 1:04
    drawn into the canvas so how do you
    do that?
  • 1:04 - 1:10
    Let's go and look.. so I've sort of set
    this up in advance. I have this particular
  • 1:10 - 1:13
    example and you can see it's got exactly
    what am talking about.
  • 1:13 - 1:16
    This canvas is made with the
    createCanvas() function and you
  • 1:16 - 1:19
    can see background() is called so you
    you can see that it has a black
  • 1:19 - 1:24
    blackground, then there's above the
    canvas is this like dashed area
  • 1:24 - 1:28
    on the screen that says drag you file up
    here. Now where did that come from?
  • 1:28 - 1:32
    That is written into the HTML file
    so am gonna go over here and look
  • 1:32 - 1:35
    at the ahh.. --gonna hit cancel--
    and look at the HTML file and you
  • 1:35 - 1:40
    can see there it is. There is just a
    paragraph element, 'Drag you file here'
  • 1:40 - 1:44
    I gave it an id called dropzone; because
    am gonna need to refer to it
  • 1:44 - 1:48
    hey! that's the paragraph element that
    I wanna be able to drop something on to
  • 1:48 - 1:53
    and also I've just written some style
    some styles referring to that dropzone
  • 1:53 - 1:59
    and maybe just so it's a little bit more
    visible I might say font-size is 36pt
  • 1:59 - 2:03
    and maybe I'll do something like width is
    50%. So by the way this is also something
  • 2:03 - 2:06
    that you might have not have seen
    before, there's all of this units that
  • 2:06 - 2:11
    are happening in CSS: px for pixels; so
    actually like the number of pixels on
  • 2:11 - 2:15
    that padding, pt (point) for something
    that has to do with typography (laughs)
  • 2:15 - 2:18
    am sure somebody understands much
    better than I do, but you know
  • 2:18 - 2:22
    the larger the point, the larger the
    font-size but also percentage %, being
  • 2:22 - 2:26
    relative; so if this DOM element is
    always just gonna be 50% the size
  • 2:26 - 2:31
    of the page which.. you'll see now if I
    hit run again, you can see as I resize
  • 2:31 - 2:36
    the page that DOM element doesn't
    have a fixed size it's always just 50%
  • 2:36 - 2:39
    of the page. That's a nice little tip-bit
    that am adding into this video.
  • 2:39 - 2:42
    So how do I make it so that I can
    drag a file there?
  • 2:42 - 2:44
    Incedentally what happens if I drag a file
    right now?
  • 2:44 - 2:48
    I drag this file over here, I drop it and
    am lost I'm gone from the page
  • 2:48 - 2:52
    look and you can see what it is, is that
    file has just literally opened up in the
  • 2:52 - 2:55
    page. It's just a file of bacteria
    I don't know, I was trying to do
  • 2:55 - 2:58
    something other than rainbows and
    unicorns with maybe bacteria and I went
  • 2:58 - 3:01
    in the wrong direction (laughs)
    but it's interesting. Ok so umm
  • 3:01 - 3:07
    so I need to; I need to get this working
    so first of all I need to add some
  • 3:07 - 3:11
    javascript event to this element.
    I wanna handle the event, so the
  • 3:11 - 3:15
    browser handled that event for me
    right? When you dragged the file into the
  • 3:15 - 3:18
    browser it assumes like 'ooh I'm a
    browser, you're dragging me a file, I'll
  • 3:18 - 3:21
    open it.' I wanna say to the browser,
    "No no no no no noo; I'm in charge here.
  • 3:21 - 3:24
    Don't do what you're gonna do, I got
    something else for you, I got
  • 3:24 - 3:29
    something else planned." So if I come
    over here and I first I need to do is, get
  • 3:29 - 3:33
    ahh, am gonna make a variable and am
    gonna call select() so select, and let's
  • 3:33 - 3:37
    make this a global variable because I
    might wanna access it in other places
  • 3:37 - 3:42
    I'm gonna say select the dropzone,
    now something that am doing here
  • 3:42 - 3:44
    which might, you might find it a little
    bit confusing but I find
  • 3:44 - 3:48
    I find this to be just so convenient
    I like to just name my variable the same
  • 3:48 - 3:51
    name as the id, it's absolutely not
    requiered. The id can be totally
  • 3:51 - 3:54
    diff, and nobody knows whether,
    or cares whether they're the same
  • 3:54 - 3:58
    just you the programmer, for me it's
    a little bit convenient but I don't
  • 3:58 - 4:00
    want you to be confused that there's
    some like matching happening there
  • 4:00 - 4:03
    that's just I happen to name the variable
    the same name as the id
  • 4:03 - 4:10
    ok so what's next (sighs) you know that
    you can attach an event to an area
  • 4:10 - 4:15
    by referencing that event aah as a
    function called on that object
  • 4:15 - 4:18
    so the mousePressed() event, the
    changed() event the input() event
  • 4:18 - 4:21
    the mouseOver(), the mouseOut()
    this are events that I've looked at
  • 4:21 - 4:27
    in previous videos. I've got a new event
    for you (laughs) I don't remember
  • 4:27 - 4:31
    what it's called but I do know there's
    one that is drag.. dragOver() so
  • 4:31 - 4:37
    the dragOver event let's handle that with
    a function called highlight ok?
  • 4:37 - 4:47
    So I'm gonna say function highlight()
    dropzone dot style, background
  • 4:47 - 4:54
    color and let's give it a little gray
    am gonna zoom back out here
  • 4:54 - 5:01
    so now this should atleast I've handled
    one event dragOver, ok so now if I take
  • 5:01 - 5:04
    this file and drag it over here you see
    it becomes gray. Now it's never not
  • 5:04 - 5:09
    becoming gray but atleast I've got that
    event, so this by the way is nothing
  • 5:09 - 5:13
    to do so far this has nothing to do with
    the file am just handling an event
  • 5:13 - 5:16
    if you drag over it; if I move the mouse
    over it it doesn't do anything
  • 5:16 - 5:19
    if I drag the mouse over it without a file
    it doesn't do anything.
  • 5:19 - 5:24
    This is an event only for when I drag a
    file over it, you can see it becomes gray
  • 5:24 - 5:29
    so that's one thing I can do now what if
    I want if l'm like dragging a file and
  • 5:29 - 5:30
    then am like "no no no no no, bad
    idea bad idea take the file out"
  • 5:30 - 5:35
    maybe I wanna unhighlight it right?
    So I could then say aahh I can't remeber
  • 5:35 - 5:40
    what it's called but I think it's dragOut()
    unhighlight and I'll just reset the
  • 5:40 - 5:46
    background color oops I'm gonna write
    another function and I''m gonna reset the
  • 5:46 - 5:51
    background color to white, now you might
    be asking yourself a question, and I need
  • 5:51 - 5:55
    to get to this in another video
    but you might be asking a question
  • 5:55 - 5:59
    like why do I need this two functions for?
    I could really have just one function that
  • 5:59 - 6:02
    accepts an argument: white or black..
    I mean white or grey,
  • 6:02 - 6:08
    and then I could say dragOver call that
    function with gr.. white.. gray huuh
  • 6:08 - 6:12
    dragOut call that function with white.
    This is actually quite possible in
  • 6:12 - 6:15
    javascript I'm doing it this way for
    convenience because again the other
  • 6:15 - 6:19
    way requires a sort of some trickiness
    for how do I get an argument kind of
  • 6:19 - 6:23
    into those functions. I'd love to get to
    that, remind me! remind me! remind me.
  • 6:23 - 6:27
    I'll remeber, am gonna think of a good
    scenario and try to make a nice video that
  • 6:27 - 6:29
    cause it sort of like a fundamental
    aspect of javascript
  • 6:29 - 6:34
    it uses a this thing called a closure
    ok so but we're not doing that right now
  • 6:34 - 6:37
    am not doing that right now am gonna
    leave these as two separate functions
  • 6:37 - 6:39
    but you're right to ask that question
    (laughs) cause you being the
  • 6:39 - 6:44
    metaphorical person who I think I'm
    talking to I mean you're.. umm yeah
  • 6:44 - 6:48
    ok so umm oop undefined is not a
    function line 8.
  • 6:48 - 6:50
    drag.. probably dragOut is wrong.
  • 6:50 - 6:55
    (laughs) So how do we figure this out,
    I should stop being so like am just
  • 6:55 - 6:59
    gonna imagine what the p5 DOM API
    has in it and I should probably go
  • 6:59 - 7:03
    and look this stuff up, so if you were
    doing this and you got this error
  • 7:03 - 7:05
    and you wanted to figure out how to
    find it; you could open up a browser
  • 7:05 - 7:10
    you go to p5js.org
    am gonna click on this uuhh umm
  • 7:10 - 7:13
    you should be able to find this under
    reference and am sure that will change
  • 7:13 - 7:15
    at some point soon but an easier way
    to find is am gonna go under libraries
  • 7:15 - 7:20
    and am gonna go to p5 DOM and
    now I'm gonna see oh look at all of these
  • 7:20 - 7:25
    look at all these methods and look at
    that I called the dragOut but we can see
  • 7:25 - 7:30
    here the mess.. the the event is actually
    called dragLeave() that's the name of
  • 7:30 - 7:33
    the function dragLeave.. and by the way
    there's addClass() and removeClass()
  • 7:33 - 7:37
    huh interestingly class().. am thinking
    of something that happened in the
  • 7:37 - 7:39
    previous video and I wonder if that
    should actually be changed but anyway
  • 7:39 - 7:43
    ahh it's a side topic I'll have to think
    about that in my own time, so
  • 7:43 - 7:46
    dragLeave() is the name of the function
    that I wanted
  • 7:46 - 7:49
    so am gonna minimize that, and come
    back and I could've clicked on it to see
  • 7:49 - 7:52
    some more information but
    I don't need to do that right now
  • 7:52 - 7:56
    I feel confident that I can just type
    dragLeave here and now I can run this
  • 7:56 - 8:00
    again and here we go
    file leave, file leave look at that
  • 8:00 - 8:05
    what a nice little am so good at
    user interface interaction design (laughs)
  • 8:05 - 8:08
    am being sarcastic, because am not
    good at that
  • 8:08 - 8:11
    umm but you can see this is a bit more
    intuitive atleast to be able to handle
  • 8:11 - 8:14
    this type of events of when you have the
    thing over and when you have
  • 8:14 - 8:18
    the thing out so, but we're missing kind
    of the exciting.. this is just like
  • 8:18 - 8:21
    nothing is gonna happen like if I;
    look that's the same thing happened
  • 8:21 - 8:26
    that event the drop event when I let go
    of the mouse, when I perform this
  • 8:26 - 8:29
    release action; this is me miming the
    release action umm I need to handle
  • 8:29 - 8:37
    that event and that event is handled
    with the drop() function. Now what goes
  • 8:37 - 8:42
    in here? Let's think about this for a
    second so let me comment this out
  • 8:42 - 8:47
    and run this there.. there are two things
    that have to happen and this is
  • 8:47 - 8:53
    tricky; one thing that has to happen is
    I need to read that file the other
  • 8:53 - 8:56
    thing that needs to happen is I might
    wanna uuh this is ok what actually
  • 8:56 - 9:00
    there really aren't two things I was like
    there aren't, intuitively there aren't two
  • 9:00 - 9:05
    things; there's one moment right?
    I drop. So the moment I drop I might
  • 9:05 - 9:08
    wanna handle that event by like
    applying some style to it like
  • 9:08 - 9:11
    unhighlighitng it and then I also wanna
    read the file that's being dropped
  • 9:11 - 9:16
    but those are actually divided into two
    separate callback, and there's a really
  • 9:16 - 9:20
    good reason for that and the good
    reason for that is that I might wanna
  • 9:20 - 9:24
    take all of these images. What if I drag
    all of these images into the browser
  • 9:24 - 9:28
    I need a callback to handle each file
    one at a time but there's only one
  • 9:28 - 9:33
    moment of dropping so umm so in
    that sense and I forget which is which
  • 9:33 - 9:38
    let's go back to the reference, so oops
    ahh let me go back to the reference here
  • 9:38 - 9:42
    and I'm gonna click on this drop event
    and you can see we could read this
  • 9:42 - 9:48
    (callback, callback) so look at this
    callb.. the first callback is triggered
  • 9:48 - 9:52
    when files are dropped so that's the
    event the actual event of dropping the
  • 9:52 - 9:58
    files then the second callback is to when
    the file is loaded and actually this also
  • 9:58 - 10:02
    happens at separate time cause what if
    you drag and drop like 500MB image
  • 10:02 - 10:06
    file right? That drop moment happens
    instantly but it might take the browser
  • 10:06 - 10:10
    quite a while to read that very large file
    so that's why those are two separate
  • 10:10 - 10:15
    events so I can go back to my code
    and I can say you know what when I
  • 10:15 - 10:21
    drop it I also wanna unhighlight the box
    right? That's ah I wanna unhighlight it
  • 10:21 - 10:27
    and then I wanna handle the file
    so am now acc.. am doing two things
  • 10:27 - 10:31
    as soon as I drop it, unhighlight the box
    and also trigger a new function gotFile()
  • 10:31 - 10:38
    function gotFile(file) now interestingly
    enough
  • 10:38 - 10:43
    so let's just run this and atleast see now
    when I drag the unicorn over here
  • 10:43 - 10:47
    ahh let's just only the unicorn please
    it highlights it unhighlights
  • 10:47 - 10:50
    it highlights it unhighlights
    let's drop it, it unhighlights and
  • 10:50 - 10:56
    I've over ridden the default browser
    behaviour the default browser behaviour
  • 10:56 - 11:01
    would have been to open that file
    but I've now over ridden by whatever
  • 11:01 - 11:04
    goes here. So now I can do whatever
    I want and not only can I do whatever
  • 11:04 - 11:06
    I want; I can do whatever I want with
    the data in that file.
  • 11:06 - 11:13
    For example, I could say createImg(file)
    now I dont think this is actually gonna
  • 11:13 - 11:17
    work (laughs) I know it's not gonna work
    cause that's.. so but let's try it with
  • 11:17 - 11:21
    this rainbow right? It, something
    appeared there but it's like a broken
  • 11:21 - 11:27
    image so this thing here the argument
    that comes in to the function
  • 11:27 - 11:31
    this might actually be something rather
    new to you am realising because most
  • 11:31 - 11:34
    of our callbacks like highlight() or
    unhighlight() like don't have data
  • 11:34 - 11:39
    that arrives with it; so this is a new
    scenario this callback p5 is not only
  • 11:39 - 11:43
    triggering the callback when the file is
    ready to be loaded but it's also handing
  • 11:43 - 11:47
    a variable and inside that variable is
    just.. p5 is like such a nice wonderful
  • 11:47 - 11:51
    friend that p5 is like look am gonna
    hand you am gonna call this function
  • 11:51 - 11:53
    am gonna hand you this variable and
    am gonna fill that variable for you
  • 11:53 - 11:57
    with all the data associated with that
    file, so this file variable is actually
  • 11:57 - 12:02
    an instance of a p5 file object and
    there's all of this information like
  • 12:02 - 12:06
    I know how big the file is what's it's
    name is, there's lots of metadata
  • 12:06 - 12:10
    associated with that file and something
    that's in that file is a property called
  • 12:10 - 12:15
    data and that data might just be
    the text that's in the file , it might be
  • 12:15 - 12:19
    the the other some other data format
    that's in the file or it might be image data
  • 12:19 - 12:26
    so if I createImg(file.data)
    now watch this!
  • 12:26 - 12:33
    uhh oh oh , ok let's look at the reference
    did I, sure I saved that I ran that am
  • 12:33 - 12:37
    pretty sure that was right but you know
    we have a bit of a problem here, it's not
  • 12:37 - 12:41
    working so let me go back to the
    reference and am gonna go to the
  • 12:41 - 12:47
    reference and I wanna look for file
    mmmh file this is where this is
  • 12:47 - 12:53
    this is the um where's the p5 file object?
    somebody find it for me, quickly! (laughs)
  • 12:53 - 13:01
    p5 image p5.file am not seeing it umm
    so you know for some reason it's not in
  • 13:01 - 13:05
    the reference which it probably should
    be am gonna, am taking drastic
  • 13:05 - 13:09
    measures here and I'm gonna go to the
    p5.js GitHub repository
  • 13:09 - 13:12
    I should just actually just look for..
    you know what I should look for an
  • 13:12 - 13:15
    example that does thi. There's actually
    an example that does this
  • 13:15 - 13:16
    I could go.. I was gonna go into the
    GItHub repository for this..
  • 13:16 - 13:19
    am just gonna like you know, fast
    forward through the video like
  • 13:19 - 13:22
    30 seconds or a minute you can skip
    this part but am gonna go look for
  • 13:22 - 13:28
    under ahh learning maybe or under
    DOM let's look at ahh
  • 13:28 - 13:34
    lets ahh under p5 element am gonna
    look for the drop this is probably not
  • 13:34 - 13:40
    the right place.. yeah yeaah no I wanna
    under examples ahh DOM ran-dom
  • 13:40 - 13:46
    ran-dom dom, ahh drop there we go ahh
    ahh ahhh file.data
  • 13:46 - 13:51
    well by golly I was right wasn't I?
    createImg(file.data) so that's
  • 13:51 - 13:55
    what I meant to do, why didn't that work?
    (laughs) Let's just ..
  • 13:55 - 13:59
    ok well let's just be a little methodical
    about this maybe something else
  • 13:59 - 14:03
    is going wrong that I don't know about
    so for example umm let's look
  • 14:03 - 14:08
    let's see if there's some of the metadatas
    there, Imma say createP(file.name)
  • 14:08 - 14:19
    ahh createP(file.type), createP(file.size)
    and so there.. there're.. all
  • 14:19 - 14:23
    A file has all this metadata associated
    with it; it's name, it's type
  • 14:23 - 14:26
    it's size so there are strings that's just
    data and data should actually be the
  • 14:26 - 14:30
    image stuff so let's run this and let me
    drag in this rainbow file
  • 14:30 - 14:33
    and you can see undefined drop undefined
  • 14:33 - 14:38
    ooh you know what, I wonder if I
    guess what I think I might have done
  • 14:38 - 14:42
    I know what I did, I probably
    reversed the order of those
  • 14:42 - 14:48
    I reversed the order of those uhh
    callbacks. So this is a very common
  • 14:48 - 14:50
    thing that happens, aren't you
    enjoying the fact that I just mess this
  • 14:50 - 14:52
    stuff up all the time? I really hope
    you are
  • 14:52 - 14:56
    so this is, this is something so you know
    we've looked at this and realised
  • 14:56 - 15:00
    like the callback is triggering
    I mean ofcourse it's triggering
  • 15:00 - 15:02
    because there are two callbacks
    both of them are triggering
  • 15:02 - 15:04
    but I'm triggering them probably in the
    wrong order
  • 15:04 - 15:10
    so let's go back to the p5 reference
    ahh ahh libraries; DOM;
  • 15:10 - 15:17
    and let's look at this drop function
    right here and let's read this again
  • 15:17 - 15:21
    the first callback triggers when the files
    are dropped
  • 15:21 - 15:24
    the second callback triggers to receive
    the loaded file
  • 15:24 - 15:29
    isn't that the way I did it?
    aah first callback but..
  • 15:29 - 15:32
    but I have a feeling the reference
    so it wasn't entirely my mistake
  • 15:32 - 15:39
    I have a feeling the reference misled me
    let's switch the order of these
  • 15:39 - 15:43
    it's probably the first argument is the
    calback to handle the file
  • 15:43 - 15:49
    and the second argument is the callback
    to handle the event that's just
  • 15:49 - 15:55
    the dropping event so let's try this again
    and here we go yaaaay!!
  • 15:55 - 15:59
    so I've got my.. and let's let's do
    something where i say var image equals
  • 16:00 - 16:07
    crreateImg() I say image.size(100,100)
    and I'm gonna do createP
  • 16:07 - 16:15
    am gonna just join one string ahh
    so am gonna take this out just to make
  • 16:15 - 16:17
    this look a little shorter, now let's look
    at this again
  • 16:17 - 16:20
    am gonna make a bigger window we can
    have this nice moment where I drag
  • 16:20 - 16:24
    the unicorn here , there's our unicorn
    I drag the rainbow here, there's the
  • 16:24 - 16:27
    rainbow. I drag another rainbow here
    there's that.
  • 16:27 - 16:31
    So you can see here, the files are
    coming in I'm adding them to the page
  • 16:31 - 16:35
    and also I get information; how big was
    the file, what was the name of the file
  • 16:35 - 16:37
    I could use that information I'm just
    sort of like spitting it right back
  • 16:37 - 16:41
    so (takes a deep breath) this is
    interesting hopefully
  • 16:41 - 16:44
    I mean, so am not really doing anything
    that and also I should mention
  • 16:44 - 16:46
    let's just prove that this works, am
  • 16:46 - 16:48
    gonna ref.. refresh this page to start
    over
  • 16:48 - 16:52
    let's drag all of the images all at once
    and there you go all of them appear
  • 16:52 - 16:56
    so we've handled.. that callback was
    triggered many many times for each
  • 16:56 - 17:00
    image um so am gonna do one more
    thing which I think it just kinda of like
  • 17:00 - 17:04
    makes this seem a bit more magical
    so you can have a DOM element
  • 17:04 - 17:08
    that has like a dotted line that says drag
    your files here but anything
  • 17:08 - 17:12
    can be a DOM element that you drag
    stuff on to you know like a button could
  • 17:12 - 17:14
    or a slider could even though that
    barely makes any sense
  • 17:14 - 17:17
    but let's actually do something to this
    example am gonna do save as
  • 17:17 - 17:23
    ahh drag and drop 2
    am gonna say you know what forget
  • 17:23 - 17:27
    about this dropzone thing take that out
    of there, take that out of there;
  • 17:27 - 17:36
    why not store the canvas in a variable
    and say canvas.dragOver
  • 17:36 - 17:38
    canvas.dragLeave, canvas.Drop
  • 17:38 - 17:43
    so look at this if I run this now
    I can just take this image and drag
  • 17:43 - 17:47
    it over the canvas; dropzone is not
    defined ah oh alright I don't need any
  • 17:47 - 17:49
    of this styling stuff aymore to be
    honest with you I dont
  • 17:49 - 17:53
    am deciding like not to care about that
    I can do something much simpler
  • 17:53 - 17:57
    I can just use the single callback of
    dropping it you know am not..
  • 17:57 - 18:02
    just skipping the visual feedback part
    right? So anything can be a drag and
  • 18:02 - 18:08
    drop element and now let's think about
    this even more in a different way what if
  • 18:08 - 18:12
    when I drag the element on to the
    canvas I wanna see the image in the
  • 18:12 - 18:16
    canvas, so what can I do?
    I can just call the image function
  • 18:16 - 18:22
    image(img, 0, 0, width, height)
    so this is the difference again
  • 18:22 - 18:29
    between making a DOM element image
    and then drawing and.. versus drawing an
  • 18:29 - 18:32
    image into the canvas. The image()
    function draws that image whether it's
  • 18:32 - 18:36
    through draw image or create image into
    the canvas, createImg makes the
  • 18:36 - 18:41
    DOM elements; so now we should be
    too see this ahhh that. Now am only
  • 18:41 - 18:44
    seeing the top left corner of the image
    cause they're kind of small
  • 18:44 - 18:49
    am surprised about that because am
    drawing it supposedly ahh with the width
  • 18:49 - 18:54
    and height of the umm of.. so am kind
    of surprised; I feel like that's a little
  • 18:54 - 18:57
    bit of like a p5 bug there. I don't know
    whay that's not working am gonna have to
  • 18:57 - 19:01
    look into.. examine that. But also the
    thing I wanted to show you is that
  • 19:01 - 19:06
    I don't even though I might have it do it
    this way to create the image via umm
  • 19:06 - 19:12
    this ahh umm to create the image ahh
    as a way of like getting the data from the
  • 19:12 - 19:19
    file I can use, I can use the p5 function
    image.hide(). So I can make that DOM
  • 19:19 - 19:25
    element purely to open that file and
    then I can hide it instantly and then draw
  • 19:25 - 19:28
    it into the canvas. And I'm actually sort
    of surprised that this is working
  • 19:28 - 19:31
    cause I realised that.. I was sort of
    thinking that there might be another
  • 19:31 - 19:34
    problem associated with this.
    Ohh but now it's working I don't lnow
  • 19:34 - 19:37
    what I missed with the width and height
    before umm maybe that.. oh that size
  • 19:37 - 19:40
    thing probably messed things up.
    But you can see look at this that
  • 19:40 - 19:44
    was a transparent image o you can see
    now the.. as I drag and drop the images
  • 19:44 - 19:49
    on to the canvas I see the image there.
    So that concludes this particular video
  • 19:49 - 19:53
    umm where what I was hoping to
    demonstrate I think I did; which is
  • 19:53 - 19:56
    the idea that you can create on any
    element on the page and you can
  • 19:56 - 20:00
    handle when you're dragging the file
    over it, when you decide you're not
  • 20:00 - 20:02
    gonna drag the file over it
    and when you actually drop
  • 20:02 - 20:06
    that file and if it's an image file
    you can use that image to display the
  • 20:06 - 20:09
    image data and umm in.. talking
    about other videos in the future
  • 20:09 - 20:12
    I'll probably show you how to
    do it with a text file and do something
  • 20:12 - 20:15
    like word counting in the text file
    but that's.. that you might investigate
  • 20:15 - 20:20
    the loadStrings() function and sort of see
    how you might do that umm..
  • 20:20 - 20:22
    I don't have a good exercise I can't think
    of one.. I'll write something in the
  • 20:22 - 20:26
    description. Ok thanks and I think I have
    one more sort of DOM video to make
  • 20:26 - 20:28
    after this one.
Title:
7.15: Drag and Drop a File - p5.js Tutorial
Description:

more » « less
Video Language:
English
Duration:
20:28

English subtitles

Revisions