In https://stackoverflow.com/a/44795246/3416774, the essence of the code is:
network.on("afterDrawing", function (ctx) {
var dataURL = ctx.canvas.toDataURL();
document.getElementById('canvasImg').src = dataURL;
});
How can it run? ctx isn't defined anywhere. In the Vis.js' documentation, the afterDrawing event has this description:
| Name | Properties | Description |
|---|---|---|
| afterDrawing | canvas context | Fired after drawing on the canvas has been completed. Can be used to draw on top of the network. |
ctx is short for canvas context. So when the method on() completes, it will return the canvas context so the function can pick up. This is a concept called callback functions.