@K3N's upvoted answer's is right about one point : your problem doesn't come from the </canvas>
closing tag not shown in the inspector, but from your trial to get a 2D Context from a canvas where a webgl one was already initialized.
But,
While it's true that in HTML5 some elements don't need a closing tag, for canvas
Tag omission in text/html:
Neither tag is omissible.
Here is a small counter-proof to the other fiddle, since yes, <script>
content should be executed directly when met, and that modern browsers make the canvas element available in the DOM before they met the closing tag, these browsers are able to execute this simple script, (IE9 won't be) but this is certainly something you want to avoid :
canvas{border: 1px solid}
<canvas>
<p>I'm not there</p>
So it's just your inspector, or maybe internally your browser which doesn't show it, but you must write it in your markup.
(Actually, if we're talking about chrome, it's only the inspector which doesn't show it, you can see it by calling the outerHTML
property of the canvas element, the closing tag will be there.)
And for the solution,
What you are trying to do is to export the canvas as a static image.
For this, since toBlob
is an HTMLCanvasElement's method, you don't need the current context , you only need the last part of the answer you pointed us to in your question, and small adjustements :
As well explained in this answer by gman, webgl canvas is double buffered, and the drawing buffer will be cleared as soon as the browser can, except if the context was created with the preserveDrawingBuffer: true
argument of the getContext()
method.
But this argument will have a serious effect on performances, and the best recommended way, is to call the toBlob()
method, in the same call as your rendering, synchronously.
Since it is already really well explained there, I won't extend too much on this subject, but note that it also concerns toDataURL()
and some2dCanvas.drawImage(yourWebglCanvas,x,y)
.