topical media & game development

talk show tell print

#javascript-processing-example-basic-form-simplecurves.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>SimpleCurves</h2>
  
  <p>Simple curves are drawn with simple equations. 
  By using numbers with values between 0 and 1 in
  the equations, a series of elegant curves
  are created. The numbers are then scaled to fill the screen.</p>
  
  <p><a href="http://processing.org/learning/basics/simplecurves.html"><b>Original Processing.org Example:</b> SimpleCurves</a><br>
  <script type="application/processing">
  void setup() {
    size(200, 200);
    colorMode(RGB, 100);
    background(0);
    noFill();
    noLoop();
  }
  
  void draw() {
    stroke(40);
    beginShape();
    for(int i=0; i<width; i++) {
     vertex(i, singraph((float)i/width)*height);
    }
    endShape();
    
    stroke(55);
    beginShape();
    for(int i=0; i<width; i++) {
     vertex(i, quad((float)i/width)*height);
    }
    endShape();
    
    stroke(70);
    beginShape();
    for(int i=0; i<width; i++) {
     vertex(i, quadHump((float)i/width)*height);
    }
    endShape();
    
    stroke(85);
    beginShape();
    for(int i=0; i<width; i++) {
     vertex(i, hump((float)i/width)*height);
    }
    endShape();
    
    stroke(100);
    beginShape();
    for(int i=0; i<width; i++) {
     vertex(i, squared((float)i/width)*height);
    }
    endShape();
  }
  
  float singraph(float sa) {
    sa = (sa - 0.5) * 1.0; //scale from -1 to 1
    sa = sin(sa*PI)/2 + 0.5;
    return sa;
  }
  
  float quad(float sa) {
    return sa*sa*sa*sa;
  }
  
  float quadHump(float sa) {
    sa = (sa - 0.5); //scale from -2 to 2
    sa = sa*sa*sa*sa * 16;
    return sa;
  }
  
  float hump(float sa) {
    sa = (sa - 0.5) * 2; //scale from -2 to 2
    sa = sa*sa;
    if(sa > 1) { sa = 1; }
    return 1-sa;
  }
  
  float squared(float sa) {
    sa = sa*sa;
    return sa;
  }
  </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>
  void setup() {
    size(200, 200);
    colorMode(RGB, 100);
    background(0);
    noFill();
    noLoop();
  }
  
  void draw() {
    stroke(40);
    beginShape();
    for(int i=0; i&lt;width; i++) {
     vertex(i, singraph((float)i/width)*height);
    }
    endShape();
    
    stroke(55);
    beginShape();
    for(int i=0; i&lt;width; i++) {
     vertex(i, quad((float)i/width)*height);
    }
    endShape();
    
    stroke(70);
    beginShape();
    for(int i=0; i&lt;width; i++) {
     vertex(i, quadHump((float)i/width)*height);
    }
    endShape();
    
    stroke(85);
    beginShape();
    for(int i=0; i&lt;width; i++) {
     vertex(i, hump((float)i/width)*height);
    }
    endShape();
    
    stroke(100);
    beginShape();
    for(int i=0; i&lt;width; i++) {
     vertex(i, squared((float)i/width)*height);
    }
    endShape();
  }
  
  float singraph(float sa) {
    sa = (sa - 0.5) * 1.0; //scale from -1 to 1
    sa = sin(sa*PI)/2 + 0.5;
    return sa;
  }
  
  float quad(float sa) {
    return sa*sa*sa*sa;
  }
  
  float quadHump(float sa) {
    sa = (sa - 0.5); //scale from -2 to 2
    sa = sa*sa*sa*sa * 16;
    return sa;
  }
  
  float hump(float sa) {
    sa = (sa - 0.5) * 2; //scale from -2 to 2
    sa = sa*sa;
    if(sa &gt; 1) { sa = 1; }
    return 1-sa;
  }
  
  float squared(float sa) {
    sa = sa*sa;
    return sa;
  }</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.