topical media & game development

talk show tell print

#javascript-processing-example-topic-motion-movingoncurves.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>MovingOnCurves</h2>
  
  <p>In this example, the circles moves along the curve y = x^4.
  Click the mouse to have it move to a new position.</p>
  
  <p><a href="http://processing.org/learning/topics/movingoncurves.html"><b>Original Processing.org Example:</b> MovingOnCurves</a><br>
  <script type="application/processing">
  float beginX = 20.0;  // Initial x-coordinate
  float beginY = 10.0;  // Initial y-coordinate
  float endX = 170.0;   // Final x-coordinate
  float endY = 180.0;   // Final y-coordinate
  float distX;          // X-axis distance to move
  float distY;          // Y-axis distance to move
  float exponent = 4;   // Determines the curve
  float x = 0.0;        // Current x-coordinate
  float y = 0.0;        // Current y-coordinate
  float step = 0.01;    // Size of each step along the path
  float pct = 0.0;      // Percentage traveled (0.0 to 1.0)
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    smooth();
    distX = endX - beginX;
    distY = endY - beginY;
  }
  
  void draw() 
  {
    fill(0, 2);
    rect(0, 0, width, height);
    pct += step;
    if (pct < 1.0) {
      x = beginX + (pct * distX);
      y = beginY + (pow(pct, exponent) * distY);
    }
    fill(255);
    ellipse(x, y, 20, 20);
  }
  
  void mousePressed() {
    pct = 0.0;
    beginX = x;
    beginY = y;
    endX = mouseX;
    endY = mouseY;
    distX = endX - beginX;
    distY = endY - beginY;
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  float beginX = 20.0;  // Initial x-coordinate
  float beginY = 10.0;  // Initial y-coordinate
  float endX = 170.0;   // Final x-coordinate
  float endY = 180.0;   // Final y-coordinate
  float distX;          // X-axis distance to move
  float distY;          // Y-axis distance to move
  float exponent = 4;   // Determines the curve
  float x = 0.0;        // Current x-coordinate
  float y = 0.0;        // Current y-coordinate
  float step = 0.01;    // Size of each step along the path
  float pct = 0.0;      // Percentage traveled (0.0 to 1.0)
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    smooth();
    distX = endX - beginX;
    distY = endY - beginY;
  }
  
  void draw() 
  {
    fill(0, 2);
    rect(0, 0, width, height);
    pct += step;
    if (pct &lt; 1.0) {
      x = beginX + (pct * distX);
      y = beginY + (pow(pct, exponent) * distY);
    }
    fill(255);
    ellipse(x, y, 20, 20);
  }
  
  void mousePressed() {
    pct = 0.0;
    beginX = x;
    beginY = y;
    endX = mouseX;
    endY = mouseY;
    distX = endX - beginX;
    distY = endY - beginY;
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.