Let's make a clock!
We are going to draw an analog clock and make it work as a real clock. In your body part of the HTML document, type the following code:
<canvas id="myclock" height="500" width="500"></canvas> In your <script></script> tags, take the following variables: Var canvas; // the clock canvas var canvasElement; // canvas's elements // clock settings var cX = 0; var cY = 0; var radius = 150;
Here, cX
and cY
are the center coordinates of our clock. We took 150 px as the clock's radius. You can increase or decrease it.
Then, we need to initialize the variables. Make an init();
function after defining the preceding variables.
The function should look similar to the following:
function init() { canvas = document.getElementById("myclock"); //Called the element to work on. canvasElement = canvas.getContext("2d"); //Made the context 2d. cX = canvas.width / 2; // we divided by two to get the middle point of X-axis cY = canvas.height / 2; // we...