topical media & game development

talk show tell print

#javascript-processing-example-topic-drawing-animator.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>Animator</h2>
  
  <p>A simple animation tool that displays a continuous cycle of
  twelve images. Each image is displayed for 100 milliseconds 
  (one tenth of a second) to create animation. While each image 
  is displayed, itŐs possible to draw directly into it by
  pressing the mouse and moving the cursor. Start drawing to get
  it started.</p>
  
  <p><a href="http://processing.org/learning/topics/animator.html"><b>Original Processing.org Example:</b> Animator</a><br>
  <script type="application/processing">
  int currentFrame = 0;
  PImage[] frames = new PImage[12];
  int lastTime = 0;
  
  void setup() 
  {
    size(200, 200);
    strokeWeight(4);
    smooth();
    background(204);
    for (int i = 0; i < frames.length; i++) {
      frames[i] = get(); // Create a blank frame
    }
  }
  
  void draw() 
  {
    int currentTime = millis();
    if (currentTime > lastTime+100) {
      nextFrame();
      lastTime = currentTime;
    }
    if (mousePressed == true) {
      line(pmouseX, pmouseY, mouseX, mouseY);
    }
  }
  
  void nextFrame() 
  {
    frames[currentFrame] = get(); // Get the display window
    currentFrame++; // Increment to next frame
    if (currentFrame >= frames.length) {
      currentFrame = 0;
    }
    image(frames[currentFrame], 0, 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>
  int currentFrame = 0;
  PImage[] frames = new PImage[12];
  int lastTime = 0;
  
  void setup() 
  {
    size(200, 200);
    strokeWeight(4);
    smooth();
    background(204);
    for (int i = 0; i &lt; frames.length; i++) {
      frames[i] = get(); // Create a blank frame
    }
  }
  
  void draw() 
  {
    int currentTime = millis();
    if (currentTime &gt; lastTime+100) {
      nextFrame();
      lastTime = currentTime;
    }
    if (mousePressed == true) {
      line(pmouseX, pmouseY, mouseX, mouseY);
    }
  }
  
  void nextFrame() 
  {
    frames[currentFrame] = get(); // Get the display window
    currentFrame++; // Increment to next frame
    if (currentFrame &gt;= frames.length) {
      currentFrame = 0;
    }
    image(frames[currentFrame], 0, 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.