{"id":15231,"date":"2015-06-05T05:01:59","date_gmt":"2015-06-04T19:01:59","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=15231"},"modified":"2015-06-04T21:51:40","modified_gmt":"2015-06-04T11:51:40","slug":"html-colour-coded-tic-tac-toe-drag-and-drop-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/html-colour-coded-tic-tac-toe-drag-and-drop-tutorial\/","title":{"rendered":"HTML Colour Coded Tic Tac Toe Drag and Drop Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/colour_coded_tictactoe_chalkboard.html\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"HTML\/Javascript Colour Coded Tic Tac Toe Game Drag and Drop Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/colour_coded_tictactoe_chalkboard.jpg\" title=\"HTML\/Javascript Colour Coded Tic Tac Toe Game Drag and Drop Tutorial\"  id='ihcct' onmouseover=\"  this.src=this.src.replace('.jpg','.xgif').replace('.gif','.xjpg').replace('.x','.');        \"  \/><\/a><p class=\"wp-caption-text\">HTML\/Javascript Colour Coded Tic Tac Toe Game Drag and Drop Tutorial<\/p><\/div>\n<p>Today we revisit the Javascript client drag and drop events for non-mobile platforms.  Do you remember when we presented <a target=_blank title='HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial' href='#hjctttgdadt'>HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial<\/a> as shown below, where we created a drag and drop game of Tic Tac Toe (or you can just click, if you like)?<\/p>\n<p>With our Tic Tac Toe game we present the grid on an HTML canvas element, and the player can drag a cross or a nought over to the canvas element to make a move (and then the computer has a go).  The variation today is that that first drag and drop decision is colour coded, in that the player&#8217;s move will not be accepted unless the border colour of their cross or nought matches the background colour of the square on the canvas&#8217;s grid, and this canvas grid colour is created by overlaying (or &#8220;draping&#8221;) an HTML div element over the top of the canvas element (as we did with <a target=_blank title='HTML5 Canvas Map Clickaround Overlay Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/html5-canvas-map-clickaround-overlay-tutorial\/'>HTML5 Canvas Map Clickaround Overlay Tutorial<\/a> a couple of days ago), which is given some opacity to allow this to happen.<\/p>\n<p>So let&#8217;s take a look at some HTML and Javascript code you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/colour_coded_tictactoe_chalkboard.html-GETME\" title='colour_coded_tictactoe_chalkboard.html'>colour_coded_tictactoe_chalkboard.html<\/a> (with differences in code in relation to the tutorial below as per <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/colour_coded_tictactoe_chalkboard.html-GETME\" title='colour_coded_tictactoe_chalkboard.html'>this link<\/a>), or perhaps try a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/colour_coded_tictactoe_chalkboard.html\" title='live run'>live run<\/a> (where you get <a target=_blank title='Idea from w3schools ... thanks' href='http:\/\/www.w3schools.com\/svg\/tryit.asp?filename=trysvg_polygon3'>rewarded<\/a> for good play).  Also notice that there is a brief period (5 seconds) at the start of a game where a drag and drop operation within the grid will transfer a colour into other squares, and you may need to drag and drop fom the right hand side into the grid to then start your game, but get the colour &#8220;coded&#8221;.  To understand the workings you need to examine the <a target=_blank title='CSS z-index property' href='http:\/\/www.w3schools.com\/cssref\/pr_pos_z-index.asp'>&#8220;z-index&#8221;<\/a> and <a target=_blank title='CSS opacity property' href='http:\/\/www.w3schools.com\/cssref\/css3_pr_opacity.asp'>&#8220;opacity&#8221;<\/a> (a common duo of &#8220;influence&#8221; in client work that overlays) of the HTML elements.  After that period click\/touch in the grid is your move, as per the usual functionality.<\/p>\n<hr>\n<p id='hjctttgdadt'>Previous relevant <a target=_blank title='HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/htmljavascript-canvas-tic-tac-toe-game-drag-and-drop-tutorial\/'>HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/tictactoe_chalkboard.html\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/TicTacToe_Chalkboard.jpg\" id=\"dadi\" onmouseover=\" var iqw=document.getElementById('dadi'); iqw.src = iqw.src.replace('.jpg','.PNG').replace('.png','.jpg').replace('.PNG','.png'); \" title=\"HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial\"  \/><\/a><p class=\"wp-caption-text\">HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop 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 show some image Drag and Drop functionality that you can use with the Canvas HTML5 element where we create a Tic Tac Toe game webpage.   We thank w3schools for the great advice at this <a target=_blank href='http:\/\/www.w3schools.com\/html\/html5_draganddrop.asp' title='Drag and drop advice at w3schools'>link<\/a>.<\/p>\n<p>The Drag and Drop does not work well on tablets, but for own Tic Tac Toe game today we allow either a drag and drop approach, or a touch\/click approach.<\/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\/HTMLCSS\/Canvas\/Game\/Chalkboard\/tictactoe_chalkboard.html_GETME' title='tictactoe_chalkboard.html'>tictactoe_chalkboard.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 tutorial as a <a target=_blank title='Canvas HTML element live run tutorial'  href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/tictactoe_chalkboard.html\">live run<\/a>.<\/p>\n<p>Should you find Tac Tac Toe (or even Noughts and Crosses) addictive you should see <a target=_blank title='Wargames' href='http:\/\/www.youtube.com\/watch?v=NHWjlCaIrQo'>this<\/a> &#8230; amazing!<\/p>\n<p>Yes &#8230; you&#8217;ve reached the end &#8230; <a target=_blank title='Click picture' href='http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/Canvas\/Game\/Chalkboard\/tictactoe_chalkboard.html'>go first<\/a> in each game and see if you can beat the computer!<\/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='#d8415' onclick='var dv=document.getElementById(\"d8415\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=canvas\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d8415' 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='#d15231' onclick='var dv=document.getElementById(\"d15231\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=javascript\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d15231' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we revisit the Javascript client drag and drop events for non-mobile platforms. Do you remember when we presented HTML\/Javascript Canvas Tic Tac Toe Game Drag and Drop Tutorial as shown below, where we created a drag and drop game &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/html-colour-coded-tic-tac-toe-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,224,281,364,400,401,576,578,652,876,894,997,1226,1319,1496],"class_list":["post-15231","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-games","category-gui","category-tutorials","tag-canvas","tag-colour","tag-css","tag-drag-and-drop","tag-event","tag-event-driven","tag-html","tag-html5","tag-javascript","tag-opacity","tag-overlay","tag-programming","tag-svg","tag-tutorial","tag-z-index"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/15231"}],"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=15231"}],"version-history":[{"count":10,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/15231\/revisions"}],"predecessor-version":[{"id":15241,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/15231\/revisions\/15241"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=15231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=15231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=15231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}