I am using instance mode for a p5js project and I am trying to figure out the best way to keep my functions organised.
Here is how I had my code organised originally:
var rotateSketch = function(p){
p.setup = function() {
p.rotateCanvas = p.createCanvas(800, 600);
p.rotateCanvas.parent('toyContainer');
// other set up bits
};
p.draw = function(){
// loads of stuff here,
// calling functions etc
};
// loads of functions defined here, namespaced using p.
// e.g ...
p.sendMyPoints = function() {
// Stuff happens here
}
// Also classes defined here
}
var ps = new p5(rotateSketch);
My program has a lot going on and the definition of rotateSketch grew to over 500 lines and is a nightmare to scroll through. I would like to move the functions and classes I am defining after setup and draw to separate js files to keep them organised.
I am able to do this, I have created separate files and I have tried defining functions there like so
function drawSomeStuffExample(){
ps.fill(100,100,100);
ps.rect(50,50,100,100);
// ... etc
}
This does work and I’m able to call these functions from within p.setup and p.draw, just using drawSomeStuffExample(); and it is working BUT I just feel like I’m doing it a bit wrong.
Shouldn’t my function drawSomeStuffExample sit within rotateSketch? Is there a way to do that other than having a huge long definition for rotateSketch all in one file? Or does it just not matter that drawSomeStuffExample (and all my other functions) are no longer namespaced to rotateSketch?
Thank you for any insight!