{"id":1533,"date":"2016-07-25T08:14:54","date_gmt":"2016-07-25T08:14:54","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=1533"},"modified":"2017-11-16T23:12:59","modified_gmt":"2017-11-16T23:12:59","slug":"javascript-lesson-31-creating-graphics-using-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/","title":{"rendered":"JavaScript Lesson 31: Creating Graphics  Part 1"},"content":{"rendered":"<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-30-creating-rollover-effects-in-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\">&lt;Next Lesson&gt;<\/a><\/strong><\/h3>\n<p>We can use JavaScript to draw graphics on an HTML document. In this lesson, we shall learn how to draw straight lines, triangles, and rectangles with outlines only.<\/p>\n<p>Before we\u00a0can draw graphics on an HTML document, first\u00a0we need to create a canvas using the &lt;canvas&gt; element. However, we cannot draw any graphics with the Canvas element alone. In order to draw on the canvas, we need to use JavaScript.<\/p>\n<p>To create the canvas, use the following HTML statement<\/p>\n<p>&lt;canvas id=&#8221;myCanvas&#8221; width=&#8221;300&#8243; height=&#8221;150&#8243; style=&#8221;border:1px solid #d3d3d3; background-color:beige;&#8221;&gt;<\/p>\n<p>It creates a canvas with width=300 px and height=150 px. We specify the border style and color as well as background color using the style attribute.<\/p>\n<h3>31.1: Drawing a Straight Line<\/h3>\n<p>To draw a straight line, create the\u00a0DrawLine function as follows:<\/p>\n<pre style=\"font-size: 100%;\">function DrawLine()\r\n{\r\nvar c = document.getElementById(\"myCanvas\");\r\nvar cx = c.getContext(\"2d\");\r\ncx.beginPath();\r\ncx.moveTo(80, 20);\r\ncx.lineTo(120, 100);\r\ncx.stroke();\r\n}\r\n<\/pre>\n<p>The getElementById() method returns the canvas element that has the id\u00a0attribute with the the name myCanvas\u00a0.<\/p>\n<p>The getContext() method returns an object that provides methods and properties for drawing on the canvas.<\/p>\n<p>beginPath begin the drawing and MoveTo specify\u00a0the starting point while lineTo specifies the ending point. The Stroke method actually draws the line on the canvas.<\/p>\n<p>The full JavaScript code integrated with the HTML document is as follows:<\/p>\n<pre style=\"font-size: 100%;\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;canvas id=\"myCanvas\" width=\"300\" height=\"150\" style=\"border:1px solid #d3d3d3; background-color:beige;\"&gt;\r\n&lt;\/canvas&gt;&lt;br&gt;&lt;br&gt;\r\n&lt;input type=\"button\" Value=\"Draw Line\" Onclick=\"DrawLine()\"&gt;\r\n\r\n&lt;script&gt;\r\nfunction DrawLine()\r\n{\r\nvar c = document.getElementById(\"myCanvas\");\r\nvar ctx = c.getContext(\"2d\");\r\nctx.beginPath();\r\nctx.moveTo(80, 20);\r\nctx.lineTo(120, 100);\r\nctx.stroke();\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>The Output<\/strong><\/p>\n<p><canvas id=\"myCanvas\" style=\"border: 1px solid #d3d3d3; background-color: beige;\" width=\"300\" height=\"150\"><br \/>\n<\/canvas><\/p>\n<p><input type=\"button\" Value=\"Draw Line\" Onclick=\"DrawLine()\"><\/p>\n<p><script type=\"text\/javascript\">\nfunction DrawLine() { var c = document.getElementById(\"myCanvas\"); var ctx = c.getContext(\"2d\"); ctx.beginPath(); ctx.moveTo(80, 20); ctx.lineTo(120, 100); ctx.stroke(); }\n<\/script><\/p>\n<p>You can also specify the width of the line using the lineWidth attribute, as follows:<\/p>\n<p>ctx.lineWidth = 4;<\/p>\n<p>Now add this line to the previous JavaScript function.<br \/>\n<strong>The Output<\/strong><\/p>\n<p><canvas id=\"myCanvas1\" style=\"border: 1px solid #d3d3d3; background-color: beige;\" width=\"300\" height=\"150\"><br \/>\n<\/canvas><br \/>\n<input type=\"button\" Value=\"Draw Line\" Onclick=\"DrawLine2()\"><\/p>\n<p><script type=\"text\/javascript\">\nfunction DrawLine2() { var c = document.getElementById(\"myCanvas1\"); var ctx = c.getContext(\"2d\"); ctx.beginPath(); ctx.lineWidth = 4;ctx.moveTo(80, 20); ctx.lineTo(120, 100); ctx.stroke(); }\n<\/script><\/p>\n<h3>Drawing a Triangle<\/h3>\n<p>Based on the same principle, we can draw a triangle by joining three straight lines. The JavaScript is as follows:<\/p>\n<pre style=\"font-size: 100%;\">&lt;canvas id=\"myCanvas2\" style=\"border: 1px solid #d3d3d3; background-color: beige;\" width=\"300\" height=\"150\"&gt;\r\n&lt;\/canvas&gt;\r\n&lt;input type=\"button\" value=\"Draw Line\" onclick=\"DrawTri()\"\/&gt;\r\n&lt;script&gt;\r\n\r\nfunction DrawTri()\r\n{\r\nvar c = document.getElementById(\"myCanvas2\");\r\nvar ctx = c.getContext(\"2d\");\r\nctx.beginPath();\r\nctx.moveTo(80, 20);\r\nctx.lineTo(80, 100);\r\nctx.lineTo(130, 100);\r\nctx.lineTo(80, 20);\r\nctx.stroke();\r\n}\r\n&lt;\/script&gt;\r\n<script>\r\nfunction DrawTri()\r\n{\r\nvar c = document.getElementById(\"myCanvas2\");\r\nvar ctx = c.getContext(\"2d\");\r\nctx.beginPath();\r\nctx.moveTo(80, 20);\r\nctx.lineTo(80, 100);\r\nctx.lineTo(130, 100);\r\nctx.lineTo(80, 20);\r\nctx.stroke();\r\n}\r\n<\/script>\r\n<\/pre>\n<p><canvas id=\"myCanvas2\" style=\"border: 1px solid #d3d3d3; background-color: beige;\" width=\"300\" height=\"150\"><br \/>\n<\/canvas><br \/>\n<input type=\"button\" value=\"Draw Triangle\"  Onclick=\"DrawTri()\"><br \/>\n<script>\nfunction DrawTri()\n{\nvar c = document.getElementById(\"myCanvas2\");\nvar ctx = c.getContext(\"2d\");\nctx.beginPath();\nctx.moveTo(80, 20);\nctx.lineTo(80, 100);\nctx.lineTo(130, 100);\nctx.lineTo(80, 20);\nctx.stroke();\n}\n<\/script><br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- Javascript Responsive Ads --><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"9005101105\"\n     data-ad-format=\"auto\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-30-creating-rollover-effects-in-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\">&lt;Next Lesson&gt;<\/a><\/strong><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0&lt;Next Lesson&gt; We can use JavaScript to draw graphics on an HTML document. In this lesson, we shall learn how to draw straight lines, triangles, and rectangles with outlines only. Before we\u00a0can draw graphics on an HTML document, first\u00a0we need to create a canvas using the &lt;canvas&gt; element. However, we cannot draw &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 31: Creating Graphics  Part 1&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1533","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 31: Creating Graphics Part 1 - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"description\" content=\"Learn how to create graphics in JavaScript\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 31: Creating Graphics Part 1 - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"Learn how to create graphics in JavaScript\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T23:12:59+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\",\"name\":\"JavaScript Lesson 31: Creating Graphics Part 1 - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2016-07-25T08:14:54+00:00\",\"dateModified\":\"2017-11-16T23:12:59+00:00\",\"description\":\"Learn how to create graphics in JavaScript\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 31: Creating Graphics Part 1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 31: Creating Graphics Part 1 - Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Learn how to create graphics in JavaScript","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 31: Creating Graphics Part 1 - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"Learn how to create graphics in JavaScript","og_url":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T23:12:59+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/","url":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/","name":"JavaScript Lesson 31: Creating Graphics Part 1 - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2016-07-25T08:14:54+00:00","dateModified":"2017-11-16T23:12:59+00:00","description":"Learn how to create graphics in JavaScript","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 31: Creating Graphics Part 1"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=1533"}],"version-history":[{"count":44,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1533\/revisions"}],"predecessor-version":[{"id":2509,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1533\/revisions\/2509"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}