Javascript uses Canvas to implement a simple circular clock and javascriptcanvas

Source: Internet
Author: User

Javascript uses Canvas to implement a simple circular clock and javascriptcanvas

I learned the canvas Element in html5 before and implemented a simple clock for the trainer. The clock itself is not complex, and no image is used for beautification. However, although the sparrow is small and dirty, we will share it with you below:

Demo effect:

Html code:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Clock </title>
<Style type = "text/css">
*{
Margin: 0;
Padding: 0;
}
. Canvas {
Margin-left: 20px;
Margin-top: 20px;
Border: solid 1px;
}
</Style>
</Head>
<Body onload = "main ()">
<Canvas class = "canvas" id = "canvasId" width = '500px 'height = '400px '> </canvas>
<Script type = "text/javascript" src = "Clock. js"> </script>
</Body>
</Html>

JS Code:

Copy codeThe Code is as follows:
Var Canvas = {};
Canvas. cxt = document. getElementById ('canvasid'). getContext ('2d ');
Canvas. Point = function (x, y ){
This. x = x;
This. y = y;
};
/* Erase all images on the canvas */
Canvas. clearCxt = function (){
Var me = this;
Var canvas = me. cxt. canvas;
Me. cxt. clearRect (0, 0, canvas. offsetWidth, canvas. offsetHeight );
};
/* Clock */
Canvas. Clock = function (){
Var me = Canvas,
C = me. cxt,
Radius = 150,/* radius */
Scale = 20,/* scale length */
Minangle = (1/30) * Math. PI,/* radians per minute */
Hourangle = (1/6) * Math. PI,/* hour radians */
HourHandLength = radius/2,/* hour-hand length */
MinHandLength = radius/3*2,/* sub-needle length */
SecHandLength = radius/10*9,/* seconds */
Center = new me. Point (c. canvas. width/2, c. canvas. height/2);/* center */
/* Draw the center (Dial Center )*/
Function drawCenter (){
C. save ();
C. translate (center. x, center. y );
C. fillStyle = 'black ';
C. beginPath ();
C. arc (0, 0, radius/20, 0, 2 * Math. PI );
C. closePath ();
C. fill ();
C. stroke ();
C. restore ();
};
/* Draw the dial through Coordinate Transformation */
Function drawBackGround (){
C. save ();
C. translate (center. x, center. y);/* translation transformation */
/* Draw the scale */
Function drawScale (){
C. moveTo (radius-scale, 0 );
C. lineTo (radius, 0 );
};
C. beginPath ();
C. arc (0, 0, radius, 0, 2 * Math. PI, true );
C. closePath ();
For (var I = 1; I <= 12; I ++ ){
DrawScale ();
C. rotate (hourangle);/* Rotation Transformation */
};
/* Draw time )*/
C. font = "bold 30px impack"
C. fillText ("3", 110, 10 );
C. fillText ("6",-7,120 );
C. fillText ("9",-120, 10 );
C. Fills ltext ("12",-16,-100 );
C. stroke ();
C. restore ();
};
/* Draw the hour hand (h: current hour (in 24 hours ))*/
This. drawHourHand = function (h ){
H = 0? 24: h;
C. save ();
C. translate (center. x, center. y );
C. rotate (3/2 * Math. PI );
C. rotate (h * hourangle );
C. beginPath ();
C. moveTo (0, 0 );
C. lineTo (hourHandLength, 0 );
C. stroke ();
C. restore ();
};
/* Draw a sub-needle (m: Current score )*/
This. drawMinHand = function (m ){
M = 0? 60: m;
C. save ();
C. translate (center. x, center. y );
C. rotate (3/2 * Math. PI );
C. rotate (m * minangle );
C. beginPath ();
C. moveTo (0, 0 );
C. lineTo (minHandLength, 0 );
C. stroke ();
C. restore ();
};
/* Draw a second (s: current second )*/
This. drawSecHand = function (s ){
S = 0? 60: s;
C. save ();
C. translate (center. x, center. y );
C. rotate (3/2 * Math. PI );
C. rotate (s * minangle );
C. beginPath ();
C. moveTo (0, 0 );
C. lineTo (secHandLength, 0 );
C. stroke ();
C. restore ();
};
/* Draw a clock based on the local time */
This. drawClock = function (){
Var me = this;
Function draw (){
Var date = new Date ();
Canvas. clearCxt ();
DrawBackGround ();
DrawCenter ();
Me. drawHourHand (date. getHours () + date. getMinutes ()/60 );
Me. drawMinHand (date. getMinutes () + date. getSeconds ()/60 );
Me. drawSecHand (date. getSeconds ());
}
Draw ();
SetInterval (draw, 1000 );
};
};
Var main = function (){
Var clock = new Canvas. Clock ();
Clock. drawClock ();
};

The Code involves some simple canvas elements.

The above is all the content in this article. I hope it will help you learn canvas.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.