document.write
writes to the document... which is closed by the time window.onload
fires and therefore your entire page is nuked.
Try canvas.fillText()
instead, for drawing text to the canvas.
As an aside, the variable name canvas
is misleading. Typically ctx
is used to refer to the context.