Since you access the camera through an element <video>, you must use the attributes videoWidthandvideoHeight to calculate the aspect ratio. These values reflect the actual size of the source media stream and may differ from the attributes widthand the heightitem <video>displayed on the page.
This approach seems to work well in Chrome and Firefox Nightly.