gpt4 book ai didi

javascript - 绕另一个旋转圆旋转一个圆

转载 作者:行者123 更新时间:2023-12-03 00:41:31 24 4
gpt4 key购买 nike

我在 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 中,第一个圆的中心圆圈在整个旋转过程中似乎没有改变。那么如何围绕旋转的第一个圆旋转第二个圆呢?

提前致谢。

最佳答案

终于找到解决办法了。我添加了<g>围绕第二个圆圈标记。我添加了相同的animateTransform对于<g>并使其绕中心点并在 <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);

关于javascript - 绕另一个旋转圆旋转一个圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53444921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com