topical media & game development

talk show tell print

#javascript-processing-example-basic-math-noisewave.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>NoiseWave</h2>
  
  <p>by Daniel Shiffman.  
  
  Using Perlin Noise to generate a wave-like pattern.</p>
  
  <p><a href="http://processing.org/learning/basics/noisewave.html"><b>Original Processing.org Example:</b> NoiseWave</a><br>
  <script type="application/processing">
  int xspacing = 8;   // How far apart should each horizontal location be spaced
  int w;              // Width of entire wave
  
  float yoff = 0.0f;        // 2nd dimension of perlin noise
  float[] yvalues;          // Using an array to store height values for the wave (not entirely necessary)
  
  void setup() {
    size(200,200);
    frameRate(30);
    colorMode(RGB,255,255,255,100);
    smooth();
    w = width+16;
    yvalues = new float[w/xspacing];
  }
  
  void draw() {
    background(0);
    calcWave();
    renderWave();
  
  }
  
  void calcWave() {
    float dx = 0.05f;
    float dy = 0.01f;
    float amplitude = 100.0f;
  
    // Increment y ('time')
    yoff += dy;
  
    //float xoff = 0.0;  // Option #1
    float xoff = yoff; // Option #2
  
    for (int i = 0; i < yvalues.length; i++) {
      // Using 2D noise function
      //yvalues[i] = (2*noise(xoff,yoff)-1)*amplitude; // Option #1
      // Using 1D noise function
      yvalues[i] = (2*noise(xoff)-1)*amplitude;    // Option #2
      xoff+=dx;
    }
  
  }
  
  void renderWave() {
    // A simple way to draw the wave with an ellipse at each location
    for (int x = 0; x < yvalues.length; x++) {
      noStroke();
      fill(255,50);
      ellipseMode(CENTER);
      ellipse(x*xspacing,width/2+yvalues[x],16,16);
    }
  }
  </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 xspacing = 8;   // How far apart should each horizontal location be spaced
  int w;              // Width of entire wave
  
  float yoff = 0.0f;        // 2nd dimension of perlin noise
  float[] yvalues;          // Using an array to store height values for the wave (not entirely necessary)
  
  void setup() {
    size(200,200);
    frameRate(30);
    colorMode(RGB,255,255,255,100);
    smooth();
    w = width+16;
    yvalues = new float[w/xspacing];
  }
  
  void draw() {
    background(0);
    calcWave();
    renderWave();
  
  }
  
  void calcWave() {
    float dx = 0.05f;
    float dy = 0.01f;
    float amplitude = 100.0f;
  
    // Increment y ('time')
    yoff += dy;
  
    //float xoff = 0.0;  // Option #1
    float xoff = yoff; // Option #2
  
    for (int i = 0; i &lt; yvalues.length; i++) {
      // Using 2D noise function
      //yvalues[i] = (2*noise(xoff,yoff)-1)*amplitude; // Option #1
      // Using 1D noise function
      yvalues[i] = (2*noise(xoff)-1)*amplitude;    // Option #2
      xoff+=dx;
    }
  
  }
  
  void renderWave() {
    // A simple way to draw the wave with an ellipse at each location
    for (int x = 0; x &lt; yvalues.length; x++) {
      noStroke();
      fill(255,50);
      ellipseMode(CENTER);
      ellipse(x*xspacing,width/2+yvalues[x],16,16);
    }
  }</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.