Basic Processing sketch

to be used as a base for future simple sketches.

by blackpolygon

HTML

<script type="application/processing">
// set variables
int width = 600;
int height = 400;


PVector [] findTargets (int n, PImage img)
{
  img.loadPixels();
  println ("find targets");
  PVector [] targets = new PVector [n];

  PVector pos;
  for (int i = 0; i < targets.length; i++)
  {
    println (i);
    pos = target (img.pixels, (int) random (img.width), (int) random (img.height), img.width, img.height, 0);
		println (pos);
    targets [i] = pos;
  }

  return targets;
}

PVector target (int [] colors, int x, int y, int W, int H, int depth)
{
  PVector pos = new PVector (0, 0);
  int index = y*W+x;
  color c = colors [index];
  println ("target" +x +y + W,H,depth);

  // println ("c: " + red (c) + ", " + green (c) + ", " + blue (c));
  if (depth == 5 || isValidTarget (brightness (c)))
  {
    pos.x = x;
    pos.y = y;

//    print (pos);
  } 
  else 
  {
    pos = target (colors, (int) random (W), (int) random (H), W, H, depth++);
  }
  
    return pos;
}

boolean isValidTarget (float fbrightness)
{
println ('is valid target');
  if (fbrightness > 220) return false;
  float value = map (fbrightness, 0, 255, 1, 200);
  // println (value);

  float iRandom = random (0, value);
  //println (value + ", " + iRandom);
  if (iRandom <= 1) return true;
  else return false;
}


class Line
{
  PVector start, end, middle;
  float sw = random (0.1, 0.25);
  color c;
  Line (PVector start, PVector end)
  {
  print('new line', start, end);
    this.start = start;
    this.end = end;

    setMiddle ();
  }

  void setMiddle ()
  {
    float dis = PVector.dist (start, end) * random (0.25, 0.75);
    float angle = atan2 (end.y - start.y, end.x - start.x);
    float randomAngle = random (-PI/6, PI/6);
    float x = start.x + cos (angle + randomAngle) * dis;
    float y = start.y + sin (angle + randomAngle) * dis;
    middle = new PVector (x, y);
  }

  void setStrokeWeightAndColor(int [] img, int w, int h)
  {
    color c1 = img[(int)start.y*w+(int)start.x];
    color c2 =...

JavaScript

var scripts=document.getElementsByTagName("script");
for(var i=0;i<scripts.length;i++) {
    if(scripts[i].type=="application/processing"){
        var src=scripts[i].src,canvas=scripts[i].nextSibling;
        if(src&&src.indexOf("#")){
           canvas=document.getElementById(src.substr(src.indexOf("#")+1));
        }else{
            while(canvas&&canvas.nodeName.toUpperCase()!="CANVAS")
                canvas=canvas.nextSibling;
        }
        if(canvas){
            new Processing(canvas,scripts[i].text);
        }
    }
}