过渡持续时间不均匀。

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

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

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

<h2>示例 1</h2>

在此示例中,每当球碰到屏幕边缘时,它都会改变方向(请查看注释)。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
var ball = document.getElementById("ball");
var frameRate = 60;

// 设置球的初始位置
var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
ball.style.top = currentTop + "px";
ball.style.left = currentLeft + "px";

// 设置球的初始方向和速度
var directionX = 1; // 向右为1,向左为-1
var directionY = 1; // 向下为1,向上为-1
var speedX = 2; // 根据需要调整速度
var speedY = 2; // 根据需要调整速度

const updateBallPosition = () => {
  // 计算球的新位置
  var newTop = currentTop + speedY * directionY;
  var newLeft = currentLeft + speedX * directionX;

  // 检查球是否碰到屏幕边界
  var maxHeight = window.innerHeight - ball.offsetHeight;
  var maxWidth = window.innerWidth - ball.offsetWidth;

  // 如果球碰到了顶部/底部或左侧/右侧,则改变方向
  newTop < 0 || newTop > maxHeight ? directionY *= -1 : newLeft < 0 || newLeft > maxWidth ? directionX *= -1 : "";

  // 更新球的位置
  ball.style.top = newTop + "px";
  ball.style.left = newLeft + "px";

  // 更新当前位置
  currentTop = newTop;
  currentLeft = newLeft;

  // 调用下一帧
  requestAnimationFrame(updateBallPosition);
}

// 启动动画
updateBallPosition();

<!-- 语言:lang-css -->
#ball {
  position: absolute;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: #ff6384;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

<!-- 语言:lang-html -->
<div id="ball"></div>

<!-- 结束代码片段 -->
<h2>示例 2</h2>

在此示例中,球的移动受到屏幕的限制,方向随机改变。当球到达屏幕的顶部或底部时,沿Y轴的方向将被反转。同样,当球到达屏幕的左侧或右侧边缘时,沿X轴的方向也会被反转(请查看注释)。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
var ball = document.getElementById("ball");
var frameRate = 60;
var changeDirectionInterval = 2000;

var minSpeed = 0.5; // 调整最小速度
var maxSpeed = 3; // 调整最大速度

// 设置球的初始位置
var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
ball.style.top = currentTop + "px";
ball.style.left = currentLeft + "px";

// 设置球的初始方向和速度
var directionX = Math.random() > 0.5 ? 1 : -1; // 随机设置初始X方向
var directionY = Math.random() > 0.5 ? 1 : -1; // 随机设置初始Y方向
var speedX = Math.random() * (maxSpeed - minSpeed) + minSpeed;
var speedY = Math.random() * (maxSpeed - minSpeed) + minSpeed;

const updateBallPosition = () => {
  // 计算球的新位置
  var newTop = currentTop + speedY * directionY;
  var newLeft = currentLeft + speedX * directionX;

  // 检查球是否碰到屏幕边界
  var maxHeight = window.innerHeight - ball.offsetHeight;
  var maxWidth = window.innerWidth - ball.offsetWidth;

  // 检查球是否沿Y轴越界
  if (newTop < 0) {
    directionY = 1; // 向上反转方向
    newTop = 0; // 保持球在顶部边界内
  } else if (newTop > maxHeight) {
    directionY = -1; // 向下反转方向
    newTop = maxHeight; // 保持球在底部边界内
  }

  // 检查球是否沿X轴越界
  if (newLeft < 0) {
    directionX = 1; // 向右反转方向
    newLeft = 0; // 保持球在左边界内
  } else if (newLeft > maxWidth) {
    directionX = -1; // 向左反转方向
    newLeft = maxWidth; // 保持球在右边界内
  }

  // 更新球的位置
  ball.style.top = newTop + "px";
  ball.style.left = newLeft + "px";

  // 检查是否改变方向的时间到了
  if (Math.random() < frameRate / changeDirectionInterval) {
    // 随机改变X轴方向
    directionX = Math.random() > 0.5 ? 1 : -1;

    // 随机改变Y轴方向
    directionY = Math.random() > 0.5 ? 1 : -1;
  }

  // 更新当前位置
  currentTop = newTop;
  currentLeft = newLeft;

  // 调用下一帧
  requestAnimationFrame(updateBallPosition);
}

// 启动动画
updateBallPosition();

<!-- 语言:lang-css -->
#ball {
  position: absolute;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: #ff6384;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

<!-- 语言:lang-html -->
<div id="ball"></

<details>
<summary>英文:</summary>

&lt;h2&gt;Example 1&lt;/h2&gt;

