topical media & game development

talk show tell print

#javascript-processing-example-basic-math-sine.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>Sine</h2>
  
  <p>Smoothly scaling size with the sin() function.</p>
  
  <p><a href="http://processing.org/learning/basics/sine.html"><b>Original Processing.org Example:</b> Sine</a><br>
  <script type="application/processing">
  float spin = 0.0; 
  float diameter = 84.0; 
  float angle;
  
  float angle_rot; 
  int rad_points = 90;
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    smooth();
  }
  
  void draw() 
  { 
    background(153);
    
    translate(130, 65);
    
    fill(255);
    ellipse(0, 0, 16, 16);
    
    angle_rot = 0;
    fill(51);
  
    for(int i=0; i<5; i++) {
      pushMatrix();
      rotate(angle_rot + -45);
      ellipse(-116, 0, diameter, diameter);
      popMatrix();
      angle_rot += PI*2/5;
    }
  
    diameter = 34 * sin(angle) + 168;
    
    angle += 0.02;
    if (angle > TWO_PI) { angle = 0; }
  }
  </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 spin = 0.0; 
  float diameter = 84.0; 
  float angle;
  
  float angle_rot; 
  int rad_points = 90;
  
  void setup() 
  {
    size(200, 200);
    noStroke();
    smooth();
  }
  
  void draw() 
  { 
    background(153);
    
    translate(130, 65);
    
    fill(255);
    ellipse(0, 0, 16, 16);
    
    angle_rot = 0;
    fill(51);
  
    for(int i=0; i&lt;5; i++) {
      pushMatrix();
      rotate(angle_rot + -45);
      ellipse(-116, 0, diameter, diameter);
      popMatrix();
      angle_rot += PI*2/5;
    }
  
    diameter = 34 * sin(angle) + 168;
    
    angle += 0.02;
    if (angle &gt; TWO_PI) { angle = 0; }
  }</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.