HTML Drag and Drop Image Events Tutorial

HTML Drag and Drop Image Events Tutorial

HTML Drag and Drop Image Events Tutorial

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?

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 live run to get what we mean.

And here is the HTML and Javascript code for your perusal that you could call draganddrop_leftandright.html


Previous relevant HTML Canvas Memories Game Drag and Drop Tutorial is shown below.

HTML Canvas Memories Game Drag and Drop Tutorial

HTML Canvas Memories Game Drag and Drop Tutorial

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?

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 memories.html and how it changed for the tutorial below is described at this link.

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.

HTML/Javascript Canvas Memories Card Game Primer Tutorial

HTML/Javascript Canvas Memories Card Game Primer Tutorial

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 memories.html

You’ll notice heavy use of the Javascript Math.random() function.

We hope you enjoy this Memories Card Game tutorial live run.

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 old live 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.

This entry was posted in eLearning, Event-Driven Programming, Games, Tutorials and tagged , , , , , , , , . Bookmark the permalink.

One Response to HTML Drag and Drop Image Events Tutorial

  1. Right away I am ready to do my breakfast,
    later than having my breakfast coming again to read more news.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>