xxxxxxxxxx
TextGraphics tg;
ArrayList<Part> parts;
int nbParts = 3000;
float theta = 0, gamma = 0;
void setup()
{
size(290, 350, P3D);
tg = new TextGraphics();
}
void draw()
{
background(255);
theta = map(mouseX, 0, width, -PI, PI);
gamma = map(mouseY, 0, height, PI, -PI);
for (Part p : parts) {
p.update();
}
}
void mousePressed()
{
}
void keyPressed()
{
if (key != CODED)
{
switch(key)
{
case ' '://ignore SPACE
case ESC://ignore ESCAPE
case ENTER://ignore ENTER
case BACKSPACE://ignore BACKSPACE
break;
default://char input
//tg.process("" + key);//RUN IN JAVA
tg.process(new String(key));//RUN IN JS
break;
}
}
}
class Part
{
PVector pos, origin;
float viscosity = random(.03, .2);
Part(PVector p_pos)
{
origin = p_pos.get();
origin.z = random(-15, 15);
pos = origin.get();
}
void update()
{
pushMatrix();
translate(width/2, height/2);
rotateY(theta);
rotateX(gamma);
translate(-width/2, -height/2);
float x = modelX(origin.x, origin.y, origin.z);
float y = modelY(origin.x, origin.y, origin.z);
float z = modelZ(origin.x, origin.y, origin.z);
popMatrix();
PVector dest = new PVector(x, y, z);
dest.sub(pos);
stroke(min(map(dest.mag(), 0, 100, 0, 100), 100));
strokeWeight(map(dest.z, -50, 50, 1, 3));
dest.mult(viscosity);
pos.add(dest);
point(pos.x, pos.y);
}
}
class TextGraphics
{
PGraphics pg;//buffer PG used to write the input char
TextGraphics()
{
pg = createGraphics(width, height, P2D);
process(new String("#"));//"π")//initialize with a String
}
void process(String c)
{
pg.beginDraw();
pg.translate(0, height/1.6);
pg.background(0);
pg.textSize(350);//500
pg.fill(color(0, 255, 0));
pg.text(c, 30, 30);
pg.translate(0, -height/1.6);
pg.endDraw();
parts = new ArrayList<Part>();
pg.loadPixels();
//random hair disposition
while (parts.size () < nbParts)
{
PVector pv = new PVector(random(width), random(height));
if (green(pg.pixels[(int)pv.y * width + (int)pv.x]) > 100)
parts.add(new Part(new PVector(pv.x, pv.y)));
}
pg.updatePixels();
}
}