What you need to know:
The canvas tag is just a graphics container, you have to use a script to draw the graphics. Default size: width 300px, height 150px;
The getContext() method returns an object that provides methods and properties for drawing on the canvas. ——Get the context object.
getContext("2d") object properties and methods, which can be used to draw text, lines, rectangles, circles, etc. on the canvas.
fillRect(l,t,w,h): The default color is black strokeRect(l,t,w,h): A square with a border. Default one pixel black border
The setInterval() method can call a function or calculate an expression according to the specified period (in milliseconds).
beginPath(): Define the beginning of drawing the path, which sets the current point to (0,0). When a canvas environment is first created, the beginPath()
method is called explicitly.
closePath(): End drawing the path (connect the starting point and end point)
Draw a circle:
arc (x, y, radius, starting arc, ending arc, rotation direction)
x, y: starting position
The relationship between arc and angle: Radians = angle *Math.PI/180
Rotation direction: clockwise (default: false, counterclockwise: true)
Code:
XML/HTML CodeCopy content to clipboard
- >
-
<html lang="en-US">
-
<head>
-
<meta charset="UTF-8">
-
<title>title>
-
<script>
-
window.onload = function(){
-
var oC = document.getElementById('ch1');
-
var oGC = oC.getContext('2d');
-
- function drawClock(){
-
var x = 200; //指定坐标
-
var y = 200;
-
var r = 150; //指定钟表半径
-
- oGC.clearRect(0,0,oC.width,oC.height);//清空画布
-
-
var oDate = new Date(); //创建日期对象
- var Ohours = Odate .Gethours (); //
var 🎜>
- var 🎜>
🎜>
var
var
- osenvalue = (-90 osen*6)*math.pi/180;
oGC.beginPath();//Start
-
-
for(var i=
- 0;i<60
- ;i ){ //i is 60, which represents the 60 small scales of the clock
oGC.moveTo(x,y);
- oGC.arc(x,y,r,6*i*Math.PI/180,6*(i 1)*Math.PI/180,false); //Loop from 6 degrees to 12 Degree
-
- oGC.closePath();
- oGC.stroke();
-
-
oGC.beginPath();
oGC.moveTo(x,y);
- oGC.arc(x,y,r*19/20,0,360*(i 1)*Math.PI/180,false);
-
- oGC.closePath();//End
- oGC.fill();
-
- 🎜>
’ s ’ s ’s ‐ to GC.be''s-'s t-----oGC.beginPath();
-
- for(i=0;i
<- 12
;i ){ //i is 12, which represents the 12 grids of the clock scale -
oGC.moveTo(x,y); -
‐ oGC. =Angle*Math.PI/180
- oGC.closePath();
- oGC.stroke();
-
-
oGC.beginPath();
oGC.moveTo(x,y);
- oGC.arc(x,y,r*18/20,360*(i 1)*Math.PI/180,false);
-
oGC.closePath();
-
oGC.fill();//The dial is completed
-
-
<<>
OGC.LineWidth - =
5 - ;
oGC.beginPath();//Start drawing the hour hand -
oGC.moveTo(x,y);
oGC.arc(x,y,r*10/20,oHoursValue,oHoursValue,false);//Set the hour hand size and radian -
oGC.closePath(); -
oGC.stroke(); -
-
<<> - OGC.LineWidth =
- 3 ;
- oGC.beginPath();//Start drawing the minute hand
- oGC.moveTo(x,y);
-
- oGC.arc(x,y,r*14/20,oMinValue,oMinValue,false);//Set the minute hand size and radian
oGC.closePath(); -
oGC.stroke(); -
-
<<> - OGC.LineWidth = 1 ; // Set the width of the second hand
oGC.beginPath();//Start drawing the second hand
-
oGC.moveTo(x,y);
-
-
oGC.arc(x,y,r*19/20,oSenValue,oSenValue,false);//Set the second hand size and arc
-
oGC.closePath();
-
oGC.stroke();
-
- setInterval(drawClock,1000);//Set the timer and let the clock run
- drawClock();
- };
- script>
-
head>
-
<body>
-
<canvas id = "ch1" width = "400px" height = "400px">canvas>
-
body>
-
html>
Click the result below to view the demo:
http://jsfiddle.net/eh02450b/2/
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn