Recently weโve been talking about the client side of web applications, and you could say weโve been concentrating on Javascript mouse events, and very recently, the drag and drop variety of mouse events. Today we revisit all that, but take out the HTML5 canvas element from the picture we were seeing a couple of days ago with HTML Canvas Memories Game Drag and Drop Tutorial as shown below, to see how drag and drop might work with some images on the left and right of the screen, and arranging it that the left hand images can be dragged and dropped onto the right hand images, with those images being playing cards โฆ you score by guessing card denomination matches โacross the divideโ.
So what events were relevant to the left hard (drag) side playing card image (HTML โimgโ elements) (in decreasing order of importance)?
- ondragstart=โdrag(event)โ โฆ where we can differentiate a drag and drop (of the first card chosen) from a click/touch
- onclick=โadrag(event)โ โฆ to allow for platforms that do not support drag and drop
- ondragover=โallowDrop(event)โ โฆ not called upon for non-mobile
And what additional property?
- draggable=โtrueโ
So what events were relevant to the right hard (drop) side playing card image (HTML โimgโ elements) (in decreasing order of importance)?
- onmouseover=โ if (!misdown && pickedcard != โ) { omo(event); } โ โฆ allows for identification of the dropped onto card/image in a drag and drop event
- onmouseup=โomu(event)โ โฆ allows for identification of the dropped onto card/image in a drag and drop event
- onclick=โomu(event)โ โฆ to allow for platforms that do not support drag and drop to allow for identification of the right hand image
- ondrop=โdrop(event)โ โฆ not used for non-mobile
One other event that would be interesting for the left hand images could be the onmousemove event. Perhaps a hint for โfreezing, cold, warm, hot, boilingโ could be added?! As it is we briefly reveal right hand cards, so after a shuffle before this happens, a correct guess moves your score on by โtenโ rather than by the โoneโ scored for correct guesses after that.
Think you may need to try a liverun to get what we mean.
And here is the HTML and Javascript code for your perusal that you could call draganddrop_leftandrighthtml
Previous relevant HTML Canvas Memories Game Drag and Drop Tutorial is shown below.
The Canvas HTML element tag combines with some drag and drop event Javascript logic today with our revised Memories Card Game we first developed some time back with HTML/Javascript Canvas Memories Card Game Primer Tutorial as shown below. In the same line of thinking was yesterdayโs HTML Colour Coded Tic Tac Toe Drag and Drop Tutorial.
An HTML5 canvas element can be used as the container to draw graphics on the fly usually via the use of Javascript functions for rendering and event management.
Today, with this topic, weโve focussed on non-mobile platforms with the new drag and drop possibilities, but the old methods, with touch/click, will still work for mobile platforms, and at a later date we may find room for โdrag and dropโ improvement with the mobile platforms as well โฆ in the fullness of time โฆ given the constraints of economic belt tightening โฆ over the course of the current forward estimates.
So what events, applied directly to the canvas element this time (incidentally, yesterday, this was not the case), were additionally of interest for non-mobile drag and drop functionality (in decreasing order of importance (where the already existant canvas โonclickโ event remains the most important))?
- ondragstart=โdrag(event)โ โฆ where we can differentiate a drag and drop (of the first card chosen) from a click/touch
- onmouseup=โomu(event)โ โฆ where we can get the (x,y) of the second card chosen
- onmousemove=โomo(event)โ โฆ not really important, but we can show it is a drag and drop up in the HTML h1 wording
- ondragover=โallowDrop(event)โ โฆ not called upon for non-mobile
- ondrop=โdrop(event)โ โฆ not used for non-mobile
And what additional property?
- draggable=โtrueโ
The end result on a non-mobile platform is that two cards separately clicked act very much like a drag and drop. Our drag and drop does not draw the card flopping onto the other, but shows a change in the HTML h1 wording.
Here is a link to some downloadable HTML programming code โฆ rename to memorieshtml and how it changed for the tutorial below is described at thislink.
And you may want to try a memories.htmllive run of this card game that tests your memory, and perseverance, at the very least.
Previous relevant HTML/Javascript Canvas Memories Card Game Primer Tutorial is shown below.
The Canvas HTML element tag can be used as the container to draw graphics on the fly usually via the use of Javascript functions for rendering and event management.
In todayโs tutorial we mainly use the drawImage function to create a webpage where you can play the card game called Memories.
You may want to read more at HTML Canvas Reference as a generic reference, or here, at the tutorial javascript โ How do I add a simple onClick event handler to a canvas element? โ Stack Overflow.
As you can imagine, this HTML canvas element, new to HTML5, can be very useful for some practical client-side web functionality.
Link to some downloadable HTML programming code โฆ rename to memorieshtml
Youโll notice heavy use of the Javascript Math.random() function.
We hope you enjoy this Memories Card Game tutorial liverun.
Yes โฆ youโve reached the end โฆ have a top supportive day full of happy memories!
Stop Press
As of the 5th June 2015 you may notice this game changed to add functionality, and that the live run above might support some drag and drop functionality on non-mobile platforms so you may want to compare this to an oldlive run for how it worked before this date.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
One Response to HTML Drag and Drop Image Events Tutorial