{"id":1572,"date":"2016-08-10T08:25:15","date_gmt":"2016-08-10T08:25:15","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=1572"},"modified":"2017-11-16T23:13:47","modified_gmt":"2017-11-16T23:13:47","slug":"javascript-lesson-32-creating-graphics-part-2","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/","title":{"rendered":"JavaScript Lesson 32: Creating Graphics Part 2"},"content":{"rendered":"<h3 style=\"text-align: center;\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-lesson-31-creating-graphics-using-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<\/strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\">&lt;Next Lesson&gt;<\/a><\/h3>\n<p>In the previous lesson, you have learned how to write JavaScript code to draw straight lines on a canvas. In this lesson, we shall learn more advanced skills in creating graphics using JavaScript.<\/p>\n<h3>32.1 Drawing a Rectangle with Solid Color<\/h3>\n<p>We can draw a rectangle and fill it with a certain color using the fillRect() method. The default color is black. The fillRect() method contains four parameters, as follows:<\/p>\n<pre style=\"font-size: 100%; width: 50%;\"><strong>fillRect(x,y,w,h)<\/strong>\r\n<\/pre>\n<p>x=\u00a0The x-coordinate of the upper-left corner of the rectangle<br \/>\ny= the y-coordinate of the upper-left corner of the rectangle<br \/>\nw=the width \u00a0of the rectangle,(in pixels)<br \/>\nh=The height of the rectangle(in pixels)<\/p>\n<p>The following script draws a rectangle with black color.<\/p>\n<pre style=\"font-size: 100%;\">\r\n&lt;canvas id=\"myCanvas\" width=\"300\" height=\"150\" style=\"border:1px solid #d3d3d3;\"&gt;\r\n&lt;\/canvas&gt;&lt;br&gt;&lt;br&gt;\r\n&lt;input type=\"button\" Value=\"Draw Rectangle\" Onclick=\"DrawRect()\"&gt;\r\n\r\n&lt;script&gt;\r\n\r\nfunction DrawRect()\r\n{\r\nvar c=document.getElementById(\"myCanvas\");\r\nvar ctx=c.getContext(\"2d\");\r\nctx.fillRect(20,20,150,100);\r\n}\r\n\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>The Output<br \/>\n<canvas id=\"myCanvas\" width=\"300\" height=\"150\" style=\"border:1px solid #d3d3d3;\"><br \/>\n<\/canvas><\/p>\n<p><input type=\"button\" Value=\"Draw Rectangle\" Onclick=\"DrawRect()\"><\/p>\n<p><script type=\"text\/javascript\">\nfunction DrawRect() { var c=document.getElementById(\"myCanvas\"); var ctx=c.getContext(\"2d\"); ;ctx.fillRect(20,20,150,100); } \/\/ ]]><\/script><br \/>\nTo fill the rectangle with a different color, use the fillStyle property, as follows:<\/p>\n<pre style=\"font-size: 100%; width: 50%;\">ctx.fillStyle =color\r\n<\/pre>\n<p>The following script will fill the rectangle with blue color.<\/p>\n<pre style=\"font-size: 100%;\">&lt;script&gt;\r\n\r\nfunction DrawRect1()\r\n{\r\nvar c=document.getElementById(\"myCanvas\");\r\nvar ctx=c.getContext(\"2d\");\r\n\r\nctx.fillStyle =\"blue\"\r\nctx.fillRect(20,20,150,100);\r\n}\r\n\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>The Output<br \/>\n<canvas id=\"myCanvas1\" style=\"border: 1px solid #d3d3d3;\" width=\"300\" height=\"150\"><br \/>\n<\/canvas><\/p>\n<p><input type=\"button\" value=\"Draw Rectangle\" Onclick=\"DrawRect1()\"><\/p>\n<p><script type=\"text\/javascript\">\nfunction DrawRect1() { var c=document.getElementById(\"myCanvas1\"); var ctx=c.getContext(\"2d\"); ctx.fillStyle = \"blue\";ctx.fillRect(20,20,150,100); } \/\/ ]]><\/script><\/p>\n<h3>32.2 Drawing a Rectangle with Color in Gradient<\/h3>\n<p>We use the createLinearGradient() method creates a linear gradient style to fill an object such as a rectangle. It can also be used for other objects such as circles, lines, and text.<\/p>\n<p>The createLinearGradient() method comprises four parameters:<\/p>\n<pre style=\"font-size: 100%;\"><strong>createLinearGradient(x1,y1,x2,y2)<\/strong>\r\n<\/pre>\n<p>x1=the x-coordinate of the start point of the gradient<br \/>\ny1=the y-coordinate of the start point of the gradient<br \/>\nx2=the x-coordinate of the end point of the gradient<br \/>\ny2=the y-coordinate of the end point of the gradient<\/p>\n<p>Further, we need to use addColorStop() method to specify different colors, and where to position the colors in the gradient object.<\/p>\n<p>The following function draws a rectangle and creates a gradient comprises the blue and cyan color.<\/p>\n<pre style=\"font-size: 100%;\">function DrawRect()\r\n{\r\nvar c=document.getElementById(\"myCanvas\");\r\nvar ctx=c.getContext(\"2d\");\r\nvar my_gradient=ctx.createLinearGradient(0,0,0,170);\r\nmy_gradient.addColorStop(0,\"blue\");\r\nmy_gradient.addColorStop(1,\"cyan\");\r\nctx.fillStyle=my_gradient;\r\nctx.fillRect(20,20,150,100);\r\n}\r\n<\/pre>\n<p>The Output<br \/>\n<canvas id=\"myCanvas3\" width=\"300\" height=\"150\" style=\"border:1px solid #d3d3d3;\"><br \/>\n<\/canvas><br \/>\n<input type=\"button\" Value=\"Draw Rectangle\" Onclick=\"DrawRect3()\"><\/p>\n<p><script type=\"text\/javascript\">\nfunction DrawRect3()\n{\nvar c=document.getElementById(\"myCanvas3\");\nvar ctx=c.getContext(\"2d\");\nvar my_gradient=ctx.createLinearGradient(0,0,0,170);\nmy_gradient.addColorStop(0,\"blue\");\nmy_gradient.addColorStop(1,\"cyan\");\nctx.fillStyle=my_gradient;\nctx.fillRect(20,20,150,100);\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\/javascript-lesson-31-creating-graphics-using-javascript\/\">&lt;Previous Lesson&gt;<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Table of Contents]<\/a>\u00a0<\/strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/creating-objects-javascript\/\">&lt;Next Lesson&gt;<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Previous Lesson&gt;\u00a0[Table of Contents]\u00a0&lt;Next Lesson&gt; In the previous lesson, you have learned how to write JavaScript code to draw straight lines on a canvas. In this lesson, we shall learn more advanced skills in creating graphics using JavaScript. 32.1 Drawing a Rectangle with Solid Color We can draw a rectangle and fill it with a &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 32: Creating Graphics Part 2&#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-1572","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 32: Creating Graphics Part 2 - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"description\" content=\"This article describe how to draw graphics using javascript and fill it with colors\" \/>\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-32-creating-graphics-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 32: Creating Graphics Part 2 - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"This article describe how to draw graphics using javascript and fill it with colors\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T23:13:47+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-32-creating-graphics-part-2\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\",\"name\":\"JavaScript Lesson 32: Creating Graphics Part 2 - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2016-08-10T08:25:15+00:00\",\"dateModified\":\"2017-11-16T23:13:47+00:00\",\"description\":\"This article describe how to draw graphics using javascript and fill it with colors\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 32: Creating Graphics Part 2\"}]},{\"@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 32: Creating Graphics Part 2 - Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"This article describe how to draw graphics using javascript and fill it with colors","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-32-creating-graphics-part-2\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 32: Creating Graphics Part 2 - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"This article describe how to draw graphics using javascript and fill it with colors","og_url":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T23:13:47+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-32-creating-graphics-part-2\/","url":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/","name":"JavaScript Lesson 32: Creating Graphics Part 2 - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2016-08-10T08:25:15+00:00","dateModified":"2017-11-16T23:13:47+00:00","description":"This article describe how to draw graphics using javascript and fill it with colors","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/javascript-lesson-32-creating-graphics-part-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 32: Creating Graphics Part 2"}]},{"@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\/1572","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=1572"}],"version-history":[{"count":39,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1572\/revisions"}],"predecessor-version":[{"id":2510,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/1572\/revisions\/2510"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=1572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}