UNPKG

12.1 kBMarkdownView Raw
1<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3
4- [videojs-sprite-thumbnails](#videojs-sprite-thumbnails)
5 - [Compatibility](#compatibility)
6 - [Features](#features)
7 - [Installation](#installation)
8 - [Usage](#usage)
9 - [`<script>` Tag](#script-tag)
10 - [Browserify/CommonJS](#browserifycommonjs)
11 - [RequireJS/AMD](#requirejsamd)
12 - [CDN](#cdn)
13 - [Configuration](#configuration)
14 - [Three ways to configure the location of image assets](#three-ways-to-configure-the-location-of-image-assets)
15 - [The idxTag function](#the-idxtag-function)
16 - [Individual thumbnails](#individual-thumbnails)
17 - [Initialization](#initialization)
18 - [Disabling and enabling the plugin](#disabling-and-enabling-the-plugin)
19 - [Playlist example](#playlist-example)
20 - [CSS state classes](#css-state-classes)
21 - [Debugging](#debugging)
22 - [Migrating from v2.1.x](#migrating-from-v21x)
23 - [Constraints](#constraints)
24 - [License](#license)
25
26<!-- END doctoc generated TOC please keep comment here to allow auto update -->
27
28# videojs-sprite-thumbnails
29
30Plugin to display thumbnails when hovering over the progress bar.
31
32## Compatibility
33
34This plugin version is compatible with Video.js v8.x. Compatible [releases](https://github.com/phloxic/videojs-sprite-thumbnails/releases) and [tags](https://github.com/phloxic/videojs-sprite-thumbnails/tags) are versioned v2.x.
35
36For Video.js v6.x, v7.x compatibility switch to the [vjs6-7-compat branch](https://github.com/phloxic/videojs-sprite-thumbnails/tree/vjs6-7-compat).
37
38## Features
39
40- works with single or multiple sprites containing thumbnails and individual thumbnail images
41- easy to [configure](#configuration)
42- 3 convenient ways to [retrieve image assets](#three-ways-to-configure-the-location-of-image-assets)
43- optional [function](#the-idxtag-function) to customize access to numbered lists of sequential image assets
44- uses [existing mouse time tooltip](#constraints)
45
46## Installation
47
48```sh
49npm install --save videojs-sprite-thumbnails
50```
51
52## Usage
53
54To include videojs-sprite-thumbnails on your website or web application, use any of the following methods.
55
56### `<script>` Tag
57
58This is the simplest case. Get the script in whatever way you prefer and include the plugin _after_ you include [video.js][videojs], so that the `videojs` global is available.
59
60```html
61<script src="/path/to/video.min.js"></script>
62<script src="/path/to/videojs-sprite-thumbnails.min.js"></script>
63<script>
64 const player = videojs('my-video');
65
66 // set up 160x90 thumbnails in single sprite.jpg, 1 per second
67 player.spriteThumbnails({
68 url: 'https://example.com/sprite.jpg',
69 width: 160,
70 height: 90,
71 columns: 10
72 });
73</script>
74```
75
76### Browserify/CommonJS
77
78When using with Browserify, install videojs-sprite-thumbnails via npm and `require` the plugin as you would any other module.
79
80```js
81const videojs = require('video.js');
82
83// The actual plugin function is exported by this module, but it is also
84// attached to the `Player.prototype`; so, there is no need to assign it
85// to a variable.
86require('videojs-sprite-thumbnails');
87
88const player = videojs('my-other-video');
89
90// More than 0 rows in combination with inserting {index) in the url
91// signals a sprite sequence.
92player.spriteThumbnails({
93 interval: 3,
94 url: 'https://example.com/sprite_sequence-{index}.jpg',
95 columns: 5,
96 rows: 5,
97 width: 120,
98 height: 90
99});
100```
101
102### RequireJS/AMD
103
104When using with RequireJS (or another AMD library), get the script in whatever way you prefer and `require` the plugin as you normally would:
105
106```js
107require(['video.js', 'videojs-sprite-thumbnails'], function(videojs) {
108 const player = videojs('my-video');
109
110 player.spriteThumbnails({
111 url: 'https://example.com/sprite.jpg',
112 width: 160,
113 height: 90,
114 columns: 10
115 });
116});
117```
118
119### CDN
120
121Select a 2.x version of videojs-sprite-thumbnails from the [CDN](https://unpkg.com/videojs-sprite-thumbnails@2/dist/).
122
123Or load the latest Video.js v8.x compatible release of the plugin via [script tag](#script-tag):
124
125```html
126<script src="https://unpkg.com/videojs-sprite-thumbnails@2.2.1/dist/videojs-sprite-thumbnails.min.js"></script>
127```
128
129### Configuration
130
131option | type | mandatory | default | description
132------ | ---- | --------- | ------- | -----------
133`url` | String | <a id="url" href="#url-as-is">single sprite</a> | `""` | Location of sprite image. Must be set by user.
134`urlArray` | Array | <a id="urlarray" href="urls-in-array">multiple images</a> | `[]` | Locations of images. Must be set by user.
135`url` | String | <a id="taggedurl" href="#url-by-string-expansion">multiple images</a> | `""` | Locations of multiple images via template expansion. Must be set by user.
136`width` | Integer | &#10004; | `0` | Width of a thumbnail in pixels.
137`height` | Integer | &#10004; | `0` | Height of a thumbnail in pixels.
138`columns` | Integer | <a id="columns" href="#migrating-from-v21x">&#10004;</a> | `0` | Number of thumbnail columns per image. Set both `columns` and `rows` to `1` for [individual thumbnails](#individual-thumbnails).
139`rows` | Integer | multiple images | `0` | Number of thumbnail rows per image. If set, the plugin will expect a sequence of images. The last image may have fewer rows.
140`interval` | Number | | `1` | Interval between thumbnails in seconds.
141`idxTag` | <a id="idxtag" href="#the-idxtag-function">Function</a> | | | Function determining how the `{index}` [template](#url-by-string-expansion) in the [`url`](#taggedurl) is expanded. Returns index as is by default.
142`responsive` | Integer | | `600` | Width of player in pixels below which thumbnails are responsive. Set to `0` to disable.
143`downlink` | Number | | `1.5` | Minimum of required [NetworkInformation downlink][downlink] where supported. Set to `0` to disable.
144
145#### Three ways to configure the location of image assets
146
1471. <a id="url-as-is" href="#url"><code>url</code></a> as String pointing to a single sprite image:
148```js
149{
150 url: 'https://example.com/single-sprite.jpg',
151 // [... more options]
152}
153```
154
1552. <a id="urls-in-array" href="#urlarray"><code>urlArray</code></a> containing multiple (sprite) images:
156```js
157{
158 urlArray: [
159 'https://example.com/first.jpg',
160 'https://example.com/second.jpg',
161 'https://example.com/third.jpg'
162 ],
163 rows: 7, // must be greater than 0
164 // [... more options]
165}
166```
1673. <a id="url-by-string-expansion" href="#taggedurl"><code>url</code></a> as String expanded by the `idxTag` [function](#the-idxtag-function):
168```js
169{
170 url: 'https://example.com/thumbs-{index}.jpg,
171 rows: 7, // must be greater than 0
172 idxTag(index) { // optional
173 return index; // this is the default
174 },
175 // [... more options]
176}
177```
178
179#### The idxTag function
180
181The function provided by this option can be used to generate various file naming schemes of sequential sprite images.
182
183Example for thumbnail images are numbered starting from 1, 4 digits long, and padded with leading zeroes:
184
185```js
186myplayer.spriteThumbnails({
187 // [ more options ... ]
188 url: 'https://example.com/{index}.jpg',
189 idxTag(index) {
190 return `000${index + 1}`.slice(-4);
191 },
192 colums: 5,
193 rows: 5
194});
195```
196
197Of course the same can be achieved by setting [`urlArray`](#urlarray) to the full list of images:
198
199```js
200myplayer.spriteThumbnails({
201 // [ more options ... ]
202 urlArray: [
203 'https://example.com/0001.jpg',
204 'https://example.com/0002.jpg',
205 'https://example.com/0003.jpg',
206 'https://example.com/0004.jpg'
207 ],
208 colums: 5,
209 rows: 5
210});
211```
212
213#### Individual thumbnails
214
215Set both `rows` and `columns` to `1`:
216
217```js
218myplayer.spriteThumbnails({
219 // [ other options ]
220 url: 'https://example.com/individual-thumb-{index}.avif',
221 columns: 1,
222 rows: 1
223});
224```
225
226### Initialization
227
228The plugin is initialized on plugin setup and every time the player [starts to load](https://docs.videojs.com/player#event:loadstart) a video. It monitors all video sources for an optional `spriteThumbnails` property. Any existing plugin configuration is updated by merging this `spriteThumbnails` object into the current configuration. Typical use cases are [playlists](#playlist-example).
229
230The image(s) are then loaded on demand, when the cursor hovers or moves over the progress bar.
231
232### Disabling and enabling the plugin
233
234The plugin can temporarily be disabled or enabled by toggling its boolean `ready` state:
235
236```js
237videojs.getPlayer('myplayer').spriteThumbnails().setState({ready: false});
238```
239
240Disable the plugin for a specific video about to be loaded:
241
242```js
243videojs.getPlayer('myplayer').src([{
244 type: 'video/mp4',
245 src: 'https://example.com/nothumbs.mp4',
246 // disable plugin with empty options object
247 spriteThumbnails: {}
248}]);
249```
250
251Note that the empty `spriteThumbnails: {}` configuration in this context internally uses `spriteThumbnails: {url: '', urlArray: []}` to preserve inheritance of all other options.
252
253### Playlist example
254
255```js
256const playlist = [
257 [{
258 type: 'video/webm',
259 src: 'https://example.com/video1.webm',
260
261 // only needed once, even if alternaive source is picked
262 spriteThumbnails: {
263 url: 'https://example.com/thumbnails1-{index}.jpg'
264 }
265 }, {
266 type: 'video/mp4',
267 src: 'https://example.com/video1.mp4'
268 }], [{
269 type: 'application/x-mpegurl',
270 src: 'https://example.com/video2.m3u8',
271 spriteThumbnails: {
272 url: 'https://example.com/thumbnails2-{index}.jpg'
273 }
274 }]
275];
276
277const player = videojs('myplayer', {
278 // player configuration
279 // [...]
280 // load first video in playlist
281 sources: playlist[0],
282
283 plugins: {
284 // default thumbnail settings for this player
285 spriteThumbnails: {
286 width: 160,
287 height: 90,
288 columns: 5,
289 rows: 5
290 }
291 }
292});
293
294// play 2nd video by clicking on button with id="secondvideo"
295videojs.on(videojs.dom.$('button#secondvideo'), 'click', () => {
296 player.src(playlist[1]);
297 player.play();
298});
299```
300
301### CSS state classes
302
303The plugin uses two CSS classes on the player element to signal the current state of plugin:
304
305class name | plugin state
306---------- | ------------
307`vjs-sprite-thumbnails` | plugin is/not loaded
308`vjs-thumbnails-ready` | plugin is/not ready to show thumbnails
309
310This allows for CSS directives which apply to player elements depending on plugin state:
311
312```css
313.video-js.vjs-thumbnails-ready .vjs-progress-holder {
314 background-color: green;
315}
316```
317
318### Debugging
319
320Each plugin instance has its own [log](https://docs.videojs.com/tutorial-plugins.html#logging) which can be used for targeted debugging. Its verbosity can be set by calling the player's [plugin name property](https://docs.videojs.com/tutorial-plugins.html#the-player-plugin-name-property):
321
322```js
323player.spriteThumbnails().log.level('debug');
324```
325
326The call can also be chained directly to the [manual plugin setup](https://docs.videojs.com/tutorial-plugins.html#setting-up-a-plugin):
327
328```js
329const player = videojs('example-player');
330player.spriteThumbnails({
331 url: 'https://example.com/thumbnails-{index}.jpg',
332 width: 240,
333 height: 100,
334 columns: 7,
335 rows: 6
336}).log.level('debug');
337```
338
339<h2 id="migrating-from-v21x">Migrating from v2.1.x</h2>
340
341Plugin version 2.2.0 introduced the *mandatory* option [`columns`](#columns). Thumbnail images are now [loaded on demand](https://github.com/phloxic/videojs-sprite-thumbnails/issues/56) which interferes less with video playback. Please apply the option to your existing setups.
342
343## Constraints
344
345- To display thumbnails the plugin expects the control bar in its [default tree structure](https://docs.videojs.com/tutorial-components.html#default-component-tree) to be present.
346- On some devices the [mouse time display](https://docs.videojs.com/mousetimedisplay) and its [time tooltip](https://docs.videojs.com/timetooltip) are disabled by default, and consequently thumbnails will not be shown.
347- Live streams are not supported.
348
349## License
350
351MIT. Copyright (c) Christian Ebert &lt;bcc@phloxic.productions&gt;
352
353
354[videojs]: http://videojs.com/
355[downlink]: https://developer.mozilla.org/docs/Web/API/NetworkInformation/downlink