video-layout
v1.1.0
Published
Automatic layout of video elements (publisher and subscriber) minimising white-space.
Downloads
2
Readme
video-layout
Automatic layout of video elements (publisher and subscriber) minimising white-space.
It automatically detects when an element is added or when the container is resized and it adjusts the layout of its' children accordingly.
This library is a genericization of @aullman's excellent opentok-layout-js. It can work with any video library, or any layout that has containers inside of a larger fixed container. It has all OpenTok and jQuery dependencies removed.
Demo
Demo of layout container in action (This is @aullman's demo.)
Dependencies
None
Usage
Call initLayoutContainer
and pass it the element you want it to layout. It works best if you set the position of the element to be absolute or relative. You will then be returned an object with a layout method that you can call to layout the page.
var layout = initLayoutContainer(document.getElementById("layout"), {
maxRatio: 3/2, // The narrowest ratio that will be used (default 2x3)
minRatio: 9/16, // The widest ratio that will be used (default 16x9)
fixedRatio: false, // If this is true then the aspect ratio of the video is maintained and minRatio and maxRatio are ignored (default false),
idPrefix: ".OT_", // Prefix used if main container doesn't have an id
rootClass: ".OT_root", // Class for video container elements, change for other providers
bigClass: "OT_big", // The class to add to elements that should be sized bigger
bigPercentage: 0.8 // The maximum percentage of space the big ones should take up
bigFixedRatio: false, // fixedRatio for the big ones
bigMaxRatio: 3/2, // The narrowest ratio to use for the big elements (default 2x3)
bigMinRatio: 9/16, // The widest ratio to use for the big elements (default 16x9)
bigFirst: true // Whether to place the big one in the top left (true) or bottom right
});
layout.layout()
In an OpenTok application you would do something like:
<html>
<head>
<title>Layout Container Example</title>
<script src="http://static.opentok.com/v2/js/opentok.min.js"></script>
<script src="js/video-layout.min.js"></script>
<style type="text/css" media="screen">
#layoutContainer {
width: 320px;
height: 240px;
background-color: #DDD;
position:relative;
}
</style>
</head>
<body>
<div id="layoutContainer">
<div id="publisherContainer"></div>
</div>
</body>
<script type="text/javascript" charset="utf-8">
var layoutContainer = document.getElementById("layoutContainer");
// Initialize the layout container and get a reference to the layout method
var layout = initLayoutContainer(layoutContainer).layout;
// Below is a normal hello world OpenTok application for v2 of the API
// The layout container will redraw when the layout mtehod is called and
// adjust the layout accordingly
var sessionId = "mySessionId";
var token = "myToken";
var apiKey = "myAPIKey";
var session = OT.initSession(sessionId);
session.on("streamCreated", function(event){
session.subscribe(event.stream, "layoutContainer", {
insertMode: "append"
});
layout();
}).connect(apiKey, token, function (err) {
if (!err) {
session.publish("publisherContainer");
layout();
}
});
</script>
</html>
Now any time you call the layout
method the layout container will automatically be positioned and sized so that it minimises the white-space within the box given.
Other video providers must wrap their <video>
elements in an outer container (usually a <div>
), classed with whatever you specify as rootClass
in initLayoutContainer
. These video containers are all children of the main container element passed in as the first argument to initLayoutContainer
.
Resizing the Window
You want to call the layout method any time the size of the layout container changes. If the size of the container varies depending on the size of the window you may need to do:
var resizeTimeout;
window.onresize = function() {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(function () {
layout();
}, 20);
};
This throttles calling layout so that it's not called over an over again when you're resizing the window.
Animations
If you want to animate things you can use CSS3 transitions on the children. eg.
.container > * {
transition-property: all;
transition-duration: 0.5s;
}
Big Elements
If you add the bigClass
to elements in the layout container they will be treated differently. They are essentially in a layout container of their own which takes up bigPercentage
of the space and has it's own settings for ratios (bigMaxRatio
, bigMinRatio
, bigFixedRatio
).
You can have multiple elements which are treated as big elements which allow you to have all kinds of different layouts.
To see how this works try the demo and double click on elements.
Padding, Margins and Borders
The Layout Container will take into account the padding, margins and borders on it's children. If you want spacing between elements simply give them a margin or padding and they will be spaced accordingly.