Trying to convert a basic grow shrink sketch from processing to p5.js

Hi, I’m trying to convert a processing 3 sketch that works to a p5.js and I can’t wrap my head around what the changes are or where to find a translation guide. I think if I can crack this conversion I’ll be able to do most of my coding in p5.js. The lump of code is the processing sketch I’m trying to convert. It makes a circle grow and shrink.

Ball myBall;
  boolean grow =true;
  boolean button =false;
void setup(){
  size(400,400);
     myBall = new Ball(color(255, 0,0));
  
}

void draw(){
background(255);
 myBall.GrowBall();
  myBall.dispBall();
  myBall.shrink();
  
}

//----class----
class Ball{
 float pX;
 float pY;
 float bSize; 
  color c;
  float addSize=1;
  float ballMax= 100;
  float ballMin =10;
  Ball( color tempC){
    bSize=10;
    c = tempC;

  }
  void dispBall(){
  
  pX = 200;
  pY = 200;
   
  noStroke();
fill(c);
  circle(pX, pY, bSize);
     bSize= +addSize;
  }
  
    void GrowBall(){
        //grow shrink boolean switch
     if (grow==true){
        addSize= addSize+0.5;
      }else {
         addSize = addSize-0.5;
       }
        
     //-----condionals
    bSize = constrain(bSize, 10, 100);
     // addSize = constrain(addSize, 10, 100);
    }//grow
      
 void shrink(){
   if(bSize==100){ // when size is 100 switch the boolean value to it's opposite
       grow=!grow;
     }
     if(bSize==10){
      grow=true; //when 10 grow the circle
     }
    }
}

Thanks for any and all help.

1 Like

This is how we define a class in JS:

Look at the sketches “Clickable Spawning Balls” & “Ball in the Chamber” for both Java Mode & p5.js on the link below:

More Java Mode to p5js conversion tips below:

2 Likes

Thanks. I think this will help with my issues. I really need a step by step conversion process. I will be able to work it out from this though. And I can write my own conversion tutorial for bits that I found difficult to follow or understand. Cheers!!

1 Like

Hello,

I was working through a similar experience with a member here:

You may be able to glean some insight and tips from that also.

I am new to P5.js also and always manage to work out a solution.

It was useful for me to also go through the resources and examples here:
https://p5js.org/

And related content on the Coding Train in YouTube; a search will get you there.

:)

2 Likes

Hi cheers for the constant help. I’ve decided to leave p5.js alone for now, as there is so much stuff I don’t yet know how to do in vanillia processing and I’m working from a book using it. I’ll come back to p5.js eventually. When I have a better grasp of the concepts in processing in general.
The link and coding train stuff you provided will help a lot with that!
Thanks!

1 Like