1

本质上,我正在寻找构建这样的东西:

http://themes.pixelworkshop.fr/?theme=CircularCountdownPlugin

我正在制作一个类似的倒计时时钟,两条弧线相互重叠——一个是一个完整的圆,另一个是另一种颜色的弧线,它位于第一个弧线的顶部,并且会增长和缩小以显示还剩多少时间。(我决定不使用这个实际的插件 b/c 我的时钟在计算和显示时间的方式上会有所不同)

通过重新利用另一个问题的代码:

var redArc = new Kinetic.Shape({
    drawFunc: function (canvas) {
        var ctx = canvas.getContext();
        ctx.beginPath();
        ctx.arc(x, y, r, 0, Math.PI / 2, true);
        canvas.fillStroke(this);
    },
    x: x,
    y: y,
    stroke: 'rgb(255,0,0)',
    strokeWidth: 20,
    offset: [x, y]
});

http://jsfiddle.net/EkG5P/1/

我能够画出我需要的弧线。但我不知道如何为其中一条弧线的变化大小设置动画(增加或缩小“蛇”的长度)(注意这个例子并没有准确地显示我想要做什么,但它很接近足够的 b/c 它准确地显示了我想要动画来增长和缩小的弧的类型)

谢谢你的帮助!

4

1 回答 1

4

此功能会将时钟秒数转换为弧度,您可以在弧度中使用:

// zero seconds (the 12 oclock position)
var startingAngle = secondsToRadians(0);

// get the current seconds and its associated radian angle  
var currentSeconds=new Date().getSeconds();
var endingAngle = secondsToRadians(currentSeconds);

function secondsToRadians(seconds) {
    var degrees=(seconds-15)*6;
    var radians=(degrees * Math.PI)/180;
    return(radians);
}

这是代码和小提琴:http: //jsfiddle.net/m1erickson/BR6TY/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    var centerX = Math.floor(canvas.width / 2);
    var centerY = Math.floor(canvas.height / 2);
    var radius = Math.floor(canvas.width / 3);
    var startingAngle = secondsToRadians(0);

    ctx.fillStyle = "#819FF0";
    ctx.strokeStyle="black";
    ctx.lineWidth=3;
    ctx.font="24px Verdana";

    setInterval(function() {
        // draw the arc about every second
        // set the arc to reflect the actual clock seconds
        drawClockWedge( new Date().getSeconds() );
    }, 1000);

    function drawClockWedge(seconds){

        // clear the canvas
        ctx.save();
        ctx.clearRect(0,0,canvas.width,canvas.height);

        // calculate the seconds (in actual time)
        // and convert to radians that .arc requires
        var endingAngle = secondsToRadians(seconds);

        // draw a closed arc representing elapsed seconds
        ctx.beginPath();
        ctx.moveTo(centerX, centerY);
        ctx.arc(centerX, centerY, radius, startingAngle, endingAngle, false);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();

        // also display the seconds on top
        ctx.fillText("["+seconds+"]",centerX-25,centerY-radius-20);
        ctx.restore();
    }

    function secondsToRadians(seconds) {
        var degrees=(seconds-15)*6;
        var radians=(degrees * Math.PI)/180;
        return(radians);
    }

}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
于 2013-05-24T23:05:52.173 回答