gsap旋转 – 如何避免锐利的过渡

huangapple go评论82阅读模式
英文:

gsap rotation - how to avoid sharp transitions

问题

以下是翻译好的部分:

"Could someone help me make this swing more fluently, without this sharp transitions from one direction to another? The animation should be like a swing on wind."

"有人能帮我使这个摆动更加流畅,而不是这种从一个方向到另一个方向的急剧转变吗?动画应该像风中的摆动一样。"

const stepDuration = 0.5, wing = '#wing';
gsap.set(wing, {transformOrigin: "50% 0%", rotation: 15})
const walk = () => {gsap.timeline({repeat: -1, defaults: { ease: "circ.inOut", duration: stepDuration }})
    .add('start')
    .to(wing, { rotation: -15 })
    .to(wing, { rotation: 15 })
}
window.onload = () => {walk()};
svg {
  position: fixed;
  left: 50vw;
  top: 25px;
  width: 70%;
  height: 70%;
  animation: a01 9s;
  background: #0099cc;
}

#wing {
  fill: gold;
  transform-origin: top center;
  animation: awinga 25s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.0.4/gsap.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MotionPathPlugin.min.js?v=15"></script>

<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" version="1.1" viewBox="0 0 1280 720">
  <path id='wing' d="M100 0 L150 0 L150 300 L100 300 Z" />
</svg>

<script>

</script>
英文:

Could someone help me make this swing more fluently, without this sharp transitions from one direction to another? The animation should be like a swing on wind.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const 	stepDuration = 0.5, wing = &#39;#wing&#39;;
gsap.set(wing, {transformOrigin: &quot;50% 0%&quot;, rotation: 15})
const walk = () =&gt; {gsap.timeline({repeat: -1, defaults: { ease: &quot;circ.inOut&quot;, duration: stepDuration }})
    .add(&#39;start&#39;)
	.to(wing, { rotation: -15 })
    .to(wing, { rotation: 15 })
}
window.onload = () =&gt; {walk()};

<!-- language: lang-css -->

svg {
  position: fixed;
  left: 50vw;
  top: 25px;
  width: 70%;
  height: 70%;
  animation: a01 9s;
  background: #0099cc;
}

#wing {
  fill: gold;
  transform-origin: top center;
  animation: awinga 25s;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/gsap/3.0.4/gsap.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MotionPathPlugin.min.js?v=15&quot;&gt;&lt;/script&gt;

   &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xml:space=&quot;preserve&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 1280 720&quot;&gt;
      &lt;path id=&#39;wing&#39; d=&quot;M100 0 L150 0 L150 300 L100 300 Z&quot; /&gt;
    &lt;/svg&gt;

&lt;script&gt;

&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 2

你所需的基本上是一个缓动/缓和函数,它在开始和结束时逐渐减小,中间没有陡峭的梯度。你的问题出在于 circ.inOut 缓动函数,在中间有一个无穷小的时间段,梯度变为无限(正好在50%标记处),这导致了在中间阶段出现尖锐的 jerking(即在你的元素旋转超过垂直中线时看到的情况):

因此,你应该选择一个缓动函数,它在中间没有突然的变化,比如 power1.inOut 或类似的函数:

const stepDuration = 0.5, wing = '#wing';
gsap.set(wing, { transformOrigin: "50% 0%", rotation: 15 })
const walk = () => { gsap.timeline({ repeat: -1, defaults: { ease: "power1.inOut", duration: stepDuration } })
    .add('start')
    .to(wing, { rotation: -15 })
    .to(wing, { rotation: 15 })
}
window.onload = () => { walk() };
svg {
  position: fixed;
  left: 50vw;
  top: 25px;
  width: 70%;
  height: 70%;
  animation: a01 9s;
  background: #0099cc;
}

#wing {
  fill: gold;
  transform-origin: top center;
  animation: awinga 25s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.0.4/gsap.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MotionPathPlugin.min.js?v=15"></script>

<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" version="1.1" viewBox="0 0 1280 720">
  <path id='wing' d="M100 0 L150 0 L150 300 L100 300 Z" />
</svg>
英文:

What you want is basically a tweening/easing function that tapers out at the start and at the end, without a sharp gradient in between. The reason of your jerking is because for the circ.inOut easing, there is basically an infinitesimally small period of time where the gradient is infinite (right at the 50% mark), which causes the sharp jerking you see when it's mid-tween (right when your element rotates past the vertical midline):

gsap旋转 – 如何避免锐利的过渡

Therefore, you should pick an easing function that doesn't have that abrupt change in the middle, e.g. power1.inOut or the likes of it:

[gsap旋转 – 如何避免锐利的过渡

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const 	stepDuration = 0.5, wing = &#39;#wing&#39;;
gsap.set(wing, {transformOrigin: &quot;50% 0%&quot;, rotation: 15})
const walk = () =&gt; {gsap.timeline({repeat: -1, defaults: { ease: &quot;power1.inOut&quot;, duration: stepDuration }})
    .add(&#39;start&#39;)
	.to(wing, { rotation: -15 })
    .to(wing, { rotation: 15 })
}
window.onload = () =&gt; {walk()};

<!-- language: lang-css -->

svg {
  position: fixed;
  left: 50vw;
  top: 25px;
  width: 70%;
  height: 70%;
  animation: a01 9s;
  background: #0099cc;
}

#wing {
  fill: gold;
  transform-origin: top center;
  animation: awinga 25s;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/gsap/3.0.4/gsap.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MotionPathPlugin.min.js?v=15&quot;&gt;&lt;/script&gt;

   &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; xml:space=&quot;preserve&quot; version=&quot;1.1&quot; viewBox=&quot;0 0 1280 720&quot;&gt;
      &lt;path id=&#39;wing&#39; d=&quot;M100 0 L150 0 L150 300 L100 300 Z&quot; /&gt;
    &lt;/svg&gt;

&lt;script&gt;

&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 14:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定