过渡持续时间不均匀。

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

Transition duration not being homogeneous

问题

我正在制作一个小游戏,其中一个球在屏幕上移动,使用样式.top = Math.random()以及样式.left也是如此。然而,当我指定过渡持续时间时,起初它会非常快速地加速,然后移动到指定位置,然后会非常缓慢地减速。我希望球的速度在整个移动过程中保持一致。

有任何想法吗?我正在寻找一种属性来实现这一点。

英文:

Im working on a small game where a ball moves over the screen, using style.top = Math.random() and the same with style.left. However, when I specify the transition duration, at first it accelerates really quickly, and then moves to the specified place, and then really slowly slows down. I want the speed of the ball be the same throughout the whole movement.

Any ideas? Im looking for some sort of property to achieve this

答案1

得分: 1

听起来你在询问过渡时间函数。更改你使用的函数将改变时间曲线(从慢到快,从快到慢等)。

你可能会想要使用 linear 函数,因为它具有恒定的变化速率。

这是Mozilla关于它的文档链接:https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function

英文:

It sounds like you're asking about transition timing functions. Changing which function you use will change the timing curve (slow to fast, fast to slow, etc).

You're probably going to want the linear function, since that has a constant rate of change.

Here's the Mozilla documentation on it: https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function

答案2

得分: 0

