The way I will do this is to update the cloned canvas after drawing on the main canvas.
Based on your current code, I would first like to return a cloned canvas instead of the old canvas. So you have a link to it.
function cloneCanvas(canvas, posx, posy, size,) { ...
Next, looking at the line:
cloneCanvas(canvas, window.posx, window.posy, size);
I see that you are using your own posx and posy variables. I would get rid of them, since you always want to copy the entire canvas. (In this case). Change the line as follows
var clone = cloneCanvas(canvas, 0, 0, size);
Then I wrote a little helper function to link the two canvases.
function drawFromSource(source, destination) { return function() { var context = destination.getContext('2d'); context.clearRect(0, 0, destination.width, destination.height); context.drawImage(source, 0, 0); } }
This function returns a callback that, when called, draws the original canvas onto the cloned canvas.
You initialize it as follows:
var updateClone = drawFromSource(canvas, clone);
Last but not least, you need to add the newly created callback to the draw function. Immediately after you draw your main canvas.
function draw(canvas, posx, posy) { var context = canvas.getContext('2d'); if (md) { context.fillRect(posx, posy, size, size) updateClone(); } };
Voila! Here is a link to the working code, I have moved part of your code.
https://jsfiddle.net/30efdvz3/5/
Just for fun. The tiled version just changes the "numberofTiles"
https://jsfiddle.net/30efdvz3/3/