Let's say I have a HTML/JS project which involves interaction with a canvas. Resizing, drawing, retrieving the current state, etc.
Obviously I should separate these into several functions. However, I'm unsure of the best way to go with this. There is only one canvas, so every function call will be acting on the same canvas every time.
With this in mind, would it better to pass in the canvas element (or a jQuery object, to be specific... not really relevant to the question though) as a parameter or not? For example:
function setInitialCanvasSize(canvas){
canvas.width = 100;
canvas.height = 100;
}
setInitialCanvasSize($("#canvas"));
vs
function setInitialCanvasSize(){
$("#canvas").width = 100;
$("#canvas").height = 100;
}
setInitialCanvasSize();
Which is better and why?
The first one feels like it should be better. But there's a small part of me just thinking "Why bother?", and to be honest I can't really come up with a satisfactory answer.