javascript - 将 Canvas 时钟设置为系统时间

标签 javascript html canvas time clock

我在 Canvas 上做了一个时钟,它每秒都会滴答作响。我想设置系统时钟的时间。我使用函数“var date = new Date();”获得了时间。但不知道如何根据时间获取 Angular 。

非常感谢您的帮助。

JSFiddle:http://jsfiddle.net/RaoBurugula/6aLauwaj/2/

        var c = document.getElementById("myCanvas");
        var ctxline = c.getContext("2d");
        //variables
        var seconds = 0,minutes = 0 , hours = 0;
        var iSeconds = 0, counterSeconds = 0, xSeconds=180,ySeconds=0;
        var increaseSeconds = 6 * Math.PI /180;
        var iMinutes = 0, counterMinutes = 0, xMinutes=180,yMinutes=0;
        var increaseMinutes = 6 * Math.PI /180;
        var iHours = 0, counterHours = 0, xHours=180,yHours=0;
        var increaseHours = 6 * Math.PI /180;
        //ctxline.clearRect(0, 0, c.width, c.height);

        //drawLinesInCircle();
        clockDesign();
        getAndSetCurrentTime();
        clock();
        function clockDesign(){
            var iClockHours = 0, counterClockHours = 0, xClockHours=180,yClockHours=0;
            var increaseClockHours = 30 * Math.PI /180;
            var hours = ["12", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"];
            var gradient=ctxline.createLinearGradient(0,0,c.width,0);
            jQuery.each( hours, function( i, val ) {                 
                xClockHours = 180 + Math.sin(counterClockHours)* 140;
                yClockHours =  180 - Math.cos(counterClockHours)* 140;
                counterClockHours += increaseClockHours; 
                //alert(counterClockHours);
                // Create gradient
                ctxline.font="30px Verdana";
                ctxline.textAlign = 'center';
                gradient.addColorStop("0.5","red");


                ctxline.fillStyle=gradient;
                ctxline.fillText(val,xClockHours,yClockHours);

            });
        }

        function getAndSetCurrentTime(){
            var dt = new Date();
            var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
            //alert(time);
        }

        function clock(){
            //Seconds
            seconds += 1;
            xSeconds = 180 + Math.sin(counterSeconds)* 120;
            ySeconds =  180 - Math.cos(counterSeconds)* 120;    
            counterSeconds += increaseSeconds;
            iSeconds += 6;

            //minutes
            xMinutes = 180 + Math.sin(counterMinutes)* 110;
            yMinutes =  180 - Math.cos(counterMinutes)* 110;
            //hours
            xHours = 180 + Math.sin(counterHours)* 100;
            yHours =  180 - Math.cos(counterHours)* 100;

            if(seconds == 60){  
                //alert("one min");
                minutes +=1;
                seconds =0;
                counterMinutes += increaseMinutes; 
                iMinutes += 6;
            }

            if(minutes == 60){
                hours +=1;
                minutes = 0;
                seconds = 0;
                counterHours += increaseHours; 
                iHours += 6;
            }

            if(hours == 12){
                hours = 0;
                minutes = 0;
                seconds = 0; 
            }

            draw(xSeconds,ySeconds, xMinutes, yMinutes, xHours, yHours);
            setTimeout(clock, 1000); 
        }

        function draw(xSecondsTo,ySecondsTo, xMinutesTo, yMinutesTo, xHoursTo, yHoursTo){
            ctxline.clearRect(0, 0, c.width, c.height);
            clockDesign();
            ctxline.beginPath();
            //seconds line
            ctxline.strokeStyle = ("#EDDE54");
            ctxline.lineWidth = 1;
            ctxline.moveTo(180,180);     
            ctxline.lineTo(xSecondsTo,ySecondsTo);
            ctxline.stroke();
            ctxline.closePath();

            //miuntes line  
            ctxline.beginPath();
            ctxline.strokeStyle = ("#545EED");
            ctxline.lineWidth = 4;
            ctxline.moveTo(180,180);     
            ctxline.lineTo(xMinutesTo,yMinutesTo);
            ctxline.stroke();
            ctxline.closePath();
            //Hours line 
            ctxline.beginPath();
            ctxline.strokeStyle = ("#BE1616");
            ctxline.lineWidth = 7;
            ctxline.moveTo(180,180);     
            ctxline.lineTo(xHoursTo,yHoursTo);
            ctxline.stroke();
            ctxline.closePath();
        }

最佳答案

参见http://jsfiddle.net/6aLauwaj/9/

就像Luckyn建议的那样,但需要调整时间。

var secs = dt.getSeconds();
seconds=secs;
counterSeconds = secs * (increaseSeconds);

var mins = dt.getMinutes();
minutes = mins;
counterMinutes = mins * increaseMinutes;
//alert(time);
var Hours = dt.getHours() % 12;
Hours *= 5;
hours = Hours;
counterHours = (Hours * increaseHours) + (30 * counterMinutes / 360); //Need to consider the minutes, take minutes angle and calculate how much further hour should be angled.

关于javascript - 将 Canvas 时钟设置为系统时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30052954/

相关文章:

javascript 三元条件运算

javascript - 使用 Object.create() 时,如何使用其他对象键引用对象键

html - 下拉列表在 CSS HTML 中显示用户的图像和名称

html - wrapper div 的宽度和 content right 的宽度应根据 content right 中的内容增加

javascript canvas 两个path2D之间的交集

javascript - 如何在 Canvas 中的单击点上居中和缩放

node.js - 如何使用 Node.js 保存图像(作为 Base64 编码字符串传入)

javascript - 如何判断是否支持window.confirm()?

javascript - 在 Javascript 中从 PostgreSQL 查询结果中过滤掉唯一数据

javascript - JSON 数组长度为零,即使有元素?