Skip to content

Instantly share code, notes, and snippets.

@Morpholux
Last active December 17, 2015 18:59
Show Gist options
  • Save Morpholux/5657462 to your computer and use it in GitHub Desktop.
Save Morpholux/5657462 to your computer and use it in GitHub Desktop.
Un programme destiné à animer l’une après l’autre les lettres d’une chaîne de caractères.
// renaud.jean-francois(arobas)uqam(point)ca
// Syntaxe Processing version 2.0b9
// jeudi, 23 mai 2013
PFont maPolice;
char[] alphabet = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
String expression = "hello world"; // attention de ne pas mettre deux espaces blancs consécutifs (problème non résolu)
char [] reference = new char [expression.length()];
char [] motsanim = new char [expression.length()];
int index = 0, compteur = 0, indexProchaineLettre;
int chrono = 0;
int decalageCompteur, avanceUnPas;
void setup() {
size(300, 300);
background(0);
fill(255);
noStroke();
noCursor();
// Plus intéressant d’animer avec une police à chasse fixe (monospaced).
maPolice = createFont("Menlo", 30);
textFont(maPolice);
textAlign(CENTER);
//frameRate(30);
for (int i=0; i<expression.length(); i++) {
reference[i] = expression.charAt(i);
motsanim[i] = ' ';
}
//println(new String(reference));
//println(new String(motsanim));
}
void draw() {
background(0);
// Si on a fait le tour de toutes les lettres
if (index >= reference.length) {
// On remplace tous les caractères par des espaces
for (int i=0; i<motsanim.length; i++) {
motsanim[i] = ' ';
}
// On reprend le processus depuis la première lettre
index=0;
// Petite pause avant de redémarrer le chronomètre
delay(1000);
// Réinitialise le chronometre
chrono = millis();
}
// on doit ignorer les espaces blancs entre les mots dans le traitement
if (reference[index] == ' ') {
index++;
}
//On valide la militude entre la lettre de l’alphabet et la lettre à afficher
if (reference[index] == alphabet[compteur]) {
// Si les deux coïncident, alors on stabilise la lettre dans notre expression
expression = replaceCharAt_v1(new String(motsanim), index, reference[index]);
//println(index);
// Procédure pour positionner le compteur en référence à la prochaine lettre à afficher
indexProchaineLettre = new String(alphabet).indexOf(reference[(index+1)%reference.length]);
while (indexProchaineLettre == -1) { // on était avec un espace blanc
indexProchaineLettre = new String(alphabet).indexOf(reference[(index+1+avanceUnPas)%reference.length]);
avanceUnPas++;
}
avanceUnPas = 1;
//println(indexProchaineLettre);
// Permet de moduler le temps pris pour trouver la prochaine paire de lettres semblables
decalageCompteur = alphabet.length-((int)floor(random(5, 20)));
compteur = (indexProchaineLettre+decalageCompteur)%alphabet.length;
// On passe à l’animation de la lettre suivante
index++;
//println(new String(motsanim));
} else {
// Tant que l’appariement n’est pas effectué, on anime le caractère (effet de substitution)
expression = replaceCharAt_v1(new String(motsanim), index, alphabet[compteur]);
}
// petite pause dans le noir avant d’afficher le texte
if ((millis()-chrono) >= 500) {
// On a intérêt à afficher le texte en un seul bloc plutôt qu’une suite de caractères. Aide au positionnement.
// Par contre, le traitement (couleur, taille) est uniforme sur tout le texte dans son ensemble
text(expression, width/2, height/2);
compteur++;
compteur = compteur%alphabet.length;
}
}
// Crédits : http://forum.processing.org/user/thomas.diewald
// Cette fonction permet d’éditer le contenu d’un String
String replaceCharAt_v1(String s, int pos, char c) {
s.getChars(0, motsanim.length, motsanim, 0);
motsanim[pos] = c;
return new String(motsanim);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment