{"id":31155,"date":"2016-01-09T16:50:26","date_gmt":"2016-01-09T11:20:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=31155"},"modified":"2016-02-08T13:09:41","modified_gmt":"2016-02-08T07:39:41","slug":"tutorial-to-create-a-circular-progress-bar-with-canvas","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/tutorial-to-create-a-circular-progress-bar-with-canvas\/","title":{"rendered":"Tutorial to create a circular progress bar with Canvas"},"content":{"rendered":"<p>Canvas is a very powerful tool for drawing graphics using scripting but what it need a basic understanding of HTML and JavaScript.Basic description of Canvas is here:<\/p>\n<blockquote><p>The HTML5 Canvas element is an HTML tag like &lt;div&gt;, &lt;a&gt;,<br \/>\nor &lt;table&gt; tag, with the exception that its contents are rendered with JavaScript.<\/p><\/blockquote>\n<p>So, what I am sharing with you is a simple tutorial to create a circular progress bar with Canvas. As we all know that HTML5 has &lt;progress&gt; element to create a progress bar but to create a more interactive progress bar I recommend to use Canvas.<\/p>\n<p>After reading some basic but useful information let&#8217;s start with our Progress bar tutorial:<\/p>\n<p>Here&#8217;s what we get after creating it:-<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-31158\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/01\/progress_circle.png\" alt=\"progress_circle\" width=\"250\" height=\"160\" \/><\/p>\n<p>Our Coding is divided into three parts:-<\/p>\n<ul>\n<li>Variable Declaration<\/li>\n<li>Creating a simple circle on Page.<\/li>\n<li>Creating progress circle on that circle.<\/li>\n<\/ul>\n<p>Here is the HTML snippet of Progress bar:-<br \/>\nCreate a canvas element in your page like this:-<\/p>\n<p>[html]<\/p>\n<p>&lt;canvas id=&quot;myCanvas&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;\/canvas&gt;<br \/>\n[\/html]<\/p>\n<p>And after it let&#8217;s start with our Script tag:-<\/p>\n<p>First Part starts Here:-<\/p>\n<p>[html]<\/p>\n<p>var canvas = document.getElementById(&#8216;myCanvas&#8217;); \/\/ to get the element<br \/>\nvar context = canvas.getContext(&#8216;2d&#8217;); \/\/to return drawing context on canvas<br \/>\nvar al=0; \/\/ use it for Amount loaded<br \/>\nvar start=4.72; \/\/From where to start position of progress;<br \/>\nvar cw=context.canvas.width\/2; \/\/to get x cordinate;<br \/>\nvar ch=context.canvas.height\/2; \/\/ to get y coordinate;<\/p>\n<p>here width and height is divided by two so to get our progressbar in middle.<\/p>\n<p>var diff; \/\/to load progress bar Slowly<\/p>\n<p>[\/html]<\/p>\n<p>Second\u00a0Part starts Here:-<\/p>\n<p>Now comes our function which actually creating Progress Bar:-<\/p>\n<p>&nbsp;<\/p>\n<p>[html]<\/p>\n<p>function progressBar(){<br \/>\n \/\/calculated rate level to increase progress bar.<\/p>\n<p>diff=(al\/100)*Math.PI*2;<\/p>\n<p>\/\/ to clear the whole canvas rect<br \/>\n context.clearRect(0,0,400,200);<\/p>\n<p>\/\/ simple create a basic circle<\/p>\n<p>Second Part coding starts here:-<\/p>\n<p>context.beginPath();<\/p>\n<p>\u00a0[\/html]<\/p>\n<p>\/\/formula of creating a circle in canvas like cw indicating x coordinate, ch indicating y coordinate,50 indicating radius,<br \/>\n0 indicating starting angle,2*Math.PI indicating end angle<br \/>\nFalse indicating anticlockwise direction.<\/p>\n<p>&nbsp;<\/p>\n<p>[html]<\/p>\n<p>context.arc(cw,ch,50,0,2*Math.PI,false);<\/p>\n<p>context.fillStyle=&#8217;#FFF&#8217;; \/\/ for color of circle<br \/>\n context.fill(); \/\/ fill function<br \/>\n context.strokeStyle=&#8217;#e7f2ba&#8217;; \/\/ for border color<br \/>\n context.stroke(); \/\/ Stroke function<\/p>\n<p>\u00a0[\/html]<\/p>\n<p>Up to this point, a\u00a0basic circle is created. Now, what we have to create is a progress bar circle.<\/p>\n<p>Third part coding starts here:-<\/p>\n<p>[html]<\/p>\n<p>context.fillStyle=&#8217;#000&#8242;; \/\/ For text color<br \/>\n context.strokeStyle=&#8217;#b3cf3c&#8217;; \/\/For Stroke Color<br \/>\n context.textAlign=&#8217;center&#8217;; \/\/you know already for aligning text in center;<br \/>\n context.lineWidth=15; \/\/ for Stroke width<br \/>\n context.font = &#8217;10pt Verdana&#8217;; \/\/ for font specifying<br \/>\n context.beginPath(); \/\/ starting circle drawing function<\/p>\n<p>\u00a0[\/html]<\/p>\n<p>\/\/formula of creating a circle in canvas like cw indicating x coordinate, ch indicating y coordinate,50 indicating radius, start<br \/>\nindicating starting angle, diff+start indicating end angle<br \/>\nFalse indicating anticlockwise direction.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>[html]<\/p>\n<p>context.arc(cw,ch,50,start,diff+start,false);<\/p>\n<p>context.stroke();\/\/ Stroke function<\/p>\n<p>context.fillText(al+&#8217;%&#8217;,cw+2,ch+6); \/\/text value &amp; text position<\/p>\n<p>This condition is used to give limit to progress bar<br \/>\n if(al&gt;=56){<br \/>\n clearTimeout(bar);<br \/>\n }<\/p>\n<p>al++; \/\/ increment the rate<br \/>\n }<\/p>\n<p>var bar=setInterval(progressBar,50); \/\/to call progress bar function after every 50 miliseconds.<\/p>\n<p>\u00a0[\/html]<\/p>\n<p>Code\u00a0part is completed here.<\/p>\n<p>Below I am giving you a\u00a0full version of the code,\u00a0copy and paste in your notepad and run the progress Bar on\u00a0the browser and do some experiments with it.<\/p>\n<p>[html]<\/p>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta charset=&quot;UTF-8&quot;&gt;<br \/>\n&lt;title&gt;Canvas Tutorial&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<\/p>\n<p>&lt;canvas id=&quot;myCanvas&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;\/canvas&gt;<br \/>\n&lt;script&gt;<br \/>\nvar canvas = document.getElementById(&#8216;myCanvas&#8217;);<br \/>\nvar context = canvas.getContext(&#8216;2d&#8217;);<br \/>\nvar al=0;<br \/>\nvar start=4.72;<br \/>\nvar cw=context.canvas.width\/2;<br \/>\nvar ch=context.canvas.height\/2;<br \/>\nvar diff;<\/p>\n<p>function progressBar(){<br \/>\ndiff=(al\/100)*Math.PI*2;<br \/>\ncontext.clearRect(0,0,400,200);<br \/>\ncontext.beginPath();<br \/>\ncontext.arc(cw,ch,50,0,2*Math.PI,false);<br \/>\ncontext.fillStyle=&#8217;#FFF&#8217;;<br \/>\ncontext.fill();<br \/>\ncontext.strokeStyle=&#8217;#e7f2ba&#8217;;<br \/>\ncontext.stroke();<br \/>\ncontext.fillStyle=&#8217;#000&#8242;;<br \/>\ncontext.strokeStyle=&#8217;#b3cf3c&#8217;;<br \/>\ncontext.textAlign=&#8217;center&#8217;;<br \/>\ncontext.lineWidth=15;<br \/>\ncontext.font = &#8217;10pt Verdana&#8217;;<br \/>\ncontext.beginPath();<br \/>\ncontext.arc(cw,ch,50,start,diff+start,false);<br \/>\ncontext.stroke();<br \/>\ncontext.fillText(al+&#8217;%&#8217;,cw+2,ch+6);<br \/>\nif(al&gt;=56){<br \/>\nclearTimeout(bar);<br \/>\n}<\/p>\n<p>al++;<br \/>\n}<\/p>\n<p>var bar=setInterval(progressBar,50);<br \/>\n&lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<p>[\/html]<\/p>\n<p>That&#8217;s all for my this blog.It concludes my experiment with HTML5\u2032s canvas. I hope this helps someone.Here are the list of browsers who fully support Canvas:- Chrome-4.0, IE-9.0, Firefox-2.0, Safari-3.1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Canvas is a very powerful tool for drawing graphics using scripting but what it need a basic understanding of HTML and JavaScript.Basic description of Canvas is here: The HTML5 Canvas element is an HTML tag like &lt;div&gt;, &lt;a&gt;, or &lt;table&gt; tag, with the exception that its contents are rendered with JavaScript. So, what I am [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":304},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31155"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=31155"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31155\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=31155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=31155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=31155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}