Joke Collection Website - Joke collection - What are the calendar clock cases implemented by js Canvas?

What are the calendar clock cases implemented by js Canvas?

First, get the context object?

Varcxt = document. Getelementbyid ('element name'). get cont(“2d”); ?

Elements are not supported in browsers of IE8 or earlier.

Second, draw clock()- realize drawing clock?

1.clearRect () clears the specified pixel in the given rectangle. ?

context.clearRect(x,y,width,height);

Attribute |? value

- | -

X, y | The (x, y) coordinate of the upper-left corner of the rectangle to be cleared.

Width, height| The width and height of the rectangle to be cleared, in pixels 12345.

2.new date()- get the system time.

var sec = now . get seconds(); ? var min = now . get minutes(); ? var hour = now . get hours(); ? 123

Draw the shape of the clock

cxt . begin path(); ? cxt . line width = 10; ? cxt.strokeStyle = " blue? cxt.arc(550,3 10,300,0,360,false); ? cxt . close path(); ? cxt . stroke(); 123456

BeginPath () is used to draw the canvas, and all paths after the last beginPath will be drawn.

Closepath () is a closed path, not an ending path. It will try to connect a path from the end point to the start point of the current path, so that the whole path can be closed.

Cxt.lineWidth (): the width of the brush.

Cxt.strokeStyle (): Sets or returns the color, gradient or mode used for strokes.

Attribute value: color indicates the CSS color value of the stroke color of the drawing brush. The default value is #000000.

GradientA gradient object (linear or radioactive) used to fill a drawing.

Pattern A pattern object used to create a pattern stroke.

Stroke () draws the defined path.

The arc () method creates an arc/curve (used to create a circle or a partial circle). To create a circle by an arc (), set the starting angle to 0 and the ending angle to 2 * math.pi ...

Context.arc(x, y, r, Sang Le, eAngle, counterclockwise);

parameter

describe

The x coordinate of the center of the x circle.

Y coordinate of the center of the y circle.

The radius of the r circle.

Sang Le starting angle, in radians. The three o'clock position of the arc is 0 degrees.

Angle End Angle, in radians.

Counterclockwise is optional. Specifies whether the drawing should be counterclockwise or clockwise. False = clockwise, true = counterclockwise.

4)draw scale- user-defined function drawing scale.

Function drawScale(size, width, color, value, startx, starty, endx, endy){?

for(var I = 0; I< size; i++){?

DrawPointer (width, color, value, I, startx, starty, endx, endy); ?

} ? } ? 12345

5. Draw the clock scale support point

Function drawPointer (width, color, value, angle, startx, starty, endx, endy){?

cxt . save(); //Save the current canvas first?

Cxt.lineWidth = width; ? //Set the width of the brush?

Cxt.strokeStyle = color// Set the color of the brush?

cxt.translate(550,3 10); //Reset the origin coordinates of different dimensions?

Cxt.rotate (value * angle * mathematics. PI/ 180); ? //Set the rotation angle. Parameter is radian?

cxt . begin path(); ?

cxt.moveTo(startx,starty); ?

cxt.lineTo(endx,endy); ?

cxt . close path(); //Close the path first, then draw a line?

cxt . stroke(); ? //Start drawing lines?

cxt . restore(); ? //Return the rotated line segment to the canvas? } ? 123456789 10 1 1 12 13

The translate () method remaps the (0,0) position on the canvas.

-

JS code is as follows:

//Get the context document object? var clock = document . getelementbyid(' clock '); ?

var cxt = clock . get context(' 2d '); ?

//Draw a pointer? Function drawPointer (width, color, value, angle, startx, starty, endx, endy){?

cxt . save(); //Save the current canvas first?

Cxt.lineWidth = width; ? //Set the width of the brush?

Cxt.strokeStyle = color// Set the color of the brush?

cxt.translate(550,3 10); //Reset the origin coordinates of different dimensions?

Cxt.rotate (value * angle * mathematics. PI/ 180); ? //Set the rotation angle. Parameter is radian?

cxt . begin path(); ?

cxt.moveTo(startx,starty); ?

cxt.lineTo(endx,endy); ?

cxt . close path(); //Close the path first, then draw a line?

cxt . stroke(); ? //Start drawing lines?

cxt . restore(); ? //Return the rotated line segment to the canvas? } ?

//Draw a scale? Function drawScale(size, width, color, value, startx, starty, endx, endy){?

for(var I = 0; I< size; i++){?

DrawPointer (width, color, value, I, startx, starty, endx, endy); ?

} ?

} ?

//Fill the center of the dial with color? Function drawFill(){?

cxt . save(); ?

cxt . begin path(); ?

cxt.arc(550,3 10,7,0,360,false); ?

cxt . close path(); ?

cxt.fillStyle = " red?

cxt . fill(); ?

cxt . restore(); ?

} ?

//Draw a clock? Function drawClock(){?

cxt.clearRect(0,0, 1350,620); ? //Empty the whole canvas?

var now = new Date(); //Get the system time, when it is taken out, minutes, seconds?

var sec = now . get seconds(); ?

var min = now . get minutes(); ?

var hour = now . get hours(); ?

Min += second/60; ?

Hours+= minutes/60; ?

If (hour >12) hour-=12; ?

cxt . begin path(); ?

cxt . line width = 10; ?

cxt.strokeStyle = " blue?

cxt.arc(550,3 10,300,0,360,false); ?

cxt . close path(); ?

cxt . stroke(); ?

Drawscale (12,7, "pink", 30,0,-280,0,-260); ? //draw time?

DrawScale(60, 5, "pink", 6,? 0, -280, 0, -270); ? //Draw a scale?

DrawPointer(7, "purple", hour, 30,0,12,0,-210); ? //Draw an hour hand?

DrawPointer(5, "yellow", min, 6,0,15,0,-240); //Draw the minute hand?

DrawPointer(4, "red", sec, 6,0,17,0,-250); ? //Draw a second hand?

//Refine the second hand and add arrows?

DrawPointer(3, "red", sec, 6, -7, -235, 0,-255); ?

DrawPointer(3, "red", sec, 6, 7, -235, 0,-255); ?

draw fill(); ?

} ?

draw clock(); ?

setInterval(drawClock, 1000); ? //Using the //setInterval () method means that drawClock is executed every 1000ms? 123456789 10 1 1 12 13 14 15 16 17 18 19202 1 2223242526272829303 132333435363738 39404 14243444546474849505 15253545556575859606 16263646566676869707 1