- View Offline
So application's working pretty well for desktop machines, but this is the perfect type of app you might want on the tablet. And when I was doing this project, my daughter immediately want to play this one the iPad. Right now if you take a look at this project on the iPad. You'll notice that you can't really drag elements. It doesn't understand click events on the iPad. And the other thing is if you rotate this into vertical view, you'll see that the elements don't quite fit the screen. And when you rotate them back you'll see that they're also sort of too big, so we're going to have to adjust a few things to make this work.
So the first thing we need to do is fix an issue with the view port, so that whenever we rotate the device, it'll display it properly. The view port is how iPads and iPhones understand a browser's width. And normally in most web applications, you want to set the view port to whatever size your device would be. So we're going to do something unusual here, and what we want to do is, go over to the head section of our HTML page. And somewhere in there add in a meta tag, and the meta tag is going to have a name of view port.
And then we're going to set the content. To a specific width. Now as I mentioned in most web applications we want the width to be equal to the device width. In this case we're setting all of our measurements by absolute positioning as you see right on any of these images. So we want to set the width of our elements to width that encompasses the entire screen that we're trying to position these elements in. The one that works with this project happens to be 1200.
So that's what I'll set it. And the other thing that I need is another meta tag. This one is going to have a name of apple-mobile. Web app capable and the content is going to be set to Yes. Now, what this will do is when we pull this up in a browser, it's going to allow us to export this as a desktop item. So I'm going to save this and switch back over. So if we pull this back up into our iPad with this new code that I've inserted in there, it will look just fine when it comes up in my browser.
And now if I rotate the device Notice that it adjusts to the size of the device. So that's what the view port tag is doing for us. The other tag, allows us to save this as an application on our desktops. If I click on this icon right here, and I tap on, at the home screen. It's going to allow us to say this is an application on our home screen. So, I'm going to click on this add button. So, it's going to show, somewhere on our desktop like this. And if I tap on this application, it's going to come up in the browser that doesn't have any of the navigation that the browser window would normally have.
So that's the advantage of using those 2 tags. Let's go back into our code and add the ability to touch the element on the screen to our code. So we're going to go back into the script. Let's go ahead and close out the index and I'm going to hide these other elements here. And what we want to do is track another event so it's going to go down here with all the other events. That I'm using. And this event is going to be called touchstart. And of course I'm going to execute a function called touchstart that I'll have to create up here.
So, I'll add another function, and this is going to be called touchstart. It's going to receive an event and the first thing I want to do here is prevent that the fall behavior on a mobile device to the fall behavior when you tap and move, will be to scroll the page. And you're probably seen that when I were showing you the page at the very beginning of this video So we want to prevent that, so we use prevent default. And the next thing we want to do is create some variables for our artwork, so which art, and just like we had before.
Except that now when I'm touching I'm not going to have any of the variables that I'm setting up in any of these other events, because these touch events are going to be different than any of these move events. So I'm going to need to keep track of my element in its own variable, so I'll set Which are to the target of the event. So in this case the target would be the thing that I touched on. And then I'm going to create another variable, this variable's going to be called touch, and it's going to be set to the event and then touches, and then array value of zero.
So the way that touches work on an iPad, whenever you touch the screen it's going to create a finger touch. That's going to be tracked in a different array number, so the first finger put on the iPad is going to be tracked at this event touches. And then the array value of zero if you tapped somewhere else in the screen, it would be tracked on a separate array number. So this is going to give us the touch event at position 0 which will be our first touch. So then just like in other places, I need to track the position of my move.
So I'm going to create another variable called move offsetX. And that is going to to be equal whichart, the variable I just created. And then offset left minus The touch position and pagex. And you might be noticing that we're using things that are maybe not compatible with some other platform like old version of Firefox. But we don't really care in this case because I'm targeting right now the iPad and it will understand everyone of these x and y positions and dimensions.
So in addition to move offset x, I need to track move offset y. And you gotta be careful here that you update this to top and also this to page y. Now just like before we need to reset the z position so it's going to execute this function right here so that every element that I tap. Is going to be at the very top of every one of the other images. And then after that, I am going to set the z index of the element that I tapped on (NOISE), to be 10.
Alright, so then the next thing I need to do is add an event listener for my moving or my touch moving. And down here, I was adding an event in three different calls, right? But when I'm doing touches, I contract the event inside this touch start. So, I'll do whichart. And then add an event listener. And I want to track this time touch move. And then execute a function literal inside here. So I'm going to open this up.
And then do the rest of my code here. So now the postition of my element is going to be equal to touch. That pagex that's the position of the touch in relationship to the page and plus the moveoffsetx and the same thing for the y position. So, as the element moves I'm going to track the position dynamically. So, this touch move is going to execute while I'm moving the finger on top of the iPad.
And this touch right here. Should just be touch not touche. PageY move offsetY and then I need to set the positioning attributes of the element that I'm moving. WhichArt.style.left equals Position X in this case, plus the word pixels or px, and then clean this up a little bit.
And then I'm going to do the same thing for the Y parameter here, so whichart style. Top when we do Y position, is going to be Y, and pixels as well. One more thing, I need to make sure that this passes the false parameter here, so that it bubbles properly, and I'm going to save this. I've uploaded this application into an FTP server, so that I can look at it with my iPad. And If I click on one of these elements and just drag them over I can start making the snowman just fine. So our touch events are working properly and now we can get busy building our snowmen or snowladies.
So our approach to touches is definitely different than what we did when dragging. We were able to create a touch start event and instead of creating separate listener for the other events. We just added in EventListener for the touch start, and then inside touch start we added an event listener to touch move. Notice that we didn't really need to drag a touch drop or a drop event, because as soon as we're done moving something, the element is already positioned in the right place. So with the exception of having to keep track of the touches, using touch events is no different than any other event.
Now that your application is done, it's time to go ahead and build your own drag and drop games. If you're a dad like me, you're going to be a super popular dad whenever you finish one of these.
- Understanding event registration and propagation
- Analyzing event properties
- Creating and removing DOM elements with events
- Removing an event
- Canceling and preventing default events
- Playing media events
- Handling media pauses
- Dragging and dropping
- Adding touch events