Calling Sketches

Is there a way to call a sketch from another sketch?

Thanks for any pointers.

Chris

Hi,

Calling a sketch from another sketch doesn’t really make sense.
The two important parts are the setup() and draw() functions so you want all of your drawing instructions to go there.

Let’s suppose you have two sketches :

// sketch1.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // rectangle
  rect(50, 50, 100, 100);
}
// sketch2.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // ellipse
  ellipse(50, 50, 100, 100);
}

Then you just combine the two sketches :

// sketch_combined.js

function setup() {
  createCanvas(400, 400);
}

function draw() {
  // ellipse
  ellipse(50, 50, 100, 100);

  // rectangle
  rect(50, 50, 100, 100);
}

This is possible when your sketches are small but when they get bigger, the easiest way is to encapsulate them in classes and functions and then call them in a single sketch.

I agree. I will combine the code with different state variables to run the code I want to see.

1 Like