In HTML5 on the desktop, the viewport is the part of the browser window that has
the document you want to view. This is the ‘layout viewport’ of the window,
which is the part without browser chrome.
The top-level window aso has a “visual viewport” which defines what part of the
layout viewport to display, In effect, this is used to implement “magnifying”,
but doesn’t support “zooming out” past 100% of the layout view.
In computer graphics, visual objects are modeled mathematically using a
“coordinate system” to describe where objects are. Coordinate systems have
“units” (pixels, inches, meters) along a set of “coordinate axes” (x, y, etc) to
place elements into the “coordinate space”. The center of this space is called
the “origin” where each axis is zero.
This is sometimes called the “handedness” of a coordinate system. 3D Computer
graphics systems tend to use the “right handed” coordinate systems:
Traditional 2D computer graphics systems tend to use its own convention:
- origin is at the top-left of the screen, angle origin is on negative Y axis
- positive-x goes to the right, positive-y goes DOWN from the origin
- positive-z goes goes AWAY from you, usually
- positive angle start at 0 at the top and go CLOCKWISE
World Definition
Creating a “world” inhabited by of visual objects consists of giving each of
them its own coordinates in the world’s “coordinate space”. However, there are
many coordinate spaces in a computer graphics application!
-
On the Modeling side, your visual objects may have their own “local
coordinates”, with the origin for each object in the center or at its point of
rotation.
-
On the Device side, your screen has its own coordinates system that
differs from your world.
There are many “transformations” that occur between coordinate system data. For example,
a 3D game on a web browser might have:
IN WORLD SPACE
- elements designed using their local coordinate system…
- mapped to a place in the world coordinate system…
IN VIEW SPACE
- mapped to a view that limits how much as being seen of the world…
- mapped from 3D to 2D coordinates using “2d projection”
- mapped to viewport that is all the “visible 2D data”…
IN APPLICATION WINDOW SPACE
- mapped to web page with its own page coordinates…
- mapped to a browser window with its own scrollable area…
- mapped to a brower’s “zoom factor”…
IN DEVICE SPACE
- mapped to the browser host device’s screen coordinates
CSS Specifics
Coordinate Systems of the Browser
undefined
For web browsers, the basic coordinate system references are defined by the
window and the document that is shown inside the window. The elements
inside the document also have their own coordinate systems.
There seem to be these coordinate system contexts used consistently in the documentation:
The origin is top-left of containing VIEWPORT, which is the part
of the browser that shows the document.
The origin is top left of Document, and coordinates of the
elements in the page are relative to it. This is displayed inside the Client area.
The origin is top-left of the display screen. The Client area is
some subset of displayed somewhere on the screen in a window.
A Page has a root Element in document.documentElement, which has the same coordinate system conventions as
other elements (see next section):
The origin is top-left corner of the Element. The origin is inset by the
node’s padding-left and padding-top, so 0,0 may not be where you expect.
CSS uses 2D coordinate system conventions:
- increasing x values go to the right
- increasing y values go down
- angles are clockwise, with the angle origin at the top
- the
z-index value stacks positive values before negative values
Element Properties and Dimensions
Every element in an HTML document has a ‘margin’, ‘border’, and ‘padding’ that affects the
size of a given box. These properties are always inspectable:
the size of the entire element, not including margin.
the size of the content area inside the element.
Because elements can contain child elements as scrollable content with hidden
overflow, these properties can help us compute offsets:
the size of the element’s child contents, even if overflow is hidden.
The meaning of these terms are also explained
here.
The Page Coordinates correspond to
You can ALSO use Javascript’s getBoundingClientRect() to return a DOMRect data
structure of the “rendered size” on the page in the Client Coordinates context:
the origin, “typically the top-left corner”
the origin, “typically the top-left corner”
if width less-than 0, returns x
if height greater-than 0, returns y
Event Coordinate Systems
Events always report where they happened within their event target:
coordinates inside the event target
coordinates relative to browser viewport origin
coordinates relative to the document origin
coordinates relative to the screen origin
As mentioned before, calling Element.getBoundingClientRect() returns the
client-relative coordinates, so this can be used as-is with the clientX, clientY information in the event.
But what if you need the coordinates relative to another element? Just use
getBoundingClientRect() on both elements, and do some arithmetic.
Pinch to Zoom Display Scaling
Every browser window object (and iframes) has the main “Layout Viewport” that
we’ve discussed above, but there is a second property for the root viewport
called visualViewport used to implement things like “pinch to zoom/pan”.
It specifies a subarea of the Layout Viewport to magnify/pan.
where within the layoutViewport
size of the visualViewport
offset from starting visualViewport
Operating System Display Properties
You can also access the dimensions of the screen through the window.screen object:
available width usable by browser window
available height minus operating system stuff like taskbar
display color palette (24bit = 16.7million colors)
bits per pixel (256 colors = 8, 16.7 million colors = 24)
Obsolete Methods?
When searching for information about browser element dimensions, you get a lot
of old stuff relating to jQuery methods like:
- innerWidth, innerHeight
- outerWidth, outerHeight
These also exist as window properties, maybe for compatibility, maybe because
these are operating system window properties and not browser window
properties?
Other Element Typed?
The SVG and Canvas types may have their own conventions, and I’m not sure
different they are compared to CSS. Additional, there is the WebGL display
context with its own conventions too.
HTML can contain SVGElements, Canvas elements, and WebGL elements inside of
Canvas elements. Hopefuly they all use similar conventions.