代码之家  ›  专栏  ›  技术社区  ›  mimelaine

p5.js-从相关时间的两个点设置一条线的动画

  •  0
  • mimelaine  · 技术社区  · 5 年前

    我正在尝试设置从一点到另一点的线的动画。我想设置动画持续时间,例如10分钟,使用变量传递行程持续时间:

    let p1_x = 200;
    let p1_y = 200;
    let p2_x = 200;
    let p2_y = 300;
    let run_x = p1_x;
    let run_y = p1_y;
    
    
    function setup() {
      createCanvas(400, 400);
    }
    
    function draw() {
      background(220);
      noFill();
      stroke(0);
      point(p1_x, p1_y);
      point(p2_x, p2_y);
      let d = int(dist(p1_x, p1_y, p2_x, p2_y));
      stroke(0, 153, 255);
      
      if(run_x < p2_x) {
        run_x = run_x + 0.1;
      } else if(run_y < p2_y) {
        run_y = run_y + 0.1;
      }
      
      line(p1_x, p1_y, run_x, run_y);
    }
    <script src="https://cdn.jsdelivr.net/npm/p5@1.2.0/lib/p5.js"></script>
    0 回复  |  直到 5 年前
        1
  •  0
  •   user15302406    5 年前

        let p1_x = 200;
        let p1_y = 200;
        let p2_x = 200;
        let p2_y = 300;
        let run_x = p1_x;
        let run_y = p1_y;
        let timer = 600;
        let d = int(dist(p1_x, p1_y, p2_x, p2_y));
        let t = 0;    
    
        function setup() {
          createCanvas(400, 400);
          frameRate(10);
    
        }
        
     function draw()
    {
          background(220);
          noFill();
          stroke(0);
          point(p1_x, p1_y);
          point(p2_x, p2_y);
          stroke(0, 153, 255);
          
          if(run_x < p2_x) {
            run_x = run_x + 0.1;
          } else if(run_y < p2_y) {
            run_y = run_y+(d/timer);
          }
          
          line(p1_x, p1_y, run_x, run_y);
      
          if(t>=timer)
            {
              noLoop();
              console.log("Works")
            }
          t++;
        }