As you know, the <canvas>
element in HTML5 has an internal size, set via elm.width
and elm.height
where elm
is the DOM element.
The element within the DOM itself can have a different box size, set via CSS.
When the two differ, the browser automatically stretches the contents of the <canvas>
to fit the content box size. Can this behaviour be adjusted without an extra element? Can it be set to something like background-size: contain
so that it keeps the aspect ratio of the inner canvas size?
As far as I know, there is no standard way to do this, so webkit-specific hacks are also accepted. But if there is no way to do it at all, what's the most elegant to do it with a wrapper element (we all know that we can wrap the element and adjust it via JS on resize)?
The one solution I can think of is actually using background-size: contain
and background: element()
but this is Firefox-specific, and I am looking for a WebKit solution.
Side question, can a difference in element box and canvas internal sizes impact performance? If so, how much?