{"id":1186,"date":"2017-12-29T12:43:05","date_gmt":"2017-12-29T17:43:05","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1186"},"modified":"2018-02-17T18:21:43","modified_gmt":"2018-02-17T23:21:43","slug":"javascript-intro-web-game-development-part-1-canvas-element","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/12\/29\/javascript-intro-web-game-development-part-1-canvas-element\/","title":{"rendered":"JavaScript: Intro to Web Game Development &#8211; Part 1: canvas element"},"content":{"rendered":"<p>For this series, I am going to detail how to use JavaScript to create a web based browser game by going step by step through all of the concepts involved in creating the game of Pong. I will be using parts of the <a href=\"https:\/\/www.udemy.com\/code-your-first-game\/learn\/v4\/overview\">tutorial<\/a> written by Chris DeLeon, but greatly expanding on and customizing his code example.<\/p>\n<p>The first concept to introduce when it comes to making JavaScript browser games is the use of the HTML5 canvas object. A canvas is basically a rectangular box that you can insert into any HTML document which JavaScript will use to draw on.<\/p>\n<p>Here is what w3shcools.com says about the canvas element:<\/p>\n<blockquote><p>The HTML &lt;canvas&gt; element is used to draw graphics, on the fly, via JavaScript.<\/p>\n<p>The &lt;canvas&gt; element is only a container for graphics. You must use JavaScript to actually draw the graphics.<\/p>\n<p>Canvas has several methods for drawing paths, boxes, circles, text, and adding images.<\/p><\/blockquote>\n<p>Since this game will live in a web page, lets start by setting up an empty HTML document that will serve as a template.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;html&gt;\r\n&lt;head&gt;\r\n  &lt;meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\nPong 1.0&lt;br&gt;\r\n\r\n&lt;!-- Create an HTML canvas and assign it an id. --&gt;\r\n&lt;canvas id=\"gameCanvas\" width=\"750\" height=\"550\" style=\"border:1px\u00a0solid\u00a0\r\n#000000;\"&gt;&lt;\/canvas&gt;\r\n\r\n&lt;script&gt;\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>For this project, I gave the canvas an id name of &#8220;gameCanvas&#8221; and a size 750 pixels wide by 550 pixels high. Nothing is visible on the page yet except the word &#8220;Pong 1.0&#8221;, and a 1 pixel border around the canvas, which I added just to make the canvas visible at this point. The HTML template also has <code class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;script&gt;&lt;\/script&gt;<\/code> tags where the JavaScript code will live.<\/p>\n<p>Here is what the canvas looks like so far:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1228 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/12\/canvas_blank.png\" alt=\"\" width=\"407\" height=\"318\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/12\/canvas_blank.png 407w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2017\/12\/canvas_blank-300x234.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/p>\n<p>For more information about the HTML canvas element, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/html\/html5_canvas.asp\">https:\/\/www.w3schools.com\/html\/html5_canvas.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this series, I am going to detail how to use JavaScript to create a web based browser game by going step by step through all of the concepts involved in creating the game of Pong. I will be using parts of the tutorial written by Chris DeLeon, but greatly expanding on and customizing his &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/12\/29\/javascript-intro-web-game-development-part-1-canvas-element\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: Intro to Web Game Development &#8211; Part 1: canvas element<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[82,81,45],"class_list":["post-1186","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-canvas","tag-html5","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1186"}],"version-history":[{"count":14,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1186\/revisions"}],"predecessor-version":[{"id":1514,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1186\/revisions\/1514"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}