topical media & game development

talk show tell print

#javascript-processing-example-basic-math-distance2d.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>Distance2D</h2>
  
  <p>Move the mouse across the image to obscure and reveal the matrix.  
  Measures the distance from the mouse to each square and sets the
  size proportionally.</p>
  
  <p><a href="http://processing.org/learning/basics/distance2d.html"><b>Original Processing.org Example:</b> Distance2D</a><br>
  <script type="application/processing">
  float max_distance;
  
  void setup() {
    size(200, 200); 
    smooth();
    noStroke();
    max_distance = dist(0, 0, width, height);
  }
  
  void draw() 
  {
    background(51);
  
    for(int i = 0; i <= width; i += 20) {
      for(int j = 0; j <= width; j += 20) {
        float size = dist(mouseX, mouseY, i, j);
        size = size/max_distance * 66;
        //opera.postError(i, j, size);
        ellipse(i, j, size, size);
      }
    }
  }
  </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 max_distance;
  
  void setup() {
    size(200, 200); 
    smooth();
    noStroke();
    max_distance = dist(0, 0, width, height);
  }
  
  void draw() 
  {
    background(51);
  
    for(int i = 0; i &lt;= width; i += 20) {
      for(int j = 0; j &lt;= width; j += 20) {
        float size = dist(mouseX, mouseY, i, j);
        size = size/max_distance * 66;
        ellipse(i, j, 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.