The answer that has been upvoted by @K3N is correct about one thing: the issue is not with the </canvas>
closing tag missing in the inspector, but rather with attempting to obtain a 2D Context from a canvas where a webgl context was already initialized.
However,
While it's true that certain elements in HTML5 do not require a closing tag, such as some elements, for canvas
Tag omission in text/html:
Neither tag is omissible.
Here is an example disproving this notion using another fiddle. While <script>
content should be executed immediately upon being encountered, and modern browsers make the canvas element available in the DOM before reaching the closing tag, enabling these browsers to execute the script, (although IE9 may not) this practice is generally discouraged :
canvas{border: 1px solid}
<canvas>
<p>I'm not there</p>
Therefore, despite what your inspector shows you, or potentially internally within your browser, it is imperative that you include it in your markup.
(In fact, particularly with Chrome, only the inspector doesn't display it - you can view it by accessing the outerHTML
property of the canvas element, where the closing tag will be visible.)
And now, onto the solution,
You are aiming to export the canvas as an image file.
To do so, since toBlob
is a method of the HTMLCanvasElement, you do not require the current context. Simply follow the final steps outlined in the answer referenced in your question, with minor adjustments :
As elucidated in this particular answer given by gman, a webGL canvas operates on a double-buffer system, resulting in the drawing buffer being cleared promptly by the browser, unless the context was established utilizing the preserveDrawingBuffer: true
argument in the getContext()
method.
Nevertheless, implementing this argument can severely impact performance, hence why the preferred approach would involve executing the toBlob()
method synchronously within the same rendering call.
Since the aforementioned details have been comprehensively explained, I shall refrain from further elaboration on this topic. However, do take note that this principle also pertains to methods like toDataURL()
and
some2dCanvas.drawImage(yourWebglCanvas,x,y)
.