How to pass arguments to new sketch in instance mode

Hey all,

Is there a way to pass an arguments array/object to a new sketch in instance mode?
I would like to pass settings to the new sketch, like information on who is it’s parent element. But this could also be used to send other settings like colors or dimensions of stuff to be drawn.

This was my first approach/guess

var p5Back = function( sketch, args ) {

	let parentDivId = args.parentId;
	
	console.log( parentDivId );
}

let sketch_01 = new p5( p5Back, ‘sketch_01’, { parentId: ‘sketch_01’ } );
let sketch_02 = new p5( p5Back, ‘sketch_02’, { parentId: ‘sketch_02’ } );

But this prints TypeError: args is undefined in the console.

Is there a way to pass arguments to the new sketch?

1 Like

I think I might already have found a solution for this:

function createNewSketch( args ){

    let p5Back = function( sketch ) {

        sketch.setup = function() {

            sketch.createCanvas( args.width, args.height);

        }

    };

    return new p5( p5Back, args.parentId );
}

let sketch_01 = createNewSketch(  { parentId: 'sketch_01', width: 200, height: 50 }  );
1 Like

This seems to be relevant: https://stackoverflow.com/questions/46290952/passing-in-extra-argument-to-new-p5-instance

Kf

1 Like