LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Create Analog Clock Widget With HTML5 Canvas And JavaScript

Create Analog Clock Widget With HTML5 Canvas And JavaScript

Create-Analog-Clock-Widget-With-HTML5-Canvas-And-JavaScript
Add a free clock to your blog or website with our free HTML5 24-hour analogue web clock showing the current time for any location in the world. Simply configure your clock to match your website and copy our short and simple embed code to place on your website. Our HTML5 digital clock widgets are replacing Flash clock widgets as a more secure and faster-loading clock. Replace your Flash clock widgets with our 24-hour digital clock widget. It will update automatically for Daylight Saving Time rules for your selected location. It’s FREE!

HTML5 Canvas element is used to hold the graphics drawn with JavaScript. The canvas tag is only used to hold the element as a placeholder or container. The actual graphics are drawn with JavaScript. In this article, we will draw a simple analogue clock using JavaScript and place it on a page with a Canvas container.

There are many code snippets available online or on many other blogs and websites but everyone is not able to optimize your blog or website so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Table of Contents

Recommended For You:
Image Color Extract Using Simple PHP Without Composer

Features:

  1. Light Weight.
  2. Pure JavaScriptCode.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.

How To Create Analog Clock Widget With HTML5 Canvas And JavaScript??

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

HTML:

<center>
<canvas id="canvas" width="300" height="300"></canvas>
</center>

JavaScript:

<script type="text/JavaScript">
x=150;
y=150;
clock=document.getElementById("canvas");
ctx=clock.getContext("2d");

function loop()
{
time=new Date();
h=time.getHours();
m=time.getMinutes();
s=time.getSeconds();

ctx.beginPath();
ctx.fillStyle="white";
ctx.arc(x,y,140,0,Math.PI*2,true);
ctx.fill();
ctx.strokeStyle="red";
ctx.lineWidth=10;
ctx.stroke();
drawNumber();

drawPointer(360*(h/12)+(m/60)*30-90,70,"black",10);
drawPointer(360*(m/60)+(s/60)*6-90,100,"black",10);
drawPointer(360*(s/60)+x-90,120,"red",2);
}

function drawNumber()
{
for(n=0;n<12;n++)
{
d=-60;
num = new Number(n+1);
str = num.toString();
dd = Math.PI/180*(d+n*30);
tx = Math.cos(dd)*120+140;
ty = Math.sin(dd)*120+160;
ctx.font = "26px Verdana";
ctx.fillStyle = "brown";
ctx.fillText(str, tx, ty);
}
}

function drawPointer(deg,len,color,w)
{
rad=(Math.PI/180*deg);
x1=x+Math.cos(rad)*len;
y1=y+Math.sin(rad)*len;

ctx.beginPath();
ctx.strokeStyle=color;
ctx.lineWidth=w;
ctx.moveTo(x,y);
ctx.lineTo(x1,y1);
ctx.stroke();
}
setInterval(loop,500);

</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after HTML code.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

Recommended For You:
Sliding Panel From Side On Click Using JQuery

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *