Create an Analog Clock using Javascript
⧉ Category: Programing | 📅 Date: 17 Mar 2024 | अ🉀 Translation:

<canvas id="canvas" width="400" height="400" style="background-color: #333333"></canvas>
Let's call the canvas element and store it in variable "canvas", getContext() function is to open the canvas editable and tell javascript that it is a 2D image. Since we are making a clock, name it 'clock' as well. Create a variable and name it 'radius' since we are creating a circle shape clock and radius of the circle is required. And therRadius is half the size of the circle.
translate(): here is our first function inside the canvas. Remember the Pencil Compass we used to draw circle when we were in school? translate() function acts like the compass point. it is to mark the starting point of our drawing in the canvas. In other words, we are using translate() to change the current Origin point of the canvas. we need to mark X and Y axis in the canvas using translate(x,y). Currently we use radius as both X and Y axis since it is the center of the canvas.
<script>
var canvas = document.getElementById("canvas");
var clock = canvas.getContext("2d");
var radius = canvas.width/2;
clock.translate(radius, radius);
</script>

function clockface(clock, radius) {
clock.beginPath();
clock.arc(0, 0, radius*0.9, 0, 2*Math.PI);
clock.fillStyle = "white";
clock.fill();
}

function clocknumber(clock, radius) {
clock.textAlign = "center";
clock.textBaseline = "middle";
clock.fillStyle = "#333333";
clock.font = radius*0.2+"px arial";
for(n=1; n<13; n++) {
var ang = n*Math.PI/6;
clock.rotate(ang);
clock.translate(0, -radius*0.75);
clock.rotate(-ang);
clock.fillText(n.toString(), 0, 0);
clock.rotate(ang);
clock.translate(0, radius*0.75);
clock.rotate(-ang);
}
}

function clocktime(clock, radius) {
var date = new Date();
var hour = date.getHours();
if(hour>12) {
hour = hour-12;
}
var minute = date.getMinutes();
var second = date.getSeconds();
var second_pos = ((12*(Math.PI/6))/60)*second;
var minute_pos = ((12*(Math.PI/6))/60)*minute;
var hour_pos = (Math.PI/6)*(hour+((1/60)*minute));
clockhand(clock, radius*0.05, radius*0.8, minute_pos);
clockhand(clock, radius*0.09, radius*0.55, hour_pos);
clockhand(clock, radius*0.02, radius*0.8, second_pos);
}
function clockhand(clock, width, height, pos) {
clock.beginPath();
clock.lineWidth = width;
clock.lineCap = "round";
clock.rotate(pos);
clock.moveTo(0,0);
clock.lineTo(0, -height);
clock.strokeStyle = "#333333";
clock.stroke();
clock.rotate(-pos);
}
And the final part is to create another function and add all other functions we made to this function.
Now to make this clock ticking, we can use a setInterval() function and set a (1000ms = 1 second) time interval.
setInterval(createclock, 1000);
function createclock() {
clockface(clock, radius);
clocknumber(clock, radius);
clocktime(clock, radius);
}

<canvas id="canvas" width="400" height="400" style="background-color: #333333"></canvas>
<script>
var canvas = document.getElementById("canvas");
var clock = canvas.getContext("2d");
var radius = canvas.width/2;
clock.translate(radius, radius);
setInterval(createclock, 1000);
function createclock() {
clockface(clock, radius);
clocknumber(clock, radius);
clocktime(clock, radius);
clock.beginPath();
clock.shadowBlur = 2;
clock.shadowColor = "white";
clock.fillStyle = "#333333";
clock.arc(0, 0, radius*0.08, 0, 2*Math.PI);
clock.fill();
clock.shadowBlur = 0;
}
function clockface(clock, radius) {
clock.translate(-radius, -radius);
clock.fillStyle = "#333333";
clock.fillRect(0,0, radius*2, radius*2);
clock.translate(radius, radius);
clock.beginPath();
clock.arc(0, 0, radius*0.9, 0, 2*Math.PI);
clock.fillStyle = "white";
clock.fill();
clock.textAlign = "center";
clock.textBaseline = "middle";
clock.font = radius*0.15+"px arial";
clock.fillStyle = "#333333";
clock.fillText("DIZORDAT", 0, -radius*0.35);
}
function clocknumber(clock, radius) {
clock.textAlign = "center";
clock.textBaseline = "middle";
clock.fillStyle = "#333333";
clock.font = radius*0.2+"px arial";
for(n=1; n<13; n++) {
var ang = n*Math.PI/6;
clock.rotate(ang);
clock.translate(0, -radius*0.75);
clock.rotate(-ang);
clock.fillText(n.toString(), 0, 0);
clock.rotate(ang);
clock.translate(0, radius*0.75);
clock.rotate(-ang);
}
}
function clocktime(clock, radius) {
var date = new Date();
var hour = date.getHours();
if(hour>12) {
hour = hour-12;
}
var minute = date.getMinutes();
var second = date.getSeconds();
var second_pos = ((12*(Math.PI/6))/60)*second;
var minute_pos = ((12*(Math.PI/6))/60)*minute;
var hour_pos = (Math.PI/6)*(hour+((1/60)*minute));
clockhand(clock, radius*0.05, radius*0.8, minute_pos);
clockhand(clock, radius*0.09, radius*0.55, hour_pos);
clockhand(clock, radius*0.02, radius*0.8, second_pos);
}
function clockhand(clock, width, height, pos) {
clock.beginPath();
clock.lineWidth = width;
clock.lineCap = "round";
clock.shadowBlur = 2;
clock.shadowColor = "white";
clock.rotate(pos);
clock.moveTo(0,0);
clock.lineTo(0, -height);
clock.strokeStyle = "#333333";
clock.stroke();
clock.rotate(-pos);
}
</script>
«[Previous Post] How to change server timezone to local timezone in..How to Connect Your Smartphone or Laptop to a WiFi.. [Next Post]»
Comments
Please solve above captcha based on the hint given..