Calculate text length

Update:
I found the command textAlign, but it doesn’t do what I need

I want to create a loop that runs for example 50 times,
And each time it write word in the center of the screen, in smaller font,

Here is my code:

{
  
  int x1, y1;
  x1=width/2;
  y1=height/2;

for (int j=100; j>0;j-=2)
{
  
  int c = int(random(255));
  
  fill(c,c,c);
  textAlign(CENTER, CENTER);
  textSize(j);
  text("TODAY", x1, y1);
  
}

But the alignment to the horizontal center is not really refers to the center of the screen,
because the result is this:

The question is - how do I calculate how much should I move the text?

Hello,

See the related items:
https://processing.org/reference/text_.html

Also this and the related items:
https://processing.org/reference/width.html

Lots of resources here:

:)

Hi

I think this is useful

And this is how text array …could guide from an idea

@yalaniv,

Take a look at this topic:

It will help you get to this:

image

:)

if i understand correctly i think what you need to do is draw the letters individually. you can see an example of what i mean below

and here’s the code i used (note this is really rough and i didn’t bother too much so no doubt it can be improved and you will need to mess with the inner character scaling)

String letters;
float defaultWidth, spread;
int layers, fontSize, scaleFactor;
int[] shadePalette;

void setup() {
  size(800, 600, P2D);
  fontSize = 100;
  scaleFactor = 10;
  textSize(fontSize);
  textAlign(CENTER, CENTER);
  letters = "TODAY";
  defaultWidth = textWidth(letters);
  spread = defaultWidth / letters.length();
  layers = 10;
  shadePalette = new int[layers];
  for(int i = 0; i < layers; i++) {
    //shadePalette[i] = 255 - (255 / (i + 1));
    shadePalette[i] = int(random(255));
  }
}

void draw() {
  background(0);
  int i, j;
  
  for(i = 0; i < layers; i++) {
    fill(shadePalette[i]);
    textSize(fontSize - i * 4);
    for(j = 0; j < letters.length(); j++) {
      char c = letters.charAt(j);
      text(c, (width / 2 - defaultWidth / 2) + (j * (fontSize - j * 4)), height / 2 + i);      
    }
  }
}
1 Like

@yalaniv,

Tutorials related to text:

References:

:)

I tried to copy all this code and run it, and got an error:
gluegen-rt.dll: Can’t find dependent libraries
A library relies on native code that’s not available.
Or only works properly when the sketch is run as a 32-bit application.

I usually work with the 64-bit app

this is raw processing 4 code. that error is unrelated to the code i posted.

1 Like

I just copy it and paste…
What can be the error?