javascript program of analog clock based on h5 canvas

design sketch

|




Update log

1. The first parameter of the construction method supports passing in the id of the canvas dom object. You can pass either the canvas dom object or the id of this dom object.
2. setOptions method is added to facilitate dynamic updating of analog clock properties.
3. Fix the BUG that the passed in options does not verify the attribute value. If some attributes of the passed in options are undefined, filter out the attribute.
4. options: handType pointer type is added. There are two optional values: line or triangle. The default is triangle

1. Installation

  • The < script > tag introduces "dist/clock.js" or "dist/clock.min.js"
  • Using npm, run the command: npm install - d reason clock

2. Examples

2.1 simplest use

  • code
//Method 1: pass in the canvas dom object
new Clock(document.getElementById("canvas")).run();

//Method 2: pass in the id of the canvas dom object
new Clock("canvas").run();

  • effect

2.2 display Roman numerals

  • code
new Clock(document.getElementById("canvas"), {scaleType: "roman"}).run();
  • effect


2.3 customize border color, background color, tick mark color and hour number color

  • code
new Clock(
    document.getElementById("canvas"),
    {
        borderColor: "brown",//Border color
        backgroundColor:"black",//Dial background color
        hourColor:"white",//Hour number color
        scaleColor:"yellow"//Tick mark color
    }
).run();
  • effect


2.4 custom border picture and background picture

  • Code 1
new Clock(
    document.getElementById("canvas"),
    {
        borderImage: "./img/border.png",
        backgroundImage: "./img/bg.jpg"
    }
).run();
```http://www.biyezuopin.vip
* Code 2 using remote pictures
```javascript
new Clock(
    document.getElementById("canvas"),
    {
        borderImage: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545553805386&di=ec656215a2958d617ef30631e96304e0&imgtype=0&src=http%3A%2F%2Fimg1.ali213.net%2Fshouyou%2Fupload%2Fimage%2F2018%2F07%2F09%2F584_2018070952816881.png",
        backgroundImage: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545553773235&di=1c768f80fc088c2edc20fa75af77c515&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201607%2F03%2F20160703164252_2WySB.jpeg"
    }
).run();
  • effect

3. Parameters

3.1 Clock(canvas, options)

  • canvas: container used to display analog clock. It can be an HTMLCanvasElement object or the id of an HTMLCanvasElement object.
  • Options: options has a default value and can't be passed on, but if you want to customize some beautiful styles, you can use the options parameter.

3.2 options default

attributeexplaintypeDefault value
sizeAnalog clock sizeNumber300
paddingpadding Number5
borderWidthBorder widthNumber15
borderColorBorder colorStringblack
borderImageBorder background picture, with priority over borderColorString-
backgroundColorBackground colorStringwhite
backgroundImageBackground picture, with priority over backgroundColorString-
backgroundModeBackground image display mode, with optional values of part or fullStringfull
backgroundAlphaTransparency of background pictureNumber0.5
scaleTypeScale type displayed, roman: roman numeral, arabic: arabic numeral, none: not displayedStringarabic
scaleColorTick mark colorString#666
hourColorScale value colorString#666
handTypePointer typeline | triangletriangle
secondHandColorSecond hand colorStringred
minuteHandColorMinute hand colorString#666
hourHandColorClock colorStringblack
showShadowClock colorBooleantrue
onloadAfter the picture is loaded, the callback parameter is the current Clock objectFunction-

4. Object method

  • show(time): used to display a time. It can be a Date object or a string like "hh:mm:ss". The return value of this method is the current object. For example:
//1. If you do not use the background or border map, you can directly use the show method
new Clock(document.getElementById("canvas")).show("15:25:40");

//2. If the background image or border image is used, you need to use the show method in the onload callback method
new Clock(
    document.getElementById("canvas"),
    {
        borderImage: "./img/border.png",
        backgroundImage: "./img/bg.jpg",
        onload(clock) {
            clock.show("15:25:40")
        }
    }
);

http://www.biyezuopin.vip

  • run(): when this method is executed, the simulation clock will render the interface every 1 second, and the return value of this method is the current object. For example:
new Clock(document.getElementById("canvas")).run();
  • stop(): execute this method to stop rendering the interface every 1 second. For example:
//Run an analog clock and stop after 4 seconds
var clock = new Clock(document.getElementById("canvas")).run();
setTimeout(function(){
    clock.stop();
}, 4000);
  • setOptions(options): execute this method to dynamically change some properties of the analog clock. For example:
//Run an analog clock and modify the size of the analog clock to 500 after 4 seconds. Refer to demo / demo2 html
var clock = new Clock(document.getElementById("canvas")).run();
setTimeout(function(){
    clock.setOptions({size: 500});
}, 4000);

Keywords: Javascript Front-end Vue.js

Added by harrylt on Wed, 23 Feb 2022 14:51:01 +0200