In this example, the ball changes directions whenever it hits the border of the screen (Check the comments).

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    var ball = document.getElementById(&quot;ball&quot;);
    var frameRate = 60;

    // Set the initial position of the ball
    var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
    var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
    ball.style.top = currentTop + &quot;px&quot;;
    ball.style.left = currentLeft + &quot;px&quot;;

    // Set the initial direction and speed of the ball
    var directionX = 1; // 1 for right, -1 for left
    var directionY = 1; // 1 for down, -1 for up
    var speedX = 2; // Adjust the speed as desired
    var speedY = 2; // Adjust the speed as desired

    const updateBallPosition = () =&gt; {
      // Calculate the new position of the ball
      var newTop = currentTop + speedY * directionY;
      var newLeft = currentLeft + speedX * directionX;

      // Check if the ball hits the screen boundaries
      var maxHeight = window.innerHeight - ball.offsetHeight;
      var maxWidth = window.innerWidth - ball.offsetWidth;
      
      // Reverse the direction if the ball hits the top/bottom or left/right
      newTop &lt; 0 || newTop &gt; maxHeight ? directionY *= -1 : newLeft &lt; 0 || newLeft &gt; maxWidth ? directionX *= -1 : &quot;&quot;;

      // Update the position of the ball
      ball.style.top = newTop + &quot;px&quot;;
      ball.style.left = newLeft + &quot;px&quot;;

      // Update the current position
      currentTop = newTop;
      currentLeft = newLeft;

      // Call the next frame
      requestAnimationFrame(updateBallPosition);
    }

    // Start the animation
    updateBallPosition();

&lt;!-- language: lang-css --&gt;

    #ball {
      position: absolute;
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: #ff6384;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

&lt;!-- language: lang-html --&gt;

    &lt;div id=&quot;ball&quot;&gt;&lt;/div&gt;

&lt;!-- end snippet --&gt;


&lt;h2&gt;Example 2&lt;/h2&gt;

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).

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    var ball = document.getElementById(&quot;ball&quot;);
    var frameRate = 60; 
    var changeDirectionInterval = 2000; 

    var minSpeed = 0.5; // Adjust the minimum speed
    var maxSpeed = 3; // Adjust the maximum speed

    // Set the initial position of the ball
    var currentTop = Math.random() * (window.innerHeight - ball.offsetHeight);
    var currentLeft = Math.random() * (window.innerWidth - ball.offsetWidth);
    ball.style.top = currentTop + &quot;px&quot;;
    ball.style.left = currentLeft + &quot;px&quot;;

    // Set the initial direction and speed of the ball
    var directionX = Math.random() &gt; 0.5 ? 1 : -1; // Randomly set initial X direction
    var directionY = Math.random() &gt; 0.5 ? 1 : -1; // Randomly set initial Y direction
    var speedX = Math.random() * (maxSpeed - minSpeed) + minSpeed;
    var speedY = Math.random() * (maxSpeed - minSpeed) + minSpeed;

    const updateBallPosition = () =&gt; {
      // Calculate the new position of the ball
      var newTop = currentTop + speedY * directionY;
      var newLeft = currentLeft + speedX * directionX;

      // Check if the ball hits the screen boundaries
      var maxHeight = window.innerHeight - ball.offsetHeight;
      var maxWidth = window.innerWidth - ball.offsetWidth;

      // Check if the ball is going out of bounds along the Y-axis
      if (newTop &lt; 0) {
        directionY = 1; // Reverse the direction upward
        newTop = 0; // Keep the ball within the top boundary
      } else if (newTop &gt; maxHeight) {
        directionY = -1; // Reverse the direction downward
        newTop = maxHeight; // Keep the ball within the bottom boundary
      }

      // Check if the ball is going out of bounds along the X-axis
      if (newLeft &lt; 0) {
        directionX = 1; // Reverse the direction towards the right
        newLeft = 0; // Keep the ball within the left boundary
      } else if (newLeft &gt; maxWidth) {
        directionX = -1; // Reverse the direction towards the left
        newLeft = maxWidth; // Keep the ball within the right boundary
      }

      // Update the position of the ball
      ball.style.top = newTop + &quot;px&quot;;
      ball.style.left = newLeft + &quot;px&quot;;

      // Check if it&#39;s time to change direction
      if (Math.random() &lt; frameRate / changeDirectionInterval) {
        // Randomly change direction along the X-axis
        directionX = Math.random() &gt; 0.5 ? 1 : -1;

        // Randomly change direction along the Y-axis
        directionY = Math.random() &gt; 0.5 ? 1 : -1;
      }

      // Update the current position
      currentTop = newTop;
      currentLeft = newLeft;

      // Call the next frame
      requestAnimationFrame(updateBallPosition);
    }

    // Start the animation
    updateBallPosition();

&lt;!-- language: lang-css --&gt;

    #ball {
      position: absolute;
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background-color: #ff6384;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

&lt;!-- language: lang-html --&gt;

    &lt;div id=&quot;ball&quot;&gt;&lt;/div&gt;

&lt;!-- end snippet --&gt;



</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:

确定