0

我有一个使用 Raphael 圆形饼图倒计时 + 数字计时器的组合倒计时计时器,在单击按钮 #Timer60SecStart 时运行。我在这里有点作弊,Raphael 动画与倒数计时器完全分开,但是当通过单击#Timer60SecStart 调用时两者都运行 60 秒并且似乎匹配确定。

我的问题是,每次我调用 drawCircle() 时,它都会添加一个新圆圈,同时将现有圆圈留在那里,因此您最终会在页面上散布多个倒计时圆圈。我想要一个 clearCircle() 函数,在开始绘制新圆之前清除前一个圆。

看了这个答案how-to-properly-remove-a-raphael-svg-element-referenced-in-an-animated-set但无法让 set/item/splice 位与我的工作正在做。

这是我的代码:

//Raphael Start

var archtype = Raphael("canvas", 100, 100);

function drawCircle() {
  var archtype = Raphael("canvas", 100, 100);
  archtype.customAttributes.arc = function (xloc, yloc, value, total, R) {
    var alpha = 360 / total * value,
        a = (90 - alpha) * Math.PI / 180,
        x = xloc + R * Math.cos(a),
        y = yloc - R * Math.sin(a),
        path;
    if (total == value) {
        path = [
            ["M", xloc, yloc - R],
            ["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
        ];
    } else {
        path = [
            ["M", xloc, yloc - R],
            ["A", R, R, 0, +(alpha > 180), 1, x, y]
        ];
    }
    return {
        path: path
    };
  };

  //make an arc at 50,50 with a radius of 30 that grows from 0 to 40 of 100 with a bounce
  var my_arc = archtype.path().attr({
      "stroke": "#f00",
      "stroke-width": 40,
      arc: [50, 50, 0, 100, 30]
  });

  my_arc.animate({
     arc: [50, 50, 100, 100, 30]
  }, 60000);

  set.push(my_arc);
  console.log(set)

} //end drawCircle

function clearCircle() {
  alert("clear Circle");
}

// Countdown Timer Stuff
var longTimer = 0;


$('#Timer60Sec').countdown({until: longTimer, compact:true});


$('#Timer60SecStart').click(function() { 
    longTimer = new Date();
    longTimer.setSeconds(longTimer.getSeconds() + 60.5)
    $('#Timer60Sec').countdown('option',{until: longTimer});
    drawCircle();    
    clearCircle();
}); 

在这里,这一切都是一个方便的 JSFiddle:http: //jsfiddle.net/WDpKP/2/

很高兴为您提供帮助。

4

1 回答 1

1

Remove the tags that hold the arc.

function clearCircle() {
  $('svg').remove();
}

As you are writing your own <svg> tags you can add an id to them if needed and change your code to

function clearCircle() {
  $('svg#youridhere').remove();
}

This will prevent other <svg> tags in your page being removed as well.

于 2013-05-16T12:28:03.160 回答