Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.
For web applications, the Screen Orientation API exposes the type and angle of a device's current screen orientation, and dispatches events if the device's screen orientation changes. This allows web applications to programmatically adapt the user experience for many possible screen orientations in concert with CSS. If supported and if certain preconditions are met, the API also provides the ability to lock the screen orientation. This is useful in applications such as computer games where users physically rotate the device but the screen orientation itself mustn't change.
This section describes the status of this document at the time of its publication. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.
This document is a work in progress.
This document was published by the Web Applications Working Group as a Working Draft using the Recommendation track.
Publication as a Working Draft does not imply endorsement by W3C and its Members.
This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 2 November 2021 W3C Process Document.
This section is non-normative.
In this example clicking the "Lock" button requests to go into fullscreen and then locks the screen to the opposite orientation. clicking the "unlock" button unlocks the screen.
<script>
function updateLockButton() {
const lockButton = document.getElementById("button");
const buttonOrientation = getOppositeOrientation();
lockButton.textContent = `Lock to ${buttonOrientation}`;
}
function getOppositeOrientation() {
const { type } = screen.orientation;
return type.startsWith("portrait") ? "landscape" : "portrait";
}
async function rotate(lockButton) {
if (!document.fullscreenElement) {
await document.documentElement.requestFullscreen();
}
const newOrientation = getOppositeOrientation();
await screen.orientation.lock(newOrientation);
updateLockButton(lockButton);
}
screen.orientation.addEventListener("change", updateLockButton);
window.addEventListener("load", updateLockButton);
</script>
<button onclick="rotate(this)" id="button">
Lock to...
</button>
<button onclick="screen.orientation.unlock()">
Unlock
</button>
A browsing context A is called a descendant browsing context of a browsing context B if and only if B is an ancestor browsing context of A.
To lock the screen orientation to an orientation means that the screen can only be rotated by the user to a specific screen orientation (possibly at the exclusion of other orientations). For example, locking the orientation to landscape means that the screen can be rotated by the user to landscape-primary and maybe landscape-secondary, but not to a portrait orientation.
A screen can be in, or locked to, one of the following screen orientations:
The screen of the output device has a current orientation type and a current orientation angle.
The current orientation type is the screen orientation
of the output device, represented as a OrientationLockType
.
The current orientation angle is the angle, in degrees, that the screen is rotated clockwise from its natural orientation.
When a browsing context is created, the user agent MUST update the orientation information for that browsing context. The relationship between the current orientation type and the current orientation angle MUST be kept consistent.
The screen orientation values table presents the possible value for the current orientation type and current orientation angle. The table specifies the primary and secondary values that are determined by the device's natural orientation and the possibilities available to the user agent for setting the other primary and secondary orientation values.
In the natural orientation is... | Primary Orientation 1 | Secondary Orientation 1 | Primary Orientation 2 | Secondary Orientation 2 |
---|---|---|---|---|
Portrait |
portrait-primary angle 0
|
portrait-secondary angle 180
|
landscape-primary angle 90 or angle 270
|
landscape-secondary Set at the angle not used for landscape-primary |
Landscape |
landscape-primary angle 0
|
landscape-secondary angle 180
|
portrait-primary angle 90 or angle 270
|
portrait-secondary The angle not used for portrait-primary. |
angle
and
type
relationship
Never assume any
cross-devices relationship between the screen orientation type and
the screen orientation angle. Any assumption would be wrong given
that a device might have 90
and 270
as the angles for
landscape
types but another device will have 0
and 180
,
depending on its natural orientation. Instead, it is
recommended to check during runtime the relationship between angle
and type.
WebIDLpartial interface Screen {
[SameObject] readonly attribute ScreenOrientation
orientation
;
};
The Window
object has an associated Screen
, which is a
Screen
object.
WebIDL[Exposed=Window]
interface ScreenOrientation
: EventTarget {
Promise<undefined> lock
(OrientationLockType orientation);
undefined unlock
();
readonly attribute OrientationType type
;
readonly attribute unsigned short angle
;
attribute EventHandler onchange
;
};
Internal Slot | Description |
---|---|
[[angle]] |
Represents the screen's last known current orientation angle in degrees as an unsigned short .
|
[[type]] |
Represents the screen's last known current orientation type
of the screen as an OrientationType enum value.
|
The user agent MAY require a document and its associated browsing context to meet one or more pre-lock conditions in order to lock the screen orientation in order to allow an orientation lock. See, for example, 9.1 Interaction with Fullscreen API).
The user agent MAY reject all attempts to lock the screen orientation if the platform conventions do not expect web applications to change the screen orientation. For example, on most desktop platforms, web applications can not change the screen orientation.
When the lock
()
method is invoked with
orientation, the user agent MUST run the
the following steps:
Document
.
InvalidStateError
" DOMException
.
NotSupportedError
" DOMException
and abort these steps.
NotSupportedError
" DOMException
and abort these steps.
"SecurityError"
DOMException
and abort
these steps.
[[orientationPendingPromise]]
is not null
:
[[orientationPendingPromise]]
.
AbortError
" DOMException
.
[[orientationPendingPromise]]
to a new promise.
[[orientationPendingPromise]]
and in parallel:
[[orientationPendingPromise]]
is
null
, continue.
[[orientationPendingPromise]]
with
"AbortError"
DOMException
.
[[orientationPendingPromise]]
to null
.
[[orientationPendingPromise]]
with an "AbortError
"
DOMException
.
When the unlock
()
method is invoked, the user agent MUST
run the following steps:
InvalidStateError
" DOMException
.
Document
to the default screen orientation.
unlock
()
does not return a promise because it is equivalent to
locking to the default screen orientation which might or might
not be known by the user agent. Hence, the user agent
can not predict what the new orientation is going to be and even if
it is going to change at all.
When getting, return this's [[angle]]
.
angle
represents how far the user has turned the device
counterclockwise from the natural orientation. When the device is
rotated 90° counterclockwise, the screen compensates by rotating
90° clockwise, so angle
returns 90
.
The screen orientation values table shows how the angle changes depending on the how the device is rotated.
The onchange
attribute is an event handler whose
corresponding event handler event type is change.
WebIDLenum OrientationLockType
{
"any
",
"natural
",
"landscape
",
"portrait
",
"portrait-primary
",
"portrait-secondary
",
"landscape-primary
",
"landscape-secondary
"
};
The OrientationLockType enum represents the screen orientations to which a screen can be potentially locked to.
any
" enum value represents the any orientation,
natural
" enum represents the natural
orientation,
landscape
" enum represents the landscape
orientation,
portrait
" enum represents the portrait
orientation,
portrait-primary
" enum represents the
portrait-primary orientation,
portrait-secondary
" enum represents the
portrait-secondary orientation,
landscape-primary
" enum represents the
landscape-primary orientation,
landscape-secondary
" enum represents the
landscape-secondary orientation.
WebIDLenum OrientationType
{
"portrait-primary
",
"portrait-secondary
",
"landscape-primary
",
"landscape-secondary
"
};
The OrientationType enum represents the actual current screen orientation that the screen is in irrespective of which lock is applied.
portrait-primary
" enum represents the
portrait-primary orientation,
portrait-secondary
" enum represents the
portrait-secondary orientation,
landscape-primary
" enum represents the
landscape-primary orientation,
landscape-secondary
" enum represents the
landscape-secondary orientation.
Internal Slot | Description |
---|---|
[[orientationLock]] |
The [[orientationLock]] is the active orientation lock as a OrientationLockType or null .
|
[[orientationPendingPromise]] |
Either null or a Promise . When assigned a Promise ,
that promise represents a request to lock the screen
orientation.
|
The steps to update the orientation information of a browsing context context:
OrientationType
enum that matches
the screen orientation of context.
Screen
's
orientation
attribute.
[[type]]
internal
slot to orientation.
[[angle]]
internal
slot to angle.
The steps to lock the orientation of document to orientation are as follows.
Document
's [[orientationLock]]
to orientation.
[[orientationLock]]
, abort these steps.
Document
's
current orientation type will be equal to orientation.
Document
's
top-level browsing context's screen orientation from
changing until those steps are run again.
Document
's current orientation type is not part
of orientations, change how the viewport is drawn such as the
Document
's current orientation type will be equal to one
of orientations' values.
An orientation lock is in place when a document has an active orientation lock.
The steps to determine the active orientation lock are as follows:
[[orientationLock]]
.
[[orientationLock]]
.
[[orientationLock]]
,
unless stated otherwise by the active platform conventions.
Whenever the active orientation lock changes, the
user agent MUST run the steps to lock the orientation
of the Document
to the Document
's
[[orientationLock]]
.
Whenever a top-level browsing context is navigated, the user agent MUST return the screen to the default screen orientation.
Whenever the viewport's angle changes, the user agent MUST run the following steps as part of the next animation frame:
lock
()
, the task MUST be annotated
with process user orientation change
when running the next
step.
Screen
's
orientation
attribute.
[[orientationPendingPromise]]
is
not null
:
[[orientationPendingPromise]]
with
undefined
.
[[orientationPendingPromise]]
to null
.
The page visibility change steps given document are as follows:
Screen
.
orientation
's [[type]]
or
[[angle]]
respectively, queue a task to on
the user interaction task source to:
orientation
's
[[angle]]
to newAngle.
orientation
's
[[type]]
to newType.
orientation
attribute.
This section explains how this specification interacts with other related specifications of the platform.
As a pre-lock condition, a user agent SHOULD restrict locking the screen orientation to simple fullscreen documents.
This section is non-normative.
The Web Application Manifest allows web applications to set the
Document
's default screen orientation.
This section is non-normative.
The Web Content Accessibility Guidelines (WCAG) 2.1 includes a Success Criterion related to screen orientation.
The intent of this Success Criterion is to ensure that all essential content and functionality is available regardless of the display orientation (portrait or landscape). Some websites and applications automatically set the screen to a particular display orientation and expect that users will respond by rotating their device to match.
However, some users may have their devices mounted in a fixed orientation (e.g. on the arm of a wheelchair). Therefore, websites and applications need to support both orientations by making sure essential content and functionality is available in each orientation. While the order of content and method of functionality may have differences the content and functionality must always be available. When a particular orientation is essential, the user needs to be advised of the orientation requirements.
This section is non-normative.
The screen orientation type and angle of the device can be accessed with the API specified in this document, and can be a potential fingerprinting vector.
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key words MAY, MUST, and SHOULD in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.
WebIDLpartial interface Screen {
[SameObject] readonly attribute ScreenOrientation
orientation
;
};
[Exposed=Window]
interface ScreenOrientation
: EventTarget {
Promise<undefined> lock
(OrientationLockType orientation);
undefined unlock
();
readonly attribute OrientationType type
;
readonly attribute unsigned short angle
;
attribute EventHandler onchange
;
};
enum OrientationLockType
{
"any
",
"natural
",
"landscape
",
"portrait
",
"portrait-primary
",
"portrait-secondary
",
"landscape-primary
",
"landscape-secondary
"
};
enum OrientationType
{
"portrait-primary
",
"portrait-secondary
",
"landscape-primary
",
"landscape-secondary
"
};
angle
attribute for ScreenOrientation
§4.5[[angle]]
internal slot for ScreenOrientation
§4.1"any"
enum value for OrientationLockType
§5."landscape"
enum value for OrientationLockType
§5.lock()
method for ScreenOrientation
§4.2"natural"
enum value for OrientationLockType
§5.onchange
attribute for ScreenOrientation
§4.6orientation
attribute for Screen
§3.[[orientationLock]]
internal slot for Document
§7.1[[orientationPendingPromise]]
internal slot for Document
§7.1"portrait"
enum value for OrientationLockType
§5.ScreenOrientation
interface
§4.type
attribute for ScreenOrientation
§4.4[[type]]
internal slot for ScreenOrientation
§4.1unlock()
method for ScreenOrientation
§4.3Screen
interface
Document
interface
EventTarget
interface
Document
)
EventHandler
Document
)
Document
)
Window
interface
list
)
AbortError
exception
DOMException
interface
[Exposed]
extended attribute
InvalidStateError
exception
NotSupportedError
exception
Promise
interface
[SameObject]
extended attribute
exception
)
undefined
type
unsigned short
type
Thanks Christophe Dumez, Anne van Kesteren, Chundong Wang, Fuqiao Xue, and Chaals McCathie Nevile for their useful comments.
Special thanks to Chris Jones and Jonas Sicking for their contributions to the initial design of this API.
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in: