介绍一个用HTML5 Canvas 制作的时钟


本文摘自PHP中文网,作者零下一度,侵删。

用HTML5 Canvas 制作的时钟,看着挺简单的,但其中写起来还是有很多小的问题的,感兴趣的小伙伴们可以参考一下

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<!DOCTYPE html>

<html lang="en" >

    <head>

        <meta charset="utf-8" />

        <title>HTML5 时钟</title>

        <link href="css/main.css" rel="stylesheet" type="text/css" />

        <script src="http://code.jquery.com/jquery-latest.min.js"></script>

      <style>

        .clocks {

          height: 500px;

          margin: 25px auto;

          position: relative;

          width: 500px;

        }

      </style>

    </head>

    <body>

        <header>

            <h2>HTML5 时钟</h2>

        </header>

        <p class="clocks">

            <canvas id="canvas" width="500" height="500"></canvas>

        </p>

    </body>

</html>

[JavaScript]代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

// inner variables

var canvas, ctx;

var clockRadius = 250;

var clockImage;

 

// draw functions :

function clear() { // clear canvas function

    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

}

 

function drawScene() { // main drawScene function

    clear(); // clear canvas

 

    // get current time

    var date = new Date();

    var hours = date.getHours();

    var minutes = date.getMinutes();

    var seconds = date.getSeconds();

    hours = hours > 12 ? hours - 12 : hours;

    var hour = hours + minutes / 60;

    var minute = minutes + seconds / 60;

 

    // save current context

    ctx.save();

 

    // draw clock image (as background)

    ctx.drawImage(clockImage, 0, 0, 500, 500);

 

    ctx.translate(canvas.width / 2, canvas.height / 2);

    ctx.beginPath();

 

    // draw numbers

    ctx.font = '36px Arial';

    ctx.fillStyle = '#000';

    ctx.textAlign = 'center';

    ctx.textBaseline = 'middle';

    for (var n = 1; n <= 12; n++) {

        var theta = (n - 3) * (Math.PI * 2) / 12;

        var x = clockRadius * 0.7 * Math.cos(theta);

        var y = clockRadius * 0.7 * Math.sin(theta);

        ctx.fillText(n, x, y);

    }

 

    // draw hour

    ctx.save();

    var theta = (hour - 3) * 2 * Math.PI / 12;

    ctx.rotate(theta);

    ctx.beginPath();

    ctx.moveTo(-15, -5);

    ctx.lineTo(-15, 5);

    ctx.lineTo(clockRadius * 0.5, 1);

    ctx.lineTo(clockRadius * 0.5, -1);

    ctx.fill();

    ctx.restore();

 

    // draw minute

    ctx.save();

    var theta = (minute - 15) * 2 * Math.PI / 60;

    ctx.rotate(theta);

    ctx.beginPath();

    ctx.moveTo(-15, -4);

    ctx.lineTo(-15, 4);

    ctx.lineTo(clockRadius * 0.8, 1);

    ctx.lineTo(clockRadius * 0.8, -1);

    ctx.fill();

    ctx.restore();

 

    // draw second

    ctx.save();

    var theta = (seconds - 15) * 2 * Math.PI / 60;

    ctx.rotate(theta);

    ctx.beginPath();

    ctx.moveTo(-15, -3);

    ctx.lineTo(-15, 3);

    ctx.lineTo(clockRadius * 0.9, 1);

    ctx.lineTo(clockRadius * 0.9, -1);

    ctx.fillStyle = '#0f0';

    ctx.fill();

    ctx.restore();

 

    ctx.restore();

}

// initialization

$(function(){

    canvas = document.getElementById('canvas');

    ctx = canvas.getContext('2d');

 

    // var width = canvas.width;

    // var height = canvas.height;

 

clockImage = new Image();

clockImage.src = 'http://static.oschina.net/uploads/space/2012/0712/125855_nnla_89964.png';

 

    setInterval(drawScene, 1000); // loop drawScene

});

以上就是介绍一个用HTML5 Canvas 制作的时钟的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

HTML5规定元素是否可拖动的属性draggable

HTML5的web界面中meta实列详解

关于HTML5本地存储的相关讲解

HTML5 canvas绘图之drawimage() 方法的详细介绍(代码示例)

HTML5手机开发-滚动和惯性缓动的代码实例

exports和module.expors之间有什么区别及联系?

了解一下HTML5中新增加的标签

canvas中beginpath()和closepath()的分析总结(附示例)

有关HTML5页面在iphonex适配问题

用h5做有特效的下拉框

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...