{"id":8633,"date":"2021-01-02T21:04:03","date_gmt":"2021-01-02T21:04:03","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8633"},"modified":"2021-01-02T21:05:55","modified_gmt":"2021-01-02T21:05:55","slug":"html-canvas","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/html-canvas\/","title":{"rendered":"HTML Canvas"},"content":{"rendered":"<p>HTML Canvas allows us to draw graphics (using JavaScript). We can create a canvas element and use a WebGL API to draw lines, fill rectangles etc.<\/p>\n<p>Let&#8217;s demonstrate some of the canvas and drawing functionality by creating a simple Tic-Tac-Toe UI. Start of by creating a HTML file and within that we add a canvas element, for example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;canvas id=&quot;tic-tac-toe-board&quot;&gt;&lt;\/canvas&gt;\r\n<\/pre>\n<p>Now in our JavaScript (or in my case I&#8217;m going to use TypeScript) we can get the element using the id and then start interacting with the canvas using WebGL, for for example here&#8217;s a &#8220;main&#8221; method which we&#8217;d run in the HTML body&#8217;s onload event. The first thing we need to do is get the canvas element using <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst board = &lt;HTMLCanvasElement&gt; document.getElementById('tic-tac-toe-board');\r\n<\/pre>\n<p>Next up we need to get the element&#8217;s context using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst ctx = board.getContext('2d');  \r\nif(ctx != null) {\r\n}\r\n<\/pre>\n<p>Once we have the context we can use methods, such as fillRect, moveTo, lineTo etc. as well as set properties on the context, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst ctx = board.getContext('2d');  \r\nif(ctx != null) {\r\n  ctx.fillStyle = 'white';\r\n  ctx.fillRect(0, 0, board.clientWidth, board.clientHeight);\r\n}\r\n<\/pre>\n<p>Below is an example of an implementation of code to draw a Tic-Tac-Toe board and handle click events.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfunction main() {\r\n  const board = &lt;HTMLCanvasElement&gt; document.getElementById('tic-tac-toe-board');\r\n\r\n  const size = 500;\r\n  const lineColour = &quot;#ddd&quot;;\r\n  const lineStart = 4;\r\n  const lineLength = size - 5;\r\n  const sectionSize = size \/ 3;\r\n\r\n  board.width = size;\r\n  board.height = size;\r\n\r\n  const elemLeft = board.offsetLeft + board.clientLeft;\r\n  const elemTop = board.offsetTop + board.clientTop;\r\n  const elemWidth = board.clientWidth;\r\n  const elemHeight = board.clientHeight;\r\n\r\n  \/\/ example of handling click event\r\n  board.addEventListener('click', ev =&gt; {\r\n     const x = ev.pageX - elemLeft;\r\n     const y = ev.pageY - elemTop;\r\n\r\n     console.log(`X: ${x}, Y: ${y}`);\r\n  });\r\n\r\n  const ctx = board.getContext('2d');  \r\n  if(ctx != null) {\r\n    ctx.fillStyle = 'white';\r\n    ctx.fillRect(0, 0, elemWidth, elemHeight);\r\n\r\n    ctx.lineWidth = 10;\r\n    ctx.lineCap = 'round';\r\n    ctx.strokeStyle = lineColour;\r\n    ctx.beginPath();\r\n\r\n    for (let y = 1; y &lt;= 2; y++) {  \r\n      ctx.moveTo(lineStart, y * sectionSize);\r\n      ctx.lineTo(lineLength, y * sectionSize);\r\n    }\r\n      \r\n    for (let x = 1; x &lt;= 2; x++) {\r\n      ctx.moveTo(x * sectionSize, lineStart);\r\n      ctx.lineTo(x * sectionSize, lineLength);\r\n    }\r\n      \r\n    ctx.stroke();\r\n  }\r\n}\r\n<\/pre>\n<p>Source code for this post is available on <a href=\"https:\/\/github.com\/putridparrot\/blog-projects\/tree\/master\/HtmlCanvas\" rel=\"noopener\" target=\"_blank\">github<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>HTML Canvas allows us to draw graphics (using JavaScript). We can create a canvas element and use a WebGL API to draw lines, fill rectangles etc. Let&#8217;s demonstrate some of the canvas and drawing functionality by creating a simple Tic-Tac-Toe UI. Start of by creating a HTML file and within that we add a canvas [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45,46],"tags":[],"class_list":["post-8633","post","type-post","status-publish","format-standard","hentry","category-javascript","category-typescript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8633"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8633\/revisions"}],"predecessor-version":[{"id":8636,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8633\/revisions\/8636"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}