+ 1
Wrap your JavaScript into a window.onload function:
"window.onload = function() { <your code> });"
It will execute then.
EDIT: That's because your JavaScript is inserted in a script tag into the head of the generated frame. Your canvas is created later on.