用raphaeljs库写一个时钟

在做图表中用到raphaeljs图表库,在查相关资料时做了个下面的的小练习,效果如下图


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>时钟</title>
    <script src="chart/raphael.js"></script>
    <script type="text/javascript">

        window.onload = function () {
            var originX = 300, originY = 200, backgroundWidth = 680, backgroundHeight = 480;
            var r = 100;
            var paper = Raphael(document.getElementById("paper"), backgroundWidth, backgroundHeight);
            paper.rect(0, 0, backgroundWidth, backgroundHeight, 10).attr({ fill: "#F2F1D7", stroke: "none" });
            paper.circle(originX, originY, r).attr("stroke", "red").attr("fill","white");
            var secondHand = paper.path("M" + originX + "," + originY + " v " + (r - 10));//秒针
            var minuteHand = paper.path("M" + originX + "," + originY + " v " + (-(r - 20))).attr("stroke-width", 3).attr("stroke","green");
            var hourHand = paper.path("M" + originX + "," + originY + " v " + (-(r - 40))).attr("stroke-width", 3).attr("stroke", "red");
            var timeText = paper.text(originX - 5, originY - r - 30, new Date().toLocaleTimeString()).attr("font-size",20);
            for (var i = 0; i < 12; i++) {
                paper.path("M" + (originX + r - 8) + "," + originY + "h 8").rotate(30 * i, originX, originY);
            }
            function setPosition() {
                var currentDate = new Date();
                minuteHand.attr("transform", "r " + ((currentDate.getMinutes() * 6)) + " " + originX + " " + originY);
                secondHand.attr("transform", "r " + ((currentDate.getSeconds() * 6)) + " " + originX + " " + originY);
                hourHand.attr("transform", "r " + ((currentDate.getHours() * 6)) + " " + originX + " " + originY);
                timeText.attr("text", currentDate.toLocaleTimeString());
            }
            setPosition();
            window.setInterval(function () {
                setPosition();
            }, 1000);
            
        };
    </script>
</head>
<body>
<div id="paper">

</div>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值