> show canvas only <


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

 * 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.2468/cb4.png; */

int    NBW = 8;
int    nbl = 0;
int    x   = 100;
int    y   = 100;
float  dx  = 5;
float  dy  = 0;
int    ex=-1, ey=-1, es=0;
int    hs  = 100;
PImage img, reflexion;
PImage waters[] = new PImage[NBW];


void setup() {  // this is run once.   
  size(640, 480);
  img       = loadImage("/static/uploaded_resources/p.2468/cb4.png");
  reflexion = createImage(img.width, img.height, ARGB);
  frameRate(25);
}


PImage compWater(float t){
  PImage water = createImage(width, height/2-height/12, ARGB);
  for (int j=0; j < water.height; j++) {
    for (int i=0; i < water.width; i++) {
      int  aj = (j)*0.001;
      int  ai = (water.width/2-i)*0.05;
      byte b1 = 63+noise(1/aj, ai, t)*192;
      byte g1 = noise(1/aj, ai, t+0.1)*128;
      water.pixels[i+j*water.width] = (255<<24) + (0<<16) + (g1<<8) + (b1<<0);
    }
  }
  return water;
}


void compReflexion(float shift){
  for (int j=0; j < img.height; j++) {
    for (int i=0; i < img.width; i++) {
      int c = img.pixels[i+j*img.width];
      byte a = (c&0xff000000)>>24;if (a<0) a+=256;
      byte r = (c&0x00ff0000)>>16;if (r<0) a+=256;
      byte g = (c&0x0000ff00)>>8;if (g<0) a+=256;
      byte b = (c&0x000000ff)>>0;if (b<0) a+=256;
      byte l = max(max(r, g), b);
      byte r1= (r*j + (l*(img.height-j)))/img.height;
      byte g1= (g*j + (l*(img.height-j)))/img.height;
      byte b1= (b*j + (l*(img.height-j)))/img.height;
      if (a<0) a+=256;
      float co = (j-shift)/(img.height); 
      if (co<0) co =0;
      if (a<(255*co)) a1=a; else a1 = (255*co);
      reflexion.pixels[i+(reflexion.height-j-1)*reflexion.width] = (a1<<24) + (r1<<16) + (g1<<8) + (b1<<0);
    }
  }    
}


void draw() {
  textAlign(CENTER);
  background(100, 200, 255); 
  noStroke();
  fill(255, 255, 255);
  if (nbl<NBW) {
    text("computing texture "+(nbl+1)+" / "+NBW, width/2, height/2);
    rect(width/2-NBW*5, height/2+10, (nbl+1)*10, 20);
    stroke(0);
    noFill();
    rect(width/2-NBW*5, height/2+10, NBW*10, 20);    
    waters[nbl]=compWater(nbl/(3*NBW));
    nbl++;
    return;
  }
  
  textAlign(LEFT);
  hs = 35+100*noise(millis()*0.001);
  int index = (int(frameCount/2))%(2*NBW-2);
  if (index>= NBW) index = -2+2*NBW-index;
  image(waters[index], 0, height/2);
  image(img, x, y-img.height+hs);
  compReflexion(height/2-y);
  image(reflexion, x, height-y+hs);
  noFill();
  stroke(128, 128, 255, 255-es*2);
  if (ex>=0)
    for (i=es; i>0; i-=24)
      ellipse(ex, ey, i, i/4);
  
  fill(0, 0, 0);
  text("(c) 2011 Frederic Vernier", 6, 14);
  fill(255, 255, 0);
  text("(c) 2011 Frederic Vernier", 5, 13);
  
  es = es+4;
  dy = dy+1;
  x  = x+dx;
  if (x>width-img.width) {x=2*(width-img.width)-x;dx=dx*-1;}
  if (x<0) {x=-x;dx=dx*-1;}
  dy =dy+1;
  y  =y+dy;
  if (y>height/2) {y=height-y;dy=dy*-1;ex=x+10; ey=height/2+hs;es=5; }
}


void mouseClicked(){
  x = mouseX-img.width/2;
  y = mouseY-img.height/2;
  dx=5;
  dy=0;
}