Let's suppose that I have this piece of code:
function drawToCanvas() {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
context.rect(10, 10, 100, 100);
context.fill();
const visibleCanvas = document.getElementById('mycanvas');
const visibleContext = visibleCanvas.getContext('2d');
visibleContext.drawImage(canvas, 0, 0);
return visibleCanvas;
}
From the point of view of the LoD, the first part of this function is essentially equivalent to
document.createElement('canvas').getContext('2d').rect(10, 10, 100, 100);
Where the second part is not so different.
How should I use dependency injection to prevent this, and at the same time preserve the reference to the canvas and its context?