[whatwg] [2D Canvas] Do canvases in documents without a browsing context need to be rendered?

Rik Cabanier cabanier at gmail.com
Wed Mar 5 20:42:32 PST 2014

On Mon, Mar 3, 2014 at 9:57 AM, Justin Novosad <junov at google.com> wrote:

> Hello whatwg,
> Say you create a new document using
> document.implementation.createHTMLDocument(), you get a document without a
> browsing context. This means that style and layout will never be calculated
> on the document.  Some of those calculations are context dependent, so they
> can't even be resolved.  Now, what about canvas elements? If JS code draws
> to a canvas that is in a document with no browsing context, what should
> happen?  Should the draw calls produce pixels?  Even though the canvas is
> not displayed, it can still be used as an image source. So technically, as
> currently specified, such canvases should be rendered.  Drawing to a canvas
> in a context-less document is unfortunately a little bit problematic
> because some aspects of canvas painting (drawing text in particular) are
> affected by the browsing context.  For example, there is no locale for font
> family resolution and it is not possible to resolve font sizes in physical
> length units unless the document is associated with a view.
> My 2 cents: specifying fallback behaviors for all use cases that are
> context dependent could be tedious and I have yet to see a real-world use
> case that requires being able to paint a canvas in a frame-less document.
>  Therefore, I think the spec should clearly state <canvas> elements that
> are in a document without a browsing context are unusable.  Not sure what
> the exact behavior should be though.  Should an exception be thrown upon
> trying to use the rendering context? Perhaps canvas draws should fail
> silently, and using the canvas as an image source should give transparent
> black pixels?

Testing all browsers (except IE
since document.implementation.createHTMLDocument() doesn't work) they seem
to handle canvas contexts with no browsing context except when you use text.
Chrome crashes, firefox throws an exception and Safari draws the text with
a very small scale [1]

Since this is such a small edge case, it seems reasonable to disallow the
creation of a 2d context and throw an exception. Going through all the APIs
and flagging them as suitable for a context-less environment is not really
worth the effort.

I'm curious how you found this. Was this reported by an author? If so, why
was he trying to do it this way?

1: http://codepen.io/anon/pen/tsemK

More information about the whatwg mailing list