HTML5 application clock and html5 clock
Using the Canvas API of HTML5, we can achieve unexpected animation effects. Before we wanted to place a clock on the webpage, we had to use flash tools to create a clock and write complex JavaScript code, and load it to the page. With the advent of HTML5, we can directly use the canvas tag on the page and use javascript to complete a beautiful clock.
Today, we will use HTML5 to create a clock with pointers moving around.
HTML
We only need to place a canvas label in html: # clock. The width and height are both 400px.
[Html]View plaincopy
- <! Doctype html>
- <Html>
- <Head>
- <Meta charset = "UTF-8">
- <Title> HTML5 application clock </title>
- </Head>
- <Body>
- <Canvas id = "clock" width = "400" height = "400"> </canvas>
- </Body>
- </Html>
Javascript
Let's get the current time first: including the hour, minute, and second. Make sure that the pointer position is located when the page is opened, and the dot coordinates and second angle variables are defined.
[Js]View plaincopy
- Var time = new Date ();
- Var h = time. getHours (); // time
- Var m = time. getMinutes (); // minute
- Var s = time. getSeconds (); // second
- H = h> 12? (H-12) * 5 + parseInt (m/12): h * 5 + parseInt (m/12); // hour initial position
- Var x = 200, y = 200, sAngle = 0; // x y origin second point angle variable
Next, we use canvas to draw the clock. We write a function draw () to draw a scale and execute this function once. Then, the second needle moves around 1/60 degrees of arc clockwise.
First, obtain the canvas Drawing Object and draw the clock scale. We divide the clock (circle) into 12 scales, that is, each scale represents an hour, of course, you can also divide it into 60 scales, so that each scale represents 1 minute.
[Js]View plaincopy
- Function draw (){
- Var c = document. getElementById ("clock ");
- Var ctx = c. getContext ("2d"); // obtain the Drawing Object
- Ctx. clearRect (, c. width, c. height); // clear the last drawing
- S ++; // seconds
- Ctx. fillStyle = '# fff' // fill in the white background color
- Ctx. fillRect (, c. width, c. height); // you can specify the canvas area.
- Ctx. save (); // save the current drawing status
- // Time scale
- For (var I = 0; I <12; I ++) {// divide 12 scales
- Var angle = (Math. PI * 2)/12; // obtain the radians of each scale
- Ctx. beginPath (); // start painting
- Ctx. font = "12px Arial"; // set the font
- If (I = 0 | I = 3 | I = 6 | I = 9) {// when pointing to 0 (12 points)
- Ctx. fillStyle = "red"; // The scale is red.
- Radius = 4; // The scale radius is 4px long.
- } Else {
- Ctx. fillStyle = "blue"; // The scale is blue.
- Radius = 3; // The scale radius is 3px.
- }
- Ctx. arc (x, y-100, radius, 0, Math. PI * 2, true); // circle as scale
- Ctx. fill (); // fill path
- Transform (ctx, x, y, angle, true); // scale Distribution
- }
- Ctx. restore (); // restore the status of the last saved drawing
- ...
- }
According to the above code, you can draw a dial with a scale as the clock. Next, we will continue to write the rotation of the hour, minute, and second pointer in the draw () function.
[Js]View plaincopy
- Function draw (){
- ...
- SAngle = (Math. PI * 2)/60 * s; // Second Degree
- // Clockwise rotation
- Ctx. save ();
- Ctx. strokeStyle = "red ";
- Ctx. lineWidth = 3;
- Transform (ctx, x, y, (Math. PI * 2)/60 * h );
- Sj (ctx, x, y, Y-40 );
- Ctx. restore ();
- // Split the needle to rotate
- Ctx. save ();
- Ctx. strokeStyle = "blue ";
- Ctx. lineWidth = 2;
- Transform (ctx, x, y, (Math. PI * 2)/60 * m );
- Sj (ctx, x, y, y-68 );
- Ctx. restore ();
- // Seconds
- Ctx. save ();
- Ctx. strokeStyle = "#000 ";
- Transform (ctx, x, y, sAngle );
- Sj (ctx, x, y, y-80 );
- Ctx. restore ();
- // Data sorting
- If (s % 60 = 0 ){
- SAngle = 0, s = 0, m ++;
- If (m % 12 = 0) {// rotate once every 12 minutes
- If (m! = 0) h ++;
- If (m % 60 = 0) m = 0;
- }
- If (h % 60 = 0) h = 0;
- }
- }
For each call to draw (), we set different fill colors and pointer thicknesses for the hour, minute, and second pins, draw pointers, and rotate pointer positions based on radians. In this example, we call the custom function trans () and pointer ().
The pointer () function is used to draw pointers. Ctx indicates the canvas object, x, y indicates the dot coordinate, and z indicates the position of the pointer header.
[Js]View plaincopy
- Function pointer (ctx, x, y, z ){
- Ctx. beginPath ();
- Ctx. moveTo (x, y );
- Ctx. lineTo (x, z );
- Ctx. stroke ();
- Ctx. fill ();
- }
The transform () function is used to rotate the pointer. Ctx indicates the canvas object, x, y indicates the dot coordinate, and angle indicates the rotation angle.
[Js]View plaincopy
- Function trans (ctx, x, y, angle ){
- Ctx. transform (Math. cos (angle), Math. sin (angle ),
- -Math. sin (angle), Math. cos (angle ),
- X * (1-Math.cos (angle) + x * Math. sin (angle ),
- Y * (1-Math.cos (angle)-y * Math. sin (angle ))
- }
Finally, we set to execute draw () every 1 second (that is, 1000 milliseconds ().
[Js]View plaincopy
- SetInterval ("draw ()", 1000 );