topical media & game development

talk show tell print

#javascript-processing-example-topic-motion-bounce.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>Bounce</h2>
  
  <p>When the shape hits the edge of the window, it reverses its direction.</p>
  
  <p><a href="http://processing.org/learning/topics/bounce.html"><b>Original Processing.org Example:</b> Bounce</a><br>
  <script type="application/processing">
  int wsize = 60;       // Width of the shape
  float xpos, ypos;    // Starting position of shape    
  
  float xspeed = 2.8;  // Speed of the shape
  float yspeed = 2.2;  // Speed of the shape
  
  int xdirection = 1;  // Left or Right
  int ydirection = 1;  // Top to Bottom
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    frameRate(30);
    smooth();
    // Set the starting position of the shape
    xpos = width/2;
    ypos = height/2;
  }
  
  void draw() 
  {
    background(102);
    
    // Update the position of the shape
    xpos = xpos + ( xspeed * xdirection );
    ypos = ypos + ( yspeed * ydirection );
    
    // Test to see if the shape exceeds the boundaries of the screen
    // If it does, reverse its direction by multiplying by -1
    if (xpos > width-wsize || xpos < 0) {
      xdirection *= -1;
    }
    if (ypos > height-wsize || ypos < 0) {
      ydirection *= -1;
    }
  
    // Draw the shape
    ellipse(xpos+wsize/2, ypos+wsize/2, wsize, wsize);
  }
  </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>
  int size = 60;       // Width of the shape
  float xpos, ypos;    // Starting position of shape    
  
  float xspeed = 2.8;  // Speed of the shape
  float yspeed = 2.2;  // Speed of the shape
  
  int xdirection = 1;  // Left or Right
  int ydirection = 1;  // Top to Bottom
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    frameRate(30);
    smooth();
    // Set the starting position of the shape
    xpos = width/2;
    ypos = height/2;
  }
  
  void draw() 
  {
    background(102);
    
    // Update the position of the shape
    xpos = xpos + ( xspeed * xdirection );
    ypos = ypos + ( yspeed * ydirection );
    
    // Test to see if the shape exceeds the boundaries of the screen
    // If it does, reverse its direction by multiplying by -1
    if (xpos &gt; width-size || xpos &lt; 0) {
      xdirection *= -1;
    }
    if (ypos &gt; height-size || ypos &lt; 0) {
      ydirection *= -1;
    }
  
    // Draw the shape
    ellipse(xpos+size/2, ypos+size/2, size, size);
  }</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.