Use canvas to draw a superb clock and canvas to draw a superb clock

Source: Internet
Author: User

Use canvas to draw a superb clock and canvas to draw a superb clock
This article mainly introduces how to use canvas to draw a superb clock and its code. It is very beautiful and we recommend it to you.

First:


The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> canvas watch </title>
<Meta name = "Keywords" content = "">
<Meta name = "author" content = "@ my_programmer">
<Style type = "text/css">
Body {margin: 0 ;}
</Style>
</Head>
<Body onload = "run ()">
<Canvas id = "canvas" width = 400 height = 400 style = "border: 1px # ccc solid;"> if you see this text, your browser is weak! </Canvas>
<Script>
Window. onload = draw;
Function draw (){
Var canvas = document. getElementById ('canvas ');
Var context = canvas. getContext ('2d ');
Context. save (); // save
Context. translate (200,200 );
Var deg = 2 * Math. PI/12;
//////////////////////////////////////// ///// // Dial
Context. save ();
Context. beginPath ();
For (var I = 0; I <13; I ++ ){
Var x = Math. sin (I * deg );
Var y =-Math. cos (I * deg );
Context. lineTo (x * 150, y * 150 );
}
Var c = context. createRadialGradient (0,130 );
C. addColorStop (0, "# 22f ");
C. addColorStop (1, "# 0ef ")
Context. fillStyle = c;
Context. fill ();
Context. closePath ();
Context. restore ();
//////////////////////////////////////// /// // Number
Context. save ();
Context. beginPath ();
For (var I = 1; I <13; I ++ ){
Var x1 = Math. sin (I * deg );
Var y1 =-Math. cos (I * deg );
Context. fillStyle = "# fff ";
Context. font = "bold 20px Calibri ";
Context. textAlign = 'center ';
Context. textBaseline = 'middle ';
Context. fillText (I, x1 * 120, y1 * 120 );
}
Context. closePath ();
Context. restore ();
//////////////////////////////////////// //// // Large Scale
Context. save ();
Context. beginPath ();
For (var I = 0; I <12; I ++ ){
Var x2 = Math. sin (I * deg );
Var y2 =-Math. cos (I * deg );
Context. moveTo (x2 * 148, y2 * 148 );
Context. lineTo (x2 * 135, y2 * 135 );
}
Context. strokeStyle = '# fff ';
Context. lineWidth = 4;
Context. stroke ();
Context. closePath ();
Context. restore ();
//////////////////////////////////////// //// // Small Scale
Context. save ();
Var deg1 = 2 * Math. PI/60;
Context. beginPath ();
For (var I = 0; I <60; I ++ ){
Var x2 = Math. sin (I * deg1 );
Var y2 =-Math. cos (I * deg1 );
Context. moveTo (x2 * 146, y2 * 146 );
Context. lineTo (x2 * 140, y2 * 140 );
}
Context. strokeStyle = '# fff ';
Context. lineWidth = 2;
Context. stroke ();
Context. closePath ();
Context. restore ();
//////////////////////////////////////// /// // Text
Context. save ();
Context. strokeStyle = "# fff ";
Context. font = '34px sans-serif ';
Context. textAlign = 'center ';
Context. textBaseline = 'middle ';
Context. strokeText ('canvas ', 0, 65 );
Context. restore ();
//////////////////////////////////////// //// // New Date
Var time = new Date ();
Var h = (time. getHours () % 12) * 2 * Math. PI/12;
Var m = time. getMinutes () * 2 * Math. PI/60;
Var s = time. getSeconds () * 2 * Math. PI/60; </p> <p> ///////////////////////////////// //// // hour
Context. save ();
Context. rotate (h + m/12 + s/720 );
Context. beginPath ();
Context. moveTo (0, 6 );
Context. lineTo (0,-85 );
Context. strokeStyle = "# fff ";
Context. lineWidth = 6;
Context. stroke ();
Context. closePath ();
Context. restore ();
//////////////////////////////////////// ///// Sub-needle
Context. save ();
Context. rotate (m + s/60 );
Context. beginPath ();
Context. moveTo (0, 8 );
Context. lineTo (0,-105 );
Context. strokeStyle = "# fff ";
Context. lineWidth = 4;
Context. stroke ();
Context. closePath ();
Context. restore ();
//////////////////////////////////////// //// Seconds
Context. save ();
Context. rotate (s );
Context. beginPath ();
Context. moveTo (0, 10 );
Context. lineTo (0,-120 );
Context. strokeStyle = "# fff ";
Context. lineWidth = 2;
Context. stroke ();
Context. closePath ();
Context. restore ();
Context. restore (); //
SetTimeout (draw, 1000 ); /// // timer </p> <p >}</ p> </script>
</Body>
</Html>

Related Article

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.