{"id":9275,"date":"2022-10-06T04:28:23","date_gmt":"2022-10-06T04:28:23","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=9275"},"modified":"2022-10-06T04:28:23","modified_gmt":"2022-10-06T04:28:23","slug":"canvas-intro","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/10\/06\/canvas-intro\/","title":{"rendered":"Canvas Intro"},"content":{"rendered":"\n<p>The HTML &lt;canvas> element is used for drawing graphics via scripting. It is only a container for graphics. You need\u00a0JavaScript\u00a0to draw the graphics.<\/p>\n\n\n\n<p>Canvas has multiple methods for drawing paths, circles, boxes, text, as well as for adding images.<\/p>\n\n\n\n<p>Canvas can be used to draw colorful texts, with or without animation. Besides, they can be animated. This means that canvas objects can move.<\/p>\n\n\n\n<p>More than one &lt;canvas&gt; elements can be on one HTML page.<\/p>\n\n\n\n<p>Canvas is a rectangular area, and by default, it has no border or content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;canvas id=\"canvas\" width=\"250\" height=\"150\"&gt;&lt;\/canvas&gt;<\/code><\/pre>\n\n\n\n<p>Always use an&nbsp;id&nbsp;attribute, and a&nbsp;width&nbsp;and&nbsp;height&nbsp;attribute to define the size of the canvas. Use the&nbsp;style&nbsp;attribute to add a border.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-6\">Example of the HTML &lt;canvas&gt; tag:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;canvas id=\"canvas\" width=\"250\" height=\"150\" style=\"border:1px solid #1c87c9;\"&gt;\n      The HTML5 canvas tag is not supported by your browser..\n    &lt;\/canvas&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-to-draw-a-circle-8\">Example of the HTML &lt;canvas&gt; tag to draw a circle:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;canvas id=\"exampleCanvas\" width=\"200\" height=\"200\" style=\"border:1px solid #dddddd;\"&gt;\n      HTML5 canvas tag is not supported by your browser..\n    &lt;\/canvas&gt;\n    &lt;script&gt;\n      var c = document.getElementById(\"exampleCanvas\");\n      var ctx = c.getContext(\"2d\");\n      ctx.beginPath();\n      ctx.arc(100, 100, 60, 0, 2 * Math.PI);\n      ctx.strokeStyle = '#009299';\n      ctx.stroke();\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-to-draw-a-text-10\">Example of the HTML &lt;canvas&gt; tag to draw a text:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;canvas id=\"exampleCanvas\" width=\"350\" height=\"110\" style=\"border:1px solid #dddddd;\"&gt;\n      HTML5 canvas tag is not supported by your browser.\n    &lt;\/canvas&gt;\n    &lt;script&gt;\n      var c = document.getElementById(\"exampleCanvas\");\n      var ctx = c.getContext(\"2d\");\n      ctx.font = \"40px Arial\";\n      ctx.fillStyle = '#262ac7';\n      ctx.fillText(\"Canvas Text\", 55, 65);\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-to-draw-a-linear-gradient-12\">Example of the HTML &lt;canvas&gt; tag to draw a linear gradient:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;canvas id=\"exampleCanvas\" width=\"300\" height=\"140\" style=\"border:1px solid #dddddd;\"&gt;\n      The HTML5 canvas tag is not supported by your browser.\n    &lt;\/canvas&gt;\n    &lt;script&gt;\n      var c = document.getElementById(\"exampleCanvas\");\n      var ctx = c.getContext(\"2d\");\n      var grd = ctx.createLinearGradient(0, 0, 300, 0);\n      grd.addColorStop(0, \"#359900\");\n      grd.addColorStop(1, \"#ffffff\");\n      ctx.fillStyle = grd;\n      ctx.fillRect(20, 20, 250, 100);\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-to-draw-a-line-14\">Example of the HTML &lt;canvas&gt; tag to draw a line:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;canvas id=\"exampleCanvas\" width=\"150\" height=\"150\" style=\"border:1px solid #cccccc;\"&gt;\n      The HTML5 canvas tag is not supported by your browser.\n    &lt;\/canvas&gt;\n    &lt;script&gt;\n      var c = document.getElementById(\"exampleCanvas\");\n      var ctx = c.getContext(\"2d\");\n      ctx.moveTo(0, 0);\n      ctx.lineTo(150, 150);\n      ctx.strokeStyle = '#86417d';\n      ctx.stroke();\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-to-draw-an-image-16\">Example of the HTML &lt;canvas&gt; tag to draw an image:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;div&gt;\n      &lt;h2&gt;Original image&lt;\/h2&gt;\n      &lt;img id=\"flower\" src=\"https:\/\/images.unsplash.com\/photo-1485431142439-206ba3a9383e?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=500&amp;q=60\" width=\"500\" height=\"379\"&gt;\n    &lt;\/div&gt;\n    &lt;h2&gt;Draw an image with canvas&lt;\/h2&gt;\n    &lt;canvas id=\"exampleCanvas\"&gt;&lt;\/canvas&gt;\n    &lt;script&gt;\n      const canvas = document.getElementById('exampleCanvas');\n      const ctx = canvas.getContext('2d');\n      const image = document.getElementById('flower');\n      image.addEventListener('load', e =&gt; {\n        ctx.drawImage(image, 80, 80, 350, 200, 10, 10, 150, 100);\n      });\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-the-html-lt-canvas-gt-tag-to-draw-a-circular-gradient-18\">Example of the HTML &lt;canvas&gt; tag to draw a circular gradient:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Title of the document&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;canvas id=\"exampleCanvas\" width=\"260\" height=\"160\" style=\"border:1px solid #cdcdcd;\"&gt;\n      The HTML5 canvas tag is not supported by your browser.\n    &lt;\/canvas&gt;\n    &lt;script&gt;\n      var c = document.getElementById(\"exampleCanvas\");\n      var ctx = c.getContext(\"2d\");\n      var grd = ctx.createRadialGradient(150, 75, 10, 115, 90, 150);\n      grd.addColorStop(0, \"purple\");\n      grd.addColorStop(1, \"white\");\n      ctx.fillStyle = grd;\n      ctx.fillRect(20, 20, 220, 120);\n    &lt;\/script&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The HTML &lt;canvas> element is used for drawing graphics via scripting. It is only a container for graphics. You need\u00a0JavaScript\u00a0to draw the graphics. Canvas has multiple methods for drawing paths, circles, boxes, text, as well as for adding images. Canvas can be used to draw colorful texts, with or without animation. Besides, they can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[546],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/9275"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=9275"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/9275\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=9275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=9275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=9275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}