weixin_58816567 2023-03-11 01:11 采纳率: 0%
浏览 56
已结题

此代码怎么修改能让小球自由落体下落轨迹

此代码怎么修改能让小球添加下落轨迹:

<!DOCTYPE html>
<html>
  <head>
    <title>自由落体运动仿真</title>
    <style>
      body {
        background-color: #ffffcc; /* 淡黄色的颜色代码 */
      }
      
      #mycanvas {
    display: block;
    margin: auto;
    background-color: white;
  }
  h1 {
    font-size: 32px; /* 设置标题字体大小 */
    font-weight: normal; /* 取消标题的粗体样式 */
    margin-top: 20px; /* 设置标题距离顶部的距离 */
    text-align: center;
  }
  .module {
    margin-top: -200px;
  }
  label {
    display: block;
    margin-bottom: 10px;
  }
  input[type="number"] {
    width: 50px;
    margin-right: 10px;
  }
</style>

  </head>
  <body>
    <h1>基于HTML5小球自由落体运动仿真功能与实现</h1>
    <canvas id="mycanvas" width="600" height="650" style="border:1px solid red;position:relative;"></canvas>

    
<div class="module">
    <label for="elasticity">弹力:</label>
    <input type="number" id="elasticity" name="elasticity" min="0" max="1" step="0.1" value="1" />
  </div>
  
  
  <div class="module">
    <label for="speed">初始速度:</label>
    <input type="number" id="speed" name="speed" min="0" max="100" step="1" value="0" />
  </div>
  
  <div class="module">
    <button id="startButton">开始</button>
    <button id="stopButton">停止</button>
    <button id="resetButton">复位</button>
  </div>
  
  
  <script>
    // 获取画布元素和画布上下文对象
    var canvas = document.getElementById("mycanvas");
    var context = canvas.getContext("2d");
    var animationId = null; // 动画帧编号
  
    // 定义物理参数
    var gravity = 9.8; // 重力加速度
    var airResistance = 0.1; // 空气阻力系数
  
    // 定义小球的初始状态
    var x = canvas.width/2;
    var y = 0;
    var radius = 20;
    var speed = 0;
  
    // 获取弹力的input元素
    var elasticityInput = document.getElementById("elasticity");
    var elasticity = parseFloat(elasticityInput.value);
  
    // 获取速度的input元素
  
    var elasticityInput = document.getElementById("elasticity");
    var elasticity = parseFloat(elasticityInput.value);

  // 定义允许的最小和最大弹力值
    var minElasticity = parseFloat(elasticityInput.getAttribute("min"));
    var maxElasticity = parseFloat(elasticityInput.getAttribute("max"));

  // 更新弹力值的函数
  function updateElasticity() {
// 获取当前输入框的值并转换为浮点数
  var elasticityValue = parseFloat(elasticityInput.value);

// 如果值超出了允许的范围,则将其设置为最小或最大允许值
if (elasticityValue < minElasticity) {
elasticityValue = minElasticity;
} else if (elasticityValue > maxElasticity) {
elasticityValue = maxElasticity;
}

// 更新弹力值
  elasticity = elasticityValue;
}

// 添加弹力值输入框的change事件监听器
  elasticityInput.addEventListener("change", updateElasticity);

// 确保弹力值在页面加载时被初始化
  updateElasticity();

  function draw() {
    // 清空画布
    context.clearRect(0, 0, canvas.width, canvas.height);
    
    // 绘制小球
    context.beginPath();
    context.arc(x, y, radius, 0, Math.PI*2);
    context.fillStyle = "red";
    context.fill();
    context.closePath();
    // 绘制小球速度
    context.font = "16px Arial";
    context.fillStyle = "black";
    context.fillText("速度:" + speed.toFixed(2) + " m/s", canvas.width - 120, 30);

    // 更新小球位置和速度
    speed += gravity - airResistance * speed;
    y += speed;
    
    // 边界检测,如果小球碰到底部则反弹
    if (y + radius > canvas.height) {
      y = canvas.height - radius;
      speed = -elasticity* speed;
          elasticity = parseFloat(elasticityInput.value);

          elasticity = parseFloat(elasticityInput.value); // 重新读取弹力值
        }
        
        // 循环调用自身
        animationId = requestAnimationFrame(draw);
      }


      
      // 开始按键事件处理程序
      var startButton = document.getElementById("startButton");
      startButton.onclick = function() {
        // 重新设置小球初始状态
        x = canvas.width/2;
        y = 0;
        speed = 0;
        
        // 启动动画循环
        animationId = requestAnimationFrame(draw);
      };
      
      // 停止按键事件处理程序
      var stopButton = document.getElementById("stopButton");
      stopButton.onclick = function() {
        if (animationId !== null) {
          cancelAnimationFrame(animationId);
          animationId = null;
        }
      };
      
      // 复位按键事件处理程序
      var resetButton = document.getElementById("resetButton");
      resetButton.onclick = function() {
        // 停止动画循环
        if (animationId !== null) {
          cancelAnimationFrame(animationId);
          animationId = null;
        }
        
        // 将小球返回到初始位置
        x = canvas.width/2;
        y = 0;
        speed = 0;
        
        // 清空画布
        context.clearRect(0, 0, canvas.width, canvas.height);
        
    // 绘制小球
    context.beginPath();
    context.arc(x, y, radius, 0, Math.PI*2);
    context.fillStyle = "red";
    context.fill();
    context.closePath();
  };
</script>
</body>
</html>

  • 写回答

5条回答 默认 最新

  • Taylor 淡定哥 2023-03-11 02:39
    关注

    在画布上绘制一条线条来表示小球的运动路径。在绘制小球之前,用当前的坐标点(x,y)作为路径起点,然后在每次绘制完成后将当前坐标点作为路径的终点,绘制线条。我找了一段你参考一下

    function draw() {
      // 清空画布
      context.clearRect(0, 0, canvas.width, canvas.height);
      
      // 绘制小球轨迹
      context.beginPath();
      context.moveTo(x, y);
      context.strokeStyle = "gray";
      context.lineWidth = 2;
      
      // 绘制小球
      context.arc(x, y, radius, 0, Math.PI*2);
      context.fillStyle = "red";
      context.fill();
      context.closePath();
      
      // 绘制小球速度
      context.font = "16px Arial";
      context.fillStyle = "black";
      context.fillText("速度:" + speed.toFixed(2) + " m/s", canvas.width - 120, 30);
      
      // 更新小球位置和速度
      speed += gravity - airResistance * speed;
      y += speed;
      
      // 绘制轨迹
      context.lineTo(x, y);
      context.stroke();
      
      // 边界检测,如果小球碰到底部则反弹
      if (y + radius > canvas.height) {
        y = canvas.height - radius;
        speed = -elasticity * speed;
      }
      
      // 循环调用自身
      animationId = requestAnimationFrame(draw);
    }
    
    
    

    绘制小球的代码前面添加了绘制路径的代码,使用 context.beginPath() 方法来开始一个新路径,然后使用 context.moveTo() 方法将路径起点移到当前小球坐标,接着在小球绘制后使用 context.lineTo() 方法将路径的终点设为当前坐标,最后使用 context.stroke() 方法将路径绘制出来。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月11日
  • 创建了问题 3月11日

悬赏问题

  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物