360° video
@mattebox/player-360@0.2.0Needs @mattebox/player >=0.17.0 and mattebox ^0.19.0.
@mattebox/player-360 shows a 360° video from inside. The picture is drawn
onto the inside of a sphere around the viewer, and the viewer looks around
it. It needs WebGL, which every browser down to Chromium 76 has.
The element
<mattebox-player src="https://example.com/vod/360/master.m3u8" controls="custom">
<mbx-360></mbx-360>
<mbx-control-bar>
<mbx-play-button></mbx-play-button>
<mbx-seek-bar></mbx-seek-bar>
</mbx-control-bar>
</mattebox-player>
<script type="module">
import '@mattebox/player';
import '@mattebox/player-360';
</script>
Place <mbx-360> first, right after the video. It covers the whole player,
and the controls placed after it stay on top.
Over Safari’s own HLS the video shows flat. So while <mbx-360> is inside
a player, the player’s default for native-hls is off: Safari plays HLS
through the engine. Set native-hls="on" to give Safari its own HLS back,
for a player whose sources are all flat.
| The viewer | What happens |
|---|---|
| Drags the picture | The view turns. The picture follows the pointer |
| Taps without a drag | The video plays or pauses, as a click on a flat video does |
| Turns the wheel | The view zooms in or out, from 30° to 100° tall |
| Leans the right stick | A game controller turns the view, faster the further it leans |
| Turns a phone | The view turns with the device. See Motion |
While the view shows, the video keeps a 16:9 box. A 360° picture is 2:1, and the view is a window into it.
The poster shows as on a flat video: until the programme plays or seeks. Before that the sphere stays hidden. On a new source, the last programme’s picture goes as soon as the video empties.
How the player knows
The player carries projection while its source is 360°. It comes from the
first of these that says so.
- The
projectionattribute on<mbx-360>. It wins over everything.nonekeeps a 360° source flat. - The source’s
projection, which your page or your connector sets. Use it when your content API knows: the stream often does not say. - The engine: HLS
REQ-VIDEO-LAYOUT, the DASH projection descriptor and the boxes of an MP4 init segment (engine guide, chapter 03). The player reads the engine’sprojectioneither way.
player.sources = [{ url: 'https://example.com/360/master.m3u8', projection: 'equirectangular' }];
projection |
The picture |
|---|---|
equirectangular |
The whole sphere, 2:1 |
half-equirectangular |
The half in front of the viewer, 1:1. The back is black |
A connector
SRG SSR’s content API marks a 360° resource with presentation: 'VIDEO_360', and its streams carry no projection of their own. The demo’s
connector turns the one into the other:
const sphere = resource.presentation === 'VIDEO_360' ? { projection: 'equirectangular' } : {};
return { url, type: resource.mimeType, ...sphere };
Motion
On a phone or a tablet, the view turns with the device. It is on by default, because looking around with the device is what a viewer expects of a 360° video on a phone.
When the system asks for reduced motion (prefers-reduced-motion: reduce),
motion starts off. A view that sways with the hand can make a viewer with a
vestibular disorder ill, and neither the browser nor the system stops the
motion events for them. The element follows the setting when it changes
while the page is open.
motion |
Motion starts |
|---|---|
| absent | On, or off when the system asks for reduced motion |
on |
On, even when the system asks for reduced motion |
off |
Off |
motion="on" overrides a setting the viewer made for their health. Set it
only with <mbx-motion-button> in the bar, so the viewer can turn it off.
- The element listens to
deviceorientationonly while the view shows. - The first event keeps the yaw the view has, so the view does not jump. The pitch and the roll follow the device from then on, level with the ground.
- A drag, the wheel, the stick and the
yawandpitchproperties still work. Their turn adds to the device’s, and stays as the device moves. - A computer without the sensors sends no angles, and nothing changes.
- A new source starts straight ahead again.
iOS 13 and later sends no motion before the page asks, and lets a page ask only during a touch or a click. The element asks at the viewer’s first touch on the view, once. That touch also plays or pauses, as any tap does. A refusal turns motion off.
The motion button
<mbx-motion-button> lets the viewer turn the motion on or off, for a
viewer who wants to look around by touch only. Place it in the bar:
<mattebox-player src="https://example.com/vod/360/master.m3u8" controls="custom">
<mbx-360></mbx-360>
<mbx-control-bar>
<mbx-play-button></mbx-play-button>
<mbx-seek-bar></mbx-seek-bar>
<mbx-motion-button></mbx-motion-button>
</mbx-control-bar>
</mattebox-player>
- It is a toggle: one name, and
aria-pressedsays whether the motion turns the view. - It is hidden while no 360° view shows, and on a device without motion sensors, such as a computer. It shows once the device sends angles, or at once on iOS, which sends none before the page asks.
- On iOS its click asks for the permission. A refusal leaves it off.
- The viewer’s choice lasts as long as the element. It is not saved: a
viewer who turned it off to try it would not remember it on the next
visit. A page that sets
motionreplaces the choice. - To force one way, set
motionand leave the button out.
| Name | Kind | What it does |
|---|---|---|
label |
attribute | The button’s name. Turn with the device |
icon-on |
slot | The glyph while the motion turns the view |
icon-off |
slot | The glyph while it does not |
A page with a control of its own calls setMotion() on <mbx-360> from a
click, reads motionOn and motionAvailable, and listens to
motionchange.
Headsets
<mbx-vr-button> shows the view in a VR headset, through WebXR. Place it in
the bar:
<mattebox-player src="https://example.com/vod/360/master.m3u8" controls="custom">
<mbx-360></mbx-360>
<mbx-control-bar>
<mbx-play-button></mbx-play-button>
<mbx-seek-bar></mbx-seek-bar>
<mbx-vr-button></mbx-vr-button>
</mbx-control-bar>
</mattebox-player>
- The button is hidden unless the browser has WebXR, a headset can show an
immersive-vrsession, and<mbx-360>shows a 360° source. A headset plugged in later shows it. - In the headset the viewer looks around with their head. The view starts at the yaw the screen had.
- The picture is the same for both eyes. Stereo pictures (top and bottom, side by side) are not split per eye.
- The session ends from the headset’s own menu, from the button, or when the source becomes flat. The screen view draws again then.
- Without WebXR, which includes every TV, the button stays hidden and nothing else changes.
| Name | Kind | What it does |
|---|---|---|
label-enter |
attribute | The button’s name while the screen shows the view. Enter VR |
label-exit |
attribute | The button’s name while the headset shows it. Exit VR |
icon-enter |
slot | The glyph while the screen shows the view |
icon-exit |
slot | The glyph while the headset shows it |
The words come in the 11 languages of chapter 10, from
@mattebox/player-360/locales/<language>. The ids are enterVr,
exitVr and deviceMotion, in the Player360Messages type.
import { MatteboxPlayerElement } from '@mattebox/player';
import words from '@mattebox/player/locales/de';
import words360 from '@mattebox/player-360/locales/de';
MatteboxPlayerElement.addLocale('de', words, words360);
A page with a control of its own calls enterVR() on <mbx-360> from a
click. The browser starts a session only during a click or a key press.
When the view does not show
The flat video plays instead, and unavailable on <mbx-360> says why.
unavailable |
Why |
|---|---|
drm |
The source is encrypted. A browser does not let a page draw a DRM picture: no player can |
source |
Another site’s video, served without CORS. Set crossorigin on the player and allow CORS |
webgl |
The browser gives no WebGL |
Attributes and properties
| Name | Kind | What it does |
|---|---|---|
projection |
attribute | Forces a projection, or none. See How the player knows |
fov |
attribute | The field of view a source starts with, in degrees, 75 by default |
motion |
attribute | on or off: whether the device’s motion starts turning the view. See Motion |
active |
attribute | Set by the element while the view shows |
vr |
attribute | Set by the element while a headset shows the view |
unavailable |
attribute | Set by the element. See When the view does not show |
yaw |
property | Degrees the view is turned right of the picture’s centre |
pitch |
property | Degrees the view looks up |
fov |
property | The vertical field of view in degrees, from 30 to 100 |
immersive |
property | True while a headset shows the view |
enterVR() |
method | Shows the view in a headset. Call it from a click. See Headsets |
exitVR() |
method | Ends the headset session |
motionOn |
property | True while the device’s motion turns the view, as the viewer or motion set it |
motionAvailable |
property | True on a device with motion sensors |
setMotion(on) |
method | The viewer’s choice, until the page sets motion. Call it from a click on iOS |
motionchange |
event | motionOn or motionAvailable changed. It bubbles to the player |
A new source starts straight ahead, at fov.
Subtitles
The browser draws subtitles inside the video, under the view. The element writes the active cues of the showing track over the view instead, without their styling.
Parts
| Part | What it is |
|---|---|
view |
The canvas the sphere is drawn on |
subtitles |
The subtitles written over the view |
Not yet
- Turning the view with the keyboard. The arrows move focus between the
controls, so the view needs keys of its own. Set
yawandpitchfrom your own keys meanwhile. - Stereo pictures, one half per eye, in a headset.