topical media & game development

talk show tell print

#javascript-processing-example-basic-transform-translate.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>Translate</h2>
  
  <p>The translate() function allows objects to be moved
  to any location within the window. The first parameter
  sets the x-axis offset and the second parameter sets the
  y-axis offset.</p>
  
  <p><a href="http://processing.org/learning/basics/translate.html"><b>Original Processing.org Example:</b> Translate</a><br>
  <script type="application/processing">
  float x, y;
  float s = 40.0;
  
  void setup() 
  {
    size(200,200);
    noStroke();
    frameRate(30);
  }
  
  void draw() 
  {
    background(102);
    
    x = x + 0.8;
    
    if (x > width + s) {
      x = -s;
    } 
    
    translate(x, height/2-s/2);
    fill(255);
    rect(-s/2, -s/2, s, s);
    
    // Transforms accumulate.
    // Notice how this rect moves twice
    // as fast as the other, but it has
    // the same parameter for the x-axis value
    translate(x, s);
    fill(0);
    rect(-s/2, -s/2, s, s);
  }
  </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 x, y;
  float s = 40.0;
  
  void setup() 
  {
    size(200,200);
    noStroke();
    frameRate(30);
  }
  
  void draw() 
  {
    background(102);
    
    x = x + 0.8;
    
    if (x &gt; width + s) {
      x = -s;
    } 
    
    translate(x, height/2-s/2);
    fill(255);
    rect(-s/2, -s/2, s, s);
    
    // Transforms accumulate.
    // Notice how this rect moves twice
    // as fast as the other, but it has
    // the same parameter for the x-axis value
    translate(x, s);
    fill(0);
    rect(-s/2, -s/2, s, s);
  }</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.