Text bounce bounding box?

Hello all. Is it possible to make a bounding box around your text, so when it hits the right side it won’t go beyond the width of my work area? I could minus the text width from the xPos, but that would have to make the text a fixed size and I would prefer it to be random


float xPos = random(0, 500);
float xspeed = 10;
float direction = 1;
PFont font1;
float textStr;



void setup() {
size (350, 500);
  textStr = random(10, 20);
  
} 


void draw() {
  background (0);
  textSize(textStr);

  fill(255);
  textAlign(LEFT);
  xPos = xPos + direction*xspeed;
  text("this is a test", xPos, 425);
  if (xPos > width) direction = -1;
  if (xPos < 0) direction = 1;
  if (xPos < 0)
    textStr = random(10, 20);
  if (xPos > width)
    textStr = random(10, 20);
} 
1 Like

could you first play

text(s, 10, 10, 70, 80); // Text wraps within text box

if that fits your needs

2 Likes

it still goes beyond on the right side of the canvas because the text is left aligned I suppose :confused:

1 Like

I tried a bunch of things but its still not working. I can’t seem to mix ints and floats so it’s I’ve tried both. The text is there but the animation is gone

String mytext = "this is a test";
float x1;
float x=x1, y=10, w=width, h=height;

float xPos = random(0, 500);
float xspeed = 10;
float direction = 1;
PFont font1;
float textStr;



void setup() {
  x1=random(0, 500);
size (350, 500);
  textStr = random(10, 20);
  
} 


void draw() {
  background (0);
  textSize(textStr);

  fill(255);
  textAlign(LEFT);
  xPos = xPos + direction*xspeed;
  text("this is a test", x, y, w, h);
  if (xPos > width) direction = -1;
  if (xPos < 0) direction = 1;
  if (xPos < 0)
    textStr = random(10, 20);
  if (xPos > width)
    textStr = random(10, 20);
} 
1 Like

possibly use the variables for the text boundaries?

String mytext = "this is a test";
float y=10, w, h;
float xPos = 0;
float xspeed = 10;
float direction = 1;
//PFont font1;
float textStr;

void setup() {
  size (350, 500);
  textStr = random(10, 20);
  h=height-y;
  w=width;
} 

void draw() {
  background (0);
  textSize(textStr);
  fill(255);
  //textAlign(LEFT);
  xPos = xPos + direction*xspeed;
  if (xPos > width) {  
    direction = -1;
    textStr = random(10, 20);
  }
  if (xPos < 0) {     
    direction = 1;
    textStr = random(10, 20);
  }
  w = width-xPos;
  text(mytext, xPos, y, w, h);
} 

it is difficult to see what the result of your sketch should be?

1 Like

it is difficult to see what the result of your sketch should be?

basically a ball bouncing from the left wall to the right wall, but instead of a ball it’s a piece of text.

as of now it keeps going beyond the right wall

not in my version as i recalc w

you are right! it stays within but not on the same line.

ok, no wrap, miss understanding…
just move to right and back?
using correct textwidth

String mytext = "this is a test";
float y=10;
float x = 0;
float xspeed = 10;
float direction = 1;

float textStr;

void setup() {
  size (350, 500);
  textStr = random(10, 20);
} 

void draw() {
  background (0);
  textSize(textStr);
  fill(255);
  //textAlign(LEFT);
  x = x + direction*xspeed;
  if (x > width-textWidth(mytext)) {  
    direction = -1;
    textStr = random(10, 20);
  }
  if (x < 0) {     
    direction = 1;
    textStr = random(10, 20);
  }
  text(mytext, x, y);
} 
1 Like

exactly, thank you sir.