Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Creative Coding
- * Week 5, 05 - Text agents
- * by Indae Hwang and Jon McCormack
- * Copyright (c) 2014 Monash University
- *
- * This sketch creates a simple agent-based simulation using text and objects
- * The sketch reads in characters from the keyboard and dynamically creates
- * new objects for each character.
- *
- * (c) 2015 Giles Cartmel
- * Changes to animate characters towards target points in the order
- * they are typed, adding of colours and other changes to special effects.
- *
- */
- ArrayList<AniCharacter> aniChars;
- PFont myFont;
- int currentCount;
- boolean saveFrames = false;
- int frameNo = 0;
- void setup() {
- size(700, 700);
- myFont = loadFont("HelveticaNeue-UltraLight-200.vlw");
- textFont(myFont);
- //textMode(SHAPE);
- aniChars = new ArrayList<AniCharacter>();
- smooth(8); // enable antialiasing
- colorMode(HSB,360,100,100);
- }
- void draw() {
- background(0,0,0);
- for (int i = aniChars.size()-1; i >= 0; i--) {
- AniCharacter tempObj = aniChars.get(i);
- tempObj.run(aniChars.size()); // run each char
- }
- // if save frames is turned on (toggled with mouse click)
- // then save every 10th frame
- // if (saveFrames && frameCount%10 == 0) {
- // saveFrame("frames/"+str(++frameNo)+".jpg");
- // }
- }
- void keyReleased() {
- // CTRL-H to "backspace" the characters.
- if (int(key) != 65535) { // prevent SHIFT key causing glyph error by being treated as a character
- if (8 == int(key) && aniChars.size() > 0) {
- aniChars.remove(aniChars.size()-1);
- } else {
- aniChars.add( new AniCharacter(random(width), random(height), key, aniChars.size()) );
- }
- }
- }
- void mouseReleased() {
- saveFrames = !saveFrames;
- println("Saving Frames: "+saveFrames);
- }
- // AniCharacter class
- //
- //
- class AniCharacter {
- float x, y;
- float targX, targY;
- float xSpeed, ySpeed;
- float dynamic;
- float dynamic_c;
- float size_font;
- float size_shape, randSize1, randSize2;
- char letter;
- int index;
- color fontcolour;
- // Constructor
- AniCharacter(float x_, float y_, char c_, int i_) {
- x = x_;
- y = y_;
- targX = x;
- targY = y;
- xSpeed = 0;
- ySpeed = 0;
- letter = c_;
- index = i_;
- println("Letter["+index+"]: "+letter);
- size_font = random(50, 200);
- size_shape = size_font + random(10, 50);
- randSize1 = random(0.5,1.0);
- randSize2 = random(0.5,1.5);
- dynamic_c = random(-0.1, 0.1);
- fontcolour = color(random(360),50+random(50), 50+random(50), 100+random(155));
- }
- void run(int letters) {
- // based on the number of letters determine how many 'grid' positions we need to place
- // all the letters, and move the letters in the direction of their calculated position
- float rowcols = (float) (sqrt(letters)+1); // determine how many cols/rows to have
- // determine the target Position
- float newTargX = lerp(0, width-(width/20), (float) ((index%rowcols)+1)/(rowcols+1));
- float newTargY = lerp(height/20, height+(height/20), (float) (floor((index/rowcols))+1)/(rowcols+1));
- if (newTargX != targX) {
- // target X position has changed
- // reset X speed
- targX = newTargX;
- }
- if (newTargY != targY) {
- // target Y position has changed
- targY = newTargY;
- }
- xSpeed = (targX-x)/200;
- ySpeed = (targY-y)/200;
- x = x + xSpeed;
- y = y + ySpeed;
- switch(letter) {
- case 'a':
- dynamic -= dynamic_c;
- break;
- case 'c':
- dynamic += dynamic_c;
- break;
- case 'e':
- dynamic += dynamic_c;
- break;
- case 'G':
- dynamic -= 2*dynamic_c;
- break;
- }
- visual();
- }
- /*
- * visual
- * draw the char with special cases for some letters
- */
- void visual() {
- textSize(size_font);
- float textWidth = textWidth(letter);
- float ascent = textAscent() * 0.75;
- switch(letter) {
- case 'a':
- fill(fontcolour);
- noStroke();
- text(letter, x-textWidth/2, y+ascent/2);
- stroke(245,100,100);
- noFill();
- pushMatrix();
- translate(x, y);
- rotate(dynamic);
- arc(0, 0, size_shape, size_shape*randSize1, HALF_PI*randSize2, PI+HALF_PI);
- popMatrix();
- break;
- case 'c':
- fill(fontcolour);
- noStroke();
- text(letter, x-textWidth/2, y+ascent/2);
- float tempx = x + size_shape*cos(dynamic*0.5);
- float tempy = y + size_shape*sin(dynamic*0.5);
- float tempx2 = tempx - (size_shape/2)*sin(dynamic*0.2);
- float tempy2 = tempy + (size_shape/2)*cos(dynamic*0.3);
- //fill(255, 0, 0);
- fill(200,100,100,100);
- //stroke(255, 0, 0, 140);
- stroke(200,50,50,100);
- line(x, y, tempx, tempy);
- noStroke();
- ellipse(tempx, tempy, 10, 10);
- stroke(0,100,100,128);
- fill(100,100,100,100);
- ellipse(tempx2, tempy2, 5, 5);
- break;
- case 'e':
- rectMode(CENTER);
- //fill(0,128,255);
- fill(fontcolour);
- // rect(x, y, textWidth, size_shape*sin( radians(dynamic) ));
- ellipse(x, y, textWidth, size_shape*sin( radians(dynamic) ));
- rectMode(CORNER);
- fill(255,100,100);
- noStroke();
- text(letter, x-textWidth/2, y+ascent/2);
- break;
- case 'G':
- pushMatrix();
- rectMode(CENTER);
- fill(0,100,100,75);
- translate(x,y);
- rotate(cos(radians(dynamic)));
- rect(0, 0, textWidth*1.1, textWidth*1.4);
- rectMode(CORNER);
- fill(150,80,100,200);
- noStroke();
- text(letter, -textWidth/2, ascent/2);
- popMatrix();
- break;
- default:
- fill(fontcolour);
- noStroke();
- text(letter, x-textWidth/2, y+ascent/2);
- break;
- }
- //ellipse(x, y, 10, 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement