【问题标题】:Rotate circle around another rotating circle围绕另一个旋转圆圈旋转圆圈
【发布时间】:2019-04-25 23:05:05
【问题描述】:

我在 svg 中有一个围绕给定点旋转的圆圈。我又加了一个圈。我想围绕第一个旋转圆旋转第二个圆。我怎样才能做到这一点?到目前为止,我能够围绕一个点旋转第一个圆圈。第二个圆圈正在旋转,但不是围绕第一个旋转的圆圈。我正在分享我的代码:

firstCircle= document.createElementNS(namespace, "circle");
firstCircle.setAttributeNS(null, "id", "firstCircle");
firstCircle.setAttributeNS(null, "cx", 700);
firstCircle.setAttributeNS(null, "cy", 400);
firstCircle.setAttributeNS(null, "r", 30);
firstCircle.setAttributeNS(null, "fill", "#0077BE");
svg.appendChild(firstCircle);

firstCircleAnimate = document.createElementNS(namespace, "animateTransform");
firstCircleAnimate.setAttributeNS(null, "attributeName", "transform");
firstCircleAnimate.setAttributeNS(null, "type", "rotate");
firstCircleAnimate.setAttributeNS(null, "from", "0 400 400");
firstCircleAnimate.setAttributeNS(null, "to", "360 400 400");
firstCircleAnimate.setAttributeNS(null, "begin", "0s");
firstCircleAnimate.setAttributeNS(null, "dur", "5s");
firstCircleAnimate.setAttributeNS(null, "repeatCount", "indefinite");
firstCircle.appendChild(firstCircleAnimate);

secondCircle= document.createElementNS(namespace, "circle");
secondCircle.setAttributeNS(null, "id", "secondCircle");
secondCircle.setAttributeNS(null, "cx", 760);
secondCircle.setAttributeNS(null, "cy", 400);
secondCircle.setAttributeNS(null, "r", 10);
secondCircle.setAttributeNS(null, "fill", "#D0D5D2");
svg.appendChild(secondCircle);

secondCircleAnimate =  document.createElementNS(namespace, "animateTransform");
secondCircleAnimate.setAttributeNS(null, "attributeName", "transform");
secondCircleAnimate.setAttributeNS(null, "type", "rotate");
secondCircleAnimate.setAttributeNS(null, "from", "0 " + firstCircle.getAttributeNS(null, "cx") + " " + firstCircle.getAttributeNS(null, "cy"));
secondCircleAnimate.setAttributeNS(null, "to", "360 " + firstCircle.getAttributeNS(null, "cx") + " " + firstCircle.getAttributeNS(null, "cy"));
secondCircleAnimate.setAttributeNS(null, "begin", "0s");
secondCircleAnimate.setAttributeNS(null, "dur", "2s");
secondCircleAnimate.setAttributeNS(null, "repeatCount", "indefinite");
secondCircle.appendChild(secondCircleAnimate );

在这里,我将secondCircleAnimate 的 from 和 to 属性设置为 firstCircle 的中心坐标,但是第一个圆本身正在旋转,并且在 DOM 中,第一个圆的中心似乎在整个旋转过程中没有改变.那么如何围绕旋转的第一个圆圈旋转第二个圆圈呢?

提前致谢。

【问题讨论】:

    标签: javascript svg rotation svg-animate


    【解决方案1】:

    终于有办法了。我在第二个圆圈周围添加了一个<g> 标签。我为<g> 添加了相同的animateTransform 并使其围绕中心点和<g> 内部旋转,对于第二个圆圈,我让第二个圆圈围绕第一个圆圈旋转。

    重要的是第一圈的持续时间和第二圈的持续时间应该相等(只是为了使它们的旋转同步)。

    添加的代码是:

    group= document.createElementNS(namespace, "g");
    group.setAttributeNS(null, "id", "group");
    svg.appendChild(group);
    
    groupAnimate=  document.createElementNS(namespace, "animateTransform");
    groupAnimate.setAttributeNS(null, "attributeName", "transform");
    groupAnimate.setAttributeNS(null, "type", "rotate");
    groupAnimate.setAttributeNS(null, "from", "0 400 400");
    groupAnimate.setAttributeNS(null, "to", "360 400 400");
    groupAnimate.setAttributeNS(null, "begin", "0s");
    groupAnimate.setAttributeNS(null, "dur", "5s");
    groupAnimate.setAttributeNS(null, "repeatCount", "indefinite");
    group.appendChild(groupAnimate);
    

    【讨论】:

      【解决方案2】:

      我怀疑您的这部分代码没有动态更新:

      secondCircleAnimate.setAttributeNS(null, "from", "0 " + firstCircle.getAttributeNS(null, "cx") + " " + firstCircle.getAttributeNS(null, "cy"));
      secondCircleAnimate.setAttributeNS(null, "to", "360 " + firstCircle.getAttributeNS(null, "cx") + " " + firstCircle.getAttributeNS(null, "cy"));
      

      我认为参数fromto 在字符串连接后得到它们的静态值,之后不会更新它们的值。

      两种可能的解决方案:

      1. 用 JS:编写函数,计算当前位置 第一圈。
      2. 仅使用 CSS:为两个圆做两个容器,一个嵌套另一个,所以第一个容器围绕给定点旋转,第二个围绕其父元素(第一个容器);

      【讨论】:

      • 感谢您的回复,我使用 JS 方法尝试了您的第二种可能的解决方案,并且成功了。谢谢。
      猜你喜欢
      • 2021-07-30
      • 2011-10-13
      • 2020-10-02
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 2019-03-03
      相关资源
      最近更新 更多