I would like to be able to render multiple games from different javaScript files in the same canvas element, alternating them with the press of a button/key. I have done this before but writing all the code in one script. The easiest way would be for every script to rely on a global variable number and draw when their number is called.
For example:
if(pnum === 0){
//Draw the page colour
ctx.fillStyle = pcolour[pnum];
ctx.fillRect(0,0, w,h);
ctx.fillStyle = pcolour[1];
ctx.fillText("Wubbalubbadubdub", 100,100);
}else ctx.clearRect(0,0,w,h);
and:
if(pnum === 1){
ctx.fillStyle = pcolour[pnum];
ctx.fillRect(0,0, w, h);
ctx.fillStyle = pcolour[0];
ctx.fillText("It happened", 100,100);
}else ctx.clearRect(0,0,w,h);
Where:
var pnum = 0;
var pcolour = [
"red",
"blue"
];