html - 带缓动功能的动画 Canvas 弧线

标签 html animation canvas clock easing

我正在用 Canvas 绘制一个非传统的响钟。时间由秒环、秒针、分钟环和小时环表示。我正在使用 webkit/mozRequestAnimationFrame 在适当的时间绘制。我想修改第二个环以快速动画到下一秒(125 毫秒 - 250 毫秒)并使用二次缓动(而不是那种可怕的快照)。

很像 Raphael JS Clock 的第二个响铃动画,除了它使用不同的缓动:http://raphaeljs.com/polar-clock.html

JS Fiddle 链接(必须在 Chrome、Firefox 或 Webkit Nightly 中查看):

  1. fiddle :http://jsfiddle.net/thecrypticace/qmwJx/

  2. 全屏 fiddle : http://jsfiddle.net/thecrypticace/qmwJx/embedded/result/

非常感谢任何帮助!

这很接近,但仍然非常生涩:

var startValue;
if (milliseconds < 500) {
    if (!startValue) startValue = milliseconds;
    if (milliseconds - startValue <= 125) {
        animatedSeconds = seconds - 0.5 + Math.easeIn(milliseconds - startValue, startValue, 1000 - startValue, 125)/1000;
    } else {
        animatedSeconds = seconds;
    }
    drawRing(384, 384, 384, 20, animatedSeconds / 60, 3 / 2 * Math.PI, false);
} else {
    drawRing(384, 384, 384, 20, seconds / 60, 3 / 2 * Math.PI, false);        
    startValue = 0;
}

最佳答案

这是一门数学课:

drawRing(384, 384, 384, 20, seconds / 60, 3 / 2 * Math.PI, false);

这是绘制秒圈的线。所以问题是,在任何给定的时刻,你都有 34/60、35/60 等等。这意味着你的秒圈是 60/60,因此不使用毫秒,而是每秒绘制一次。

线性缓动解决方案:让你的秒循环 60 000/60 000 -> 60 秒,每次 1000 毫秒。和数学:

drawRing(384, 384, 384, 20, ((seconds*1000)+milliseconds) / 60000, 3 / 2 * Math.PI, false);

In Out Quadric 解决方案或选择一个 these :

Math.easeInOutQuad = function (t, b, c, d) {
    t /= d/2;
    if (t < 1) return c/2*t*t + b;
    t--;
    return -c/2 * (t*(t-2) - 1) + b;
};

我优化并更改了您的代码:

//+1 animation happens before the second hand
//-1 animation happens after the second hand
animatedSeconds = seconds+1;
if (milliseconds > 10) {
    if (!startValue) { startValue = milliseconds; }
    if (milliseconds - startValue <= 100) {
        animatedSeconds -= -0.5+ Math.easeInOutQuad(milliseconds - startValue, startValue, 1000 - startValue, 125) / 1000;
    }
} else {
    startValue = 0;
}
drawRing(384, 384, 384, 20, animatedSeconds / 60, 3 / 2 * Math.PI, false);

希望这就是您要找的。

关于html - 带缓动功能的动画 Canvas 弧线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7465052/

相关文章:

javascript - 使用 HTML Canvas 时遇到的问题 & Greasemonkey 无法在 2D 上下文中绘制 Image()

javascript - 通过 CSS 与 HTML5 Canvas 在网页上移动图像的性能

javascript - IE Canvas dataURI 安全错误

html - 证明其他元素而不是文本

html - 如何使图像完全适合 div?

javascript - 动画中的相对旋转

Android:如何停止在 ImageView 上应用的无限动画?

javascript - <div> 标签位于 <table> 标签之外

javascript - 使用 hide() 隐藏元素会导致页面 "jump"

html - 向前和向后的 SVG 动画