{"id":15259,"date":"2015-06-06T05:01:22","date_gmt":"2015-06-05T19:01:22","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=15259"},"modified":"2016-02-08T21:19:31","modified_gmt":"2016-02-08T11:19:31","slug":"html-canvas-memories-game-drag-and-drop-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/html-canvas-memories-game-drag-and-drop-tutorial\/","title":{"rendered":"HTML Canvas Memories Game Drag and Drop Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"HTML Canvas Memories Game Drag and Drop Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories_draganddrop.jpg\" title=\"HTML Canvas Memories Game Drag and Drop Tutorial\"   id='qwihcct' onmouseover=\"  this.src=this.src.replace('.gif','.xjpeg').replace('.jpeg','.xjpg').replace('.jpg','.xpng').replace('.png','.xgif').replace('.x','.');        \"   \/><\/a><p class=\"wp-caption-text\">HTML Canvas Memories Game Drag and Drop Tutorial<\/p><\/div>\n<p>The <a target=_blank title='HTML5 canvas element information from w3schools' href='http:\/\/www.w3schools.com\/html\/html5_canvas.asp'>Canvas<\/a> HTML element tag combines with some <a target=_blank title='Drag and drop information from w3schools' href='http:\/\/www.w3schools.com\/html\/html5_draganddrop.asp'>drag and drop<\/a> event Javascript logic today with our revised Memories Card Game we first developed some time back with <a target=_blank title='HTML\/Javascript Canvas Memories Card Game Primer Tutorial' href='#hjcmcgpt'>HTML\/Javascript Canvas Memories Card Game Primer Tutorial<\/a> as shown below.  In the same line of thinking was yesterday&#8217;s <a target=_blank href='https:\/\/www.rjmprogramming.com.au\/ITblog\/html-colour-coded-tic-tac-toe-drag-and-drop-tutorial\/'>HTML Colour Coded Tic Tac Toe Drag and Drop Tutorial<\/a>.<\/p>\n<p>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.<\/p>\n<p>Today, with this topic, we&#8217;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 &#8220;drag and drop&#8221; improvement with the mobile platforms as well &#8230; in the fullness of time &#8230; given the constraints of economic belt tightening &#8230; over the course of the current forward estimates.<\/p>\n<p>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 &#8220;onclick&#8221; event remains the most important))?<\/p>\n<ul>\n<li><a target=_blank title='Event ondragstart information from w3schools' href='http:\/\/www.w3schools.com\/tags\/ev_ondragstart.asp'>ondragstart<\/a>=&#8221;drag(event)&#8221; &#8230; where we can differentiate a drag and drop (of the first card chosen) from a click\/touch<\/li>\n<li><a target=_blank title='Event onmouseup information from w3schools' href='http:\/\/www.w3schools.com\/tags\/ev_onmouseup.asp'>onmouseup<\/a>=&#8221;omu(event)&#8221; &#8230; where we can get the (x,y) of the second card chosen<\/li>\n<li><a target=_blank title='Event onmousemove information from w3schools' href='http:\/\/www.w3schools.com\/tags\/ev_onmousemove.asp'>onmousemove<\/a>=&#8221;omo(event)&#8221; &#8230; not really important, but we can show it is a drag and drop up in the HTML h1 wording<\/li>\n<li><a target=_blank title='Event ondragover information from w3schools' href='http:\/\/www.w3schools.com\/tags\/ev_ondragover.asp'>ondragover<\/a>=&#8221;allowDrop(event)&#8221; &#8230; not called upon for non-mobile<\/li>\n<li><a target=_blank title='Event ondrop information from w3schools' href='http:\/\/www.w3schools.com\/tags\/ev_ondrop.asp'>ondrop<\/a>=&#8221;drop(event)&#8221; &#8230; not used for non-mobile<\/li>\n<\/ul>\n<p>And what additional property?<\/p>\n<ul>\n<li><a target=_blank title='Property draggable information from w3schools' href='http:\/\/www.w3schools.com\/tags\/att_global_draggable.asp'>draggable<\/a>=&#8221;true&#8221;<\/li>\n<\/ul>\n<p>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.<\/p>\n<p>Here is a link to some downloadable HTML programming code &#8230; rename to <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html-GETME' title='memories.html'>memories.html<\/a> and how it changed for the tutorial below is described at <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html-GETME' title='memories.html'>this link<\/a>.<\/p>\n<p>And you may want to try a <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html' title='memories.html'>memories.html<\/a>live run<\/a> of this card game that tests your memory, and perseverance, at the very least.<\/p>\n<hr>\n<p id='hjcmcgpt'>Previous relevant <a target=_blank title='HTML\/Javascript Canvas Memories Card Game Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/htmljavascript-canvas-memories-card-game-primer-tutorial\/'>HTML\/Javascript Canvas Memories Card Game Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"HTML\/Javascript Canvas Memories Card Game Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/Memories.jpg\" title=\"HTML\/Javascript Canvas Memories Card Game Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">HTML\/Javascript Canvas Memories Card Game Primer Tutorial<\/p><\/div>\n<p>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.<\/p>\n<p>In today&#8217;s tutorial we mainly use the <a target=_blank href='http:\/\/www.w3schools.com\/tags\/canvas_drawimage.asp' title='HTML5 Canvas drawImage() method information from w3schools ... thanks'>drawImage<\/a> function to create a webpage where you can play the card game called Memories.<\/p>\n<p>You may want to read more at <a target=_blank title='HTML Canvas Reference' href='http:\/\/www.w3schools.com\/tags\/ref_canvas.asp'>HTML Canvas Reference<\/a> as a generic reference, or here, at the tutorial <a target=_blank title='javascript - How do I add a simple onClick event handler to a canvas element? - Stack Overflow' href='http:\/\/stackoverflow.com\/questions\/9880279\/how-do-i-add-a-simple-onclick-event-handler-to-a-canvas-element'>javascript &#8211; How do I add a simple onClick event handler to a canvas element? &#8211; Stack Overflow<\/a>.<\/p>\n<p>As you can imagine, this HTML canvas element, new to HTML5, can be very useful for some practical client-side web functionality.<\/p>\n<p>Link to some downloadable HTML programming code &#8230; rename to <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html_GETME' title='memories.html'>memories.html<\/a>\n<\/p>\n<p>You&#8217;ll notice heavy use of the Javascript <a target=_blank title='Javascript Math.random() information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/jsref_random.asp'>Math.random()<\/a> function.<\/p>\n<p>We hope you enjoy this Memories Card Game tutorial <a target=_blank title='Canvas HTML element live run tutorial'  href=\"http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/memories.html\">live run<\/a>.<\/p>\n<p>Yes &#8230; you&#8217;ve reached the end &#8230; have a top supportive day full of happy memories!<\/p>\n<p><b>Stop Press<\/b><\/p>\n<p>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 <a target=_blank title='Canvas HTML element live run tutorial'  href=\"http:\/\/www.rjmprogramming.com.au\/Games\/Memories\/index.html\">old live run<\/a> for how it worked before this date.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d8104' onclick='var dv=document.getElementById(\"d8104\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=games-2\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d8104' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d15259' onclick='var dv=document.getElementById(\"d15259\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=drag-and-drop\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d15259' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/html-canvas-memories-game-drag-and-drop-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,15,17,37],"tags":[184,281,364,400,401,576,578,652,997,1319],"class_list":["post-15259","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-games","category-gui","category-tutorials","tag-canvas","tag-css","tag-drag-and-drop","tag-event","tag-event-driven","tag-html","tag-html5","tag-javascript","tag-programming","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/15259"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=15259"}],"version-history":[{"count":8,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/15259\/revisions"}],"predecessor-version":[{"id":20069,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/15259\/revisions\/20069"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=15259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=15259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=15259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}