text input which is then randomly animated
in
Programming Questions
•
2 years ago
hello — i'm still quite a beginner at processing and i hope someone can help me with my problem.
i would like to have a text input, where the letters start to "wander" after a certain time of there being no keyboard input. please try the code below to see what i mean with "wander"...
i've managed to have a fix text, inserted in the text editor, which wanders off in different directions when "enter" is pressed, but i'd like to have the possibility of inserting the text in the display window while the programme is running.
i took the original code from the "strings and drawing text" tutorial and changed it only a little bit.
and is there a way to get the letters to wander off in one specific random direction, like on a straight line and not "wobble" around randomly like they do now?
thank you very much for any help!
here's the code:
i would like to have a text input, where the letters start to "wander" after a certain time of there being no keyboard input. please try the code below to see what i mean with "wander"...
i've managed to have a fix text, inserted in the text editor, which wanders off in different directions when "enter" is pressed, but i'd like to have the possibility of inserting the text in the display window while the programme is running.
i took the original code from the "strings and drawing text" tutorial and changed it only a little bit.
and is there a way to get the letters to wander off in one specific random direction, like on a straight line and not "wobble" around randomly like they do now?
thank you very much for any help!
here's the code:
- PFont f;
- String message = "type text here";
- // An array of Letter objects
- Letter[] letters;
- void setup() {
- size(1000, 400);
- // Load the font
- f = loadFont("Arial-Black-48.vlw");
- textFont(f);
- // Create the array the same size as the String
- letters = new Letter[message.length()];
- // Initialize Letters at the correct x location
- int x = 100;
- for (int i = 0; i < message.length(); i++) {
- letters[i] = new Letter(x,height/2,message.charAt(i));
- x += textWidth(message.charAt(i));
- }
- }
- void draw() {
- background(255);
- for (int i = 0; i < letters.length; i++) {
- // Display all letters
- letters[i].display();
- // If the mouse is pressed the letters shake
- if (keyCode == ENTER) {
- letters[i].shake();
- }
- }
- }
- // A class to describe a single Letter
- class Letter {
- char letter;
- // The object knows its original "home" location
- float homex,homey;
- // As well as its current location
- float x,y;
- Letter (float x_, float y_, char letter_) {
- homex = x = x_;
- homey = y = y_;
- letter = letter_;
- }
- // Display the letter
- void display() {
- fill(0);
- textAlign(LEFT);
- text(letter,x,y);
- }
- // Move the letter randomly
- void shake() {
- x = x + random(-1,1);
- y = y + random(-1,1);
- }
- }
1