Hi everyone. I have this class with a lot of cities. I want each city to have it’s opacity/alpha value be random and go between 0-255, kind of like a pulsing effect.
I’ve tried with if() statements but its not quite there
class OrdBillede2 {
float opacity;
color [] muligeFarver = {
color(100, 214, 232),
color(22, 81, 32),
color(31, 66, 106),
};
String [] words2 = loadStrings("byer2.txt");
String [] individual;
color [] byFarver;
int o;
PFont font1;
float textStr2 = 15;
OrdBillede2() {
opacity = random(0, 255);
font1 = loadFont ("Akkurat-Bold-18.vlw");
textFont(font1);
//for (int i = 0; i <10; i++) {
// words2 = concat(words2, words2);
//}
individual = split(join(words2, " "), " ");
byFarver = new color[individual.length];
for (int i = 0; i<individual.length; i++) {
byFarver[i] = muligeFarver [int(random(muligeFarver.length))];
} // loop
} // OrdBillede2()
void display() {
showText (individual);
} // void display
void showText(String [] localArray) {
int x = 80;
int y = 176;
for ( int i = 0; i <localArray.length; i++) {
if (x+textWidth (localArray[i])>width-5) {
x = 80;
y += 30;
}
textAlign(CENTER);
textFont(font1);
textSize(textStr2);
fill(byFarver[i], opacity);
text(localArray[i], x, y);
x+=textWidth (localArray[i])+22;
}
if (opacity > 255) {
opacity = opacity-10;
}
if (opacity < 0) {
opacity = opacity+10;
}
} // void showText
} // ordBillede2
For a pulsing effect you could try using a variable for the change in opacity in each update loop. And when the opacity reaches its bounds, negate that variable:
An alternative (and possibly simpler) pulsing animation could be done with sine functions and would give a more natural pulsing. I encourage you to try this as well:
Explanation: We want opacity to pulse vary through time, so we use frameCount as it increases with time. Plug that into the sine function and we get output in the range of -1 to 1. Since opacity is in range of 0 to 255, we use map to linearly map -1 to 0 and 1 to 255.
For randomness in each of your cities, each city should hold an extra variable called phase (a random number between 0 and TWO_PI.
Ah, the you’ve made ten different phases for each word you wanted to display. What I meant was having a phase per word. In this case, we can make a parallel list where the phases have the same size as your words array individuals.
Here’s what I would change:
In your constructor, instead of making a phases array the size of 10, we make it the same size as your individual array, so that for each item in individual, there is a phase for it.
float[] phases;
OrdBillede2() {
// ...
phases = new float[individual.length];
for (int i = 0; i < individual.length; i ++) {
phases[i]=random(0, TWO_PI);
}
// ...
}
Remove the nested for loop (the one with k) at the bottom of your showText function where it loops through all the phases for each word, instead just use the integer you already have: i to reference to phase for that letter.