topical media & game development

talk show tell print

#javascript-processing-example-topic-gui-rollover.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>Rollover</h2>
  
  <p>Roll over the colored squares in the center of the image
  to change the color of the outside rectangle. 
  
  Updated 09 February 2003.</p>
  
  <p><a href="http://processing.org/learning/topics/rollover.html"><b>Original Processing.org Example:</b> Rollover</a><br>
  <script type="application/processing">
  int rectX, rectY;      // Position of square button
  int circleX, circleY;  // Position of circle button
  int rectSize = 50;     // Diameter of rect
  int circleSize = 53;   // Diameter of circle
  
  color rectColor;
  color circleColor;
  color baseColor;
  
  boolean rectOver = false;
  boolean circleOver = false;
  
  void setup()
  {
    size(200, 200);
    smooth();
    rectColor = color(0);
    circleColor = color(255);
    baseColor = color(102);
    circleX = width/2+circleSize/2+10;
    circleY = height/2;
    rectX = width/2-rectSize-10;
    rectY = height/2-rectSize/2;
    ellipseMode(CENTER);
  }
  
  void draw()
  {
    update(mouseX, mouseY);
  
    noStroke();
    if (rectOver) {
      background(rectColor);
    } else if (circleOver) {
      background(circleColor);
    } else {
      background(baseColor);
    }
  
    stroke(255);
    fill(rectColor);
    rect(rectX, rectY, rectSize, rectSize);
    stroke(0);
    fill(circleColor);
    ellipse(circleX, circleY, circleSize, circleSize);
  }
  
  void update(int x, int y)
  {
    if( overCircle(circleX, circleY, circleSize) ) {
      circleOver = true;
      rectOver = false;
    } else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
      rectOver = true;
      circleOver = false;
    } else {
      circleOver = rectOver = false;
    }
  }
  
  boolean overRect(int x, int y, int width, int height) 
  {
    if (mouseX >= x && mouseX <= x+width && 
        mouseY >= y && mouseY <= y+height) {
      return true;
    } else {
      return false;
    }
  }
  
  boolean overCircle(int x, int y, int diameter) 
  {
    float disX = x - mouseX;
    float disY = y - mouseY;
    if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
      return true;
    } else {
      return false;
    }
  }
  </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 rectX, rectY;      // Position of square button
  int circleX, circleY;  // Position of circle button
  int rectSize = 50;     // Diameter of rect
  int circleSize = 53;   // Diameter of circle
  
  color rectColor;
  color circleColor;
  color baseColor;
  
  boolean rectOver = false;
  boolean circleOver = false;
  
  void setup()
  {
    size(200, 200);
    smooth();
    rectColor = color(0);
    circleColor = color(255);
    baseColor = color(102);
    circleX = width/2+circleSize/2+10;
    circleY = height/2;
    rectX = width/2-rectSize-10;
    rectY = height/2-rectSize/2;
    ellipseMode(CENTER);
  }
  
  void draw()
  {
    update(mouseX, mouseY);
  
    noStroke();
    if (rectOver) {
      background(rectColor);
    } else if (circleOver) {
      background(circleColor);
    } else {
      background(baseColor);
    }
  
    stroke(255);
    fill(rectColor);
    rect(rectX, rectY, rectSize, rectSize);
    stroke(0);
    fill(circleColor);
    ellipse(circleX, circleY, circleSize, circleSize);
  }
  
  void update(int x, int y)
  {
    if( overCircle(circleX, circleY, circleSize) ) {
      circleOver = true;
      rectOver = false;
    } else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
      rectOver = true;
      circleOver = false;
    } else {
      circleOver = rectOver = false;
    }
  }
  
  boolean overRect(int x, int y, int width, int height) 
  {
    if (mouseX &gt;= x &amp;&amp; mouseX &lt;= x+width &amp;&amp; 
        mouseY &gt;= y &amp;&amp; mouseY &lt;= y+height) {
      return true;
    } else {
      return false;
    }
  }
  
  boolean overCircle(int x, int y, int diameter) 
  {
    float disX = x - mouseX;
    float disY = y - mouseY;
    if(sqrt(sq(disX) + sq(disY)) &lt; diameter/2 ) {
      return true;
    } else {
      return false;
    }
  }</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.