> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.uVrdP2u3AGf/rev.502
 * 
 * authors: 
 *   
 *   frederic.vernier
 *   JDF
 *   dragice

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/* @pjs preload="/static/uploaded_resources/p.2418/logoUPSUD.png"; */


// global variables
int    N      = 20;
int    NMIN   = 10;
int    NMAX   = 30;
int    dN     = 0;

float  RADEYE = 0.5;
PFont  font;
PImage img;

String lines[];


// setup the processing canvas
void setup() {
  size(640,480);
  //println("Hello console message N= "+N);
  font = loadFont("ArialNarrow-12.vlw");
  if (font==null)
    font = loadFont("FFScala-32.vlw");

  // loading resources
  img =loadImage("/static/uploaded_resources/p.2418/logoUPSUD.png"); 
 
  noLoop();
}


/*
 * Example of function to draw text in a box
*/
void drawBoxText(int x, int y, String txt, int size){
  strokeWeight(1);  
  textSize(size);
  fill(64,128,0);
  rect(x, y, textWidth(txt)+4, size+2);
  fill(0, 0, 0);
  textFont(font); 
  text(txt, x+1, y+size-1); 
  fill(255, 255, 255);
  text(txt, x+2, y+size);
}


// repaint the canvas
void draw() {
  // grey background 
  background(200, 200, 200);
  
  // to display the eye right, it's better to change the drawing system
  translate(width/2, height/2);
  rotate(PI/4.0);
  
  // compute few values
  int dmax  = min(width/2, height/2);
  int dmin  = min(width, height);
  int d     = (int)dist(mouseX, mouseY, width/2, height/2);
  int diris = floor(RADEYE*d);
  int lev   = (int)(256*norm(d, diris, 3*dmax/2));
  float angle = atan2(mouseY-height/2, mouseX-width/2);
  
  // let's draw black 
  strokeWeight(1.5);
  stroke(0, 0, 0, 255);
  // with colored background
  fill(0, lev, 255-lev, 128);
  ellipse(0, 0, 3*dmax/2, 3*dmax/2);
  fill(0,0,0);
  ellipse(0, 0, diris, diris);

  // lines above and under the eye will be a little bit thicker
  strokeWeight(3);

  // use a loop to display them
  for (int i=0; i<=N; i++)
    line(-dmin/2+(i*dmin)/N, -dmin/2, -dmin/2, -dmin/2+((N-i)*dmin)/N);
  for (int i=0; i<=N; i++)
    line(-dmin/2+(i*dmin)/N, dmin/2, dmin/2, -dmin/2+((N-i)*dmin)/N); 

  // let s get back to straight world (but still centered)
  rotate(-PI/4.0);    

  // make the flare effect by displaying pixeles one by one
  // computing the distance to the center of the flare
  // unsing HSB mode and choosing and alpha(oppacity) relative to the distance
  diris = dmax-diris;;
  colorMode(HSB, diris/2);
  for (int i = -diris/5; i < diris/5; i++) {
    for (int j = -diris/5; j < diris/5; j++) {
      int dd = (int)dist(0,0,i,j);
      if (dd<diris/5){
        stroke(abs(i), abs(j), 255, 0.3*(diris/5-dd));
        point(i-dmax/2*cos(angle), j-dmax/2*sin(angle));
      }
    }
  }
  
  // get back to the very normal mode
  colorMode(RGB, 255);  
  translate(-width/2, -height/2);
  
  image(img, width-img.width/4, height-img.height/4, img.width/4, img.height/4);
  
  // call the previously defined function to draw texts in boxes
  drawBoxText(20, 10,  "Hey! low word ", 12);
  drawBoxText(20, 30,  "Fred is watching you", 14);
  drawBoxText(20, 54,  "diris= "+ img.width, 16);
  
  // makes evolve the animation parameter
  N = N+dN;
  if (N>NMAX||N<NMIN){dN*=-1;}
}


// example of callback
void mousePressed() {
  dN = 1;
  // start the animation when mouse is pressed 
  frameRate(25);
  loop();
}


void mouseMoved() {
  // if no animation is going on it is important to call redraw to see the eye 
  // reacting to the mouse position
  if (dN==0)
    redraw();
}


void mouseReleased() {
  dN=0;
  // stop The animation when mouse is released
  noLoop();
}