以下是代码部分的中文翻译:

  1. <h2>示例 1</h2>
  2. 在此示例中,每当球碰到屏幕边缘时,它都会改变方向(请查看注释)。
  3. <!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
  4. <!-- 语言:lang-js -->
  5. var ball = document.getElementById("ball");
  6. var frameRate = 60;
  7. // 设置球的初始位置
  8. var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
  9. var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
  10. ball.style.top = currentTop + "px";
  11. ball.style.left = currentLeft + "px";
  12. // 设置球的初始方向和速度
  13. var directionX = 1; // 向右为1,向左为-1
  14. var directionY = 1; // 向下为1,向上为-1
  15. var speedX = 2; // 根据需要调整速度
  16. var speedY = 2; // 根据需要调整速度
  17. const updateBallPosition = () => {
  18. // 计算球的新位置
  19. var newTop = currentTop + speedY * directionY;
  20. var newLeft = currentLeft + speedX * directionX;
  21. // 检查球是否碰到屏幕边界
  22. var maxHeight = window.innerHeight - ball.offsetHeight;
  23. var maxWidth = window.innerWidth - ball.offsetWidth;
  24. // 如果球碰到了顶部/底部或左侧/右侧,则改变方向
  25. newTop < 0 || newTop > maxHeight ? directionY *= -1 : newLeft < 0 || newLeft > maxWidth ? directionX *= -1 : "";
  26. // 更新球的位置
  27. ball.style.top = newTop + "px";
  28. ball.style.left = newLeft + "px";
  29. // 更新当前位置
  30. currentTop = newTop;
  31. currentLeft = newLeft;
  32. // 调用下一帧
  33. requestAnimationFrame(updateBallPosition);
  34. }
  35. // 启动动画
  36. updateBallPosition();
  37. <!-- 语言:lang-css -->
  38. #ball {
  39. position: absolute;
  40. width: 50px;
  41. height: 50px;
  42. border-radius: 50%;
  43. background-color: #ff6384;
  44. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  45. }
  46. <!-- 语言:lang-html -->
  47. <div id="ball"></div>
  48. <!-- 结束代码片段 -->
  1. <h2>示例 2</h2>
  2. 在此示例中,球的移动受到屏幕的限制,方向随机改变。当球到达屏幕的顶部或底部时,沿Y轴的方向将被反转。同样,当球到达屏幕的左侧或右侧边缘时,沿X轴的方向也会被反转(请查看注释)。
  3. <!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
  4. <!-- 语言:lang-js -->
  5. var ball = document.getElementById("ball");
  6. var frameRate = 60;
  7. var changeDirectionInterval = 2000;
  8. var minSpeed = 0.5; // 调整最小速度
  9. var maxSpeed = 3; // 调整最大速度
  10. // 设置球的初始位置
  11. var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
  12. var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
  13. ball.style.top = currentTop + "px";
  14. ball.style.left = currentLeft + "px";
  15. // 设置球的初始方向和速度
  16. var directionX = Math.random() > 0.5 ? 1 : -1; // 随机设置初始X方向
  17. var directionY = Math.random() > 0.5 ? 1 : -1; // 随机设置初始Y方向
  18. var speedX = Math.random() * (maxSpeed - minSpeed) + minSpeed;
  19. var speedY = Math.random() * (maxSpeed - minSpeed) + minSpeed;
  20. const updateBallPosition = () => {
  21. // 计算球的新位置
  22. var newTop = currentTop + speedY * directionY;
  23. var newLeft = currentLeft + speedX * directionX;
  24. // 检查球是否碰到屏幕边界
  25. var maxHeight = window.innerHeight - ball.offsetHeight;
  26. var maxWidth = window.innerWidth - ball.offsetWidth;
  27. // 检查球是否沿Y轴越界
  28. if (newTop < 0) {
  29. directionY = 1; // 向上反转方向
  30. newTop = 0; // 保持球在顶部边界内
  31. } else if (newTop > maxHeight) {
  32. directionY = -1; // 向下反转方向
  33. newTop = maxHeight; // 保持球在底部边界内
  34. }
  35. // 检查球是否沿X轴越界
  36. if (newLeft < 0) {
  37. directionX = 1; // 向右反转方向
  38. newLeft = 0; // 保持球在左边界内
  39. } else if (newLeft > maxWidth) {
  40. directionX = -1; // 向左反转方向
  41. newLeft = maxWidth; // 保持球在右边界内
  42. }
  43. // 更新球的位置
  44. ball.style.top = newTop + "px";
  45. ball.style.left = newLeft + "px";
  46. // 检查是否改变方向的时间到了
  47. if (Math.random() < frameRate / changeDirectionInterval) {
  48. // 随机改变X轴方向
  49. directionX = Math.random() > 0.5 ? 1 : -1;
  50. // 随机改变Y轴方向
  51. directionY = Math.random() > 0.5 ? 1 : -1;
  52. }
  53. // 更新当前位置
  54. currentTop = newTop;
  55. currentLeft = newLeft;
  56. // 调用下一帧
  57. requestAnimationFrame(updateBallPosition);
  58. }
  59. // 启动动画
  60. updateBallPosition();
  61. <!-- 语言:lang-css -->
  62. #ball {
  63. position: absolute;
  64. width: 50px;
  65. height: 50px;
  66. border-radius: 50%;
  67. background-color: #ff6384;
  68. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  69. }
  70. <!-- 语言:lang-html -->
  71. <div id="ball"></
  72. <details>
  73. <summary>英文:</summary>
  74. &lt;h2&gt;Example 1&lt;/h2&gt;
  75. In this example, the ball changes directions whenever it hits the border of the screen (Check the comments).
  76. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  77. &lt;!-- language: lang-js --&gt;
  78. var ball = document.getElementById(&quot;ball&quot;);
  79. var frameRate = 60;
  80. // Set the initial position of the ball
  81. var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
  82. var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
  83. ball.style.top = currentTop + &quot;px&quot;;
  84. ball.style.left = currentLeft + &quot;px&quot;;
  85. // Set the initial direction and speed of the ball
  86. var directionX = 1; // 1 for right, -1 for left
  87. var directionY = 1; // 1 for down, -1 for up
  88. var speedX = 2; // Adjust the speed as desired
  89. var speedY = 2; // Adjust the speed as desired
  90. const updateBallPosition = () =&gt; {
  91. // Calculate the new position of the ball
  92. var newTop = currentTop + speedY * directionY;
  93. var newLeft = currentLeft + speedX * directionX;
  94. // Check if the ball hits the screen boundaries
  95. var maxHeight = window.innerHeight - ball.offsetHeight;
  96. var maxWidth = window.innerWidth - ball.offsetWidth;
  97. // Reverse the direction if the ball hits the top/bottom or left/right
  98. newTop &lt; 0 || newTop &gt; maxHeight ? directionY *= -1 : newLeft &lt; 0 || newLeft &gt; maxWidth ? directionX *= -1 : &quot;&quot;;
  99. // Update the position of the ball
  100. ball.style.top = newTop + &quot;px&quot;;
  101. ball.style.left = newLeft + &quot;px&quot;;
  102. // Update the current position
  103. currentTop = newTop;
  104. currentLeft = newLeft;
  105. // Call the next frame
  106. requestAnimationFrame(updateBallPosition);
  107. }
  108. // Start the animation
  109. updateBallPosition();
  110. &lt;!-- language: lang-css --&gt;
  111. #ball {
  112. position: absolute;
  113. width: 50px;
  114. height: 50px;
  115. border-radius: 50%;
  116. background-color: #ff6384;
  117. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  118. }
  119. &lt;!-- language: lang-html --&gt;
  120. &lt;div id=&quot;ball&quot;&gt;&lt;/div&gt;
  121. &lt;!-- end snippet --&gt;
  122. &lt;h2&gt;Example 2&lt;/h2&gt;
  123. In this example, the ball&#39;s movement is restricted within the screen, changing direction randomly. When the ball reaches the top or bottom of the screen, the direction along the Y-axis is reversed. Similarly, when the ball reaches the left or right edge of the screen, the direction along the X-axis is reversed (Check the comments).
  124. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  125. &lt;!-- language: lang-js --&gt;
  126. var ball = document.getElementById(&quot;ball&quot;);
  127. var frameRate = 60;
  128. var changeDirectionInterval = 2000;
  129. var minSpeed = 0.5; // Adjust the minimum speed
  130. var maxSpeed = 3; // Adjust the maximum speed
  131. // Set the initial position of the ball
  132. var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
  133. var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
  134. ball.style.top = currentTop + &quot;px&quot;;
  135. ball.style.left = currentLeft + &quot;px&quot;;
  136. // Set the initial direction and speed of the ball
  137. var directionX = Math.random() &gt; 0.5 ? 1 : -1; // Randomly set initial X direction
  138. var directionY = Math.random() &gt; 0.5 ? 1 : -1; // Randomly set initial Y direction
  139. var speedX = Math.random() * (maxSpeed - minSpeed) + minSpeed;
  140. var speedY = Math.random() * (maxSpeed - minSpeed) + minSpeed;
  141. const updateBallPosition = () =&gt; {
  142. // Calculate the new position of the ball
  143. var newTop = currentTop + speedY * directionY;
  144. var newLeft = currentLeft + speedX * directionX;
  145. // Check if the ball hits the screen boundaries
  146. var maxHeight = window.innerHeight - ball.offsetHeight;
  147. var maxWidth = window.innerWidth - ball.offsetWidth;
  148. // Check if the ball is going out of bounds along the Y-axis
  149. if (newTop &lt; 0) {
  150. directionY = 1; // Reverse the direction upward
  151. newTop = 0; // Keep the ball within the top boundary
  152. } else if (newTop &gt; maxHeight) {
  153. directionY = -1; // Reverse the direction downward
  154. newTop = maxHeight; // Keep the ball within the bottom boundary
  155. }
  156. // Check if the ball is going out of bounds along the X-axis
  157. if (newLeft &lt; 0) {
  158. directionX = 1; // Reverse the direction towards the right
  159. newLeft = 0; // Keep the ball within the left boundary
  160. } else if (newLeft &gt; maxWidth) {
  161. directionX = -1; // Reverse the direction towards the left
  162. newLeft = maxWidth; // Keep the ball within the right boundary
  163. }
  164. // Update the position of the ball
  165. ball.style.top = newTop + &quot;px&quot;;
  166. ball.style.left = newLeft + &quot;px&quot;;
  167. // Check if it&#39;s time to change direction
  168. if (Math.random() &lt; frameRate / changeDirectionInterval) {
  169. // Randomly change direction along the X-axis
  170. directionX = Math.random() &gt; 0.5 ? 1 : -1;
  171. // Randomly change direction along the Y-axis
  172. directionY = Math.random() &gt; 0.5 ? 1 : -1;
  173. }
  174. // Update the current position
  175. currentTop = newTop;
  176. currentLeft = newLeft;
  177. // Call the next frame
  178. requestAnimationFrame(updateBallPosition);
  179. }
  180. // Start the animation
  181. updateBallPosition();
  182. &lt;!-- language: lang-css --&gt;
  183. #ball {
  184. position: absolute;
  185. width: 50px;
  186. height: 50px;
  187. border-radius: 50%;
  188. background-color: #ff6384;
  189. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  190. }
  191. &lt;!-- language: lang-html --&gt;
  192. &lt;div id=&quot;ball&quot;&gt;&lt;/div&gt;
  193. &lt;!-- end snippet --&gt;
  194. </details>

huangapple
  • 本文由 发表于 2023年6月8日 06:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427579.html
匿名

发表评论

匿名网友

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

确定