javascript - HTML5数字时钟动画

标签 javascript html html5-canvas

我是 HTML5 和 Web 编程新手。我已经掌握了一些基础知识,并开始了解基于 Canvas 的动画。然而,在尝试创建基于数字时钟的动画时,我没有成功。该代码在 JSLint 上正确验证并且预计可以工作,但实际上什么也没显示。

这是来源http://jsfiddle.net/kuydB/

这是 JavaScript 源代码:

var filterStrength = 20;
var frameTime = 0, lastLoop = new Date(), thisLoop;
var canvas, ctx;


function startWatch() {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    var currentDateTime = new Date();

    var hours = currentDateTime.getHours();
    if (hours < 10) {
        hours = "0" + hours;
    }

    var minutes = currentDateTime.getMinutes();
    if (minutes < 10) {
        minutes = "0" + minutes;
    }

    var seconds = currentDateTime.getSeconds();
    if (seconds < 10) {
        seconds = "0" + seconds;
    }

    var timeString = hours + ':' + minutes + ':' + seconds;

    ctx.font = 'bold ' + canvas.width / 4 + 'px sans-serif';

    var dim = ctx.measureText(timeString);

    var y = (canvas.height - 30) / 2;
    var x = (canvas.width - dim.width) / 2;

    ctx.fillText(timeString, x, y);   

      var thisFrameTime = (thisLoop=new Date()) - lastLoop;
      frameTime+= (thisFrameTime - frameTime) / filterStrength;
      lastLoop = thisLoop;

}

$(document).ready(function() {
    canvas = $('#canvas')[0];
    ctx = canvas.getContext('2d');
    setIntervalTimer(startWatch,1000);
    var fpsOut = document.getElementById('fps');
    setInterval(function(){
      fpsOut.innerHTML = (1000/frameTime).toFixed(1) + " fps";
    },1000);
});

最佳答案

Working Fiddle

var filterStrength = 20;
var frameTime = 0, lastLoop = new Date(), thisLoop;
var canvas, ctx;


function startWatch() {
    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    var currentDateTime = new Date();

    var hours = currentDateTime.getHours();
    if (hours < 10) {
        hours = "0" + hours;
    }

    var minutes = currentDateTime.getMinutes();
    if (minutes < 10) {
        minutes = "0" + minutes;
    }

    var seconds = currentDateTime.getSeconds();
    if (seconds < 10) {
        seconds = "0" + seconds;
    }

    var timeString = hours + ':' + minutes + ':' + seconds;

    ctx.font = 'bold ' + canvas.width / 4 + 'px sans-serif';

    var dim = ctx.measureText(timeString);

    var y = (canvas.height - 30) / 2;
    var x = (canvas.width - dim.width) / 2;

    ctx.fillText(timeString, x, y);   

      var thisFrameTime = (thisLoop=new Date()) - lastLoop;
      frameTime+= (thisFrameTime - frameTime) / filterStrength;
      lastLoop = thisLoop;

}

$(document).ready(function() {
    canvas = $('#canvas')[0];
    ctx = canvas.getContext('2d');
    setInterval(startWatch,1000); //here you had a syntax error you typed setIntervalTimer instead of setInterval
    var fpsOut = document.getElementById('fps');
    setInterval(function(){
      fpsOut.innerHTML = (1000/frameTime).toFixed(1) + " fps";
    }, 1000);
});
​

关于javascript - HTML5数字时钟动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14108359/

相关文章:

javascript - 图像合成后添加文本,html5 canvas

javascript - 如何在 jquery 中查找具有相同类名的嵌套 div?

php - 单击“返回”时,客户的 CC 双倍收费

javascript - 使用 clearInterval() 时出错

javascript - 我如何使用 JSPDF 忽略 div which style = none

javascript - Bootstrap 4 字段在表单组内联中未正确排列

javascript - 折叠 Div 内的表单未提交

HTML5 Canvas 背景图片

javascript - 如何使用jquery缩放相同质量的图像?

javascript - HTML5 Canvas 上的drawImage 不起作用