oxjs/source/Ox.UI/js/Video/Ox.VideoPreview.js

149 lines
4.8 KiB
JavaScript
Raw Normal View History

2011-08-08 13:58:19 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
Ox.VideoPreview = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
duration: 0,
getFrame: null,
fps: 25,
2011-09-18 06:50:54 +00:00
frameRatio: 16/9,
height: 256,
2011-09-29 17:25:50 +00:00
position: void 0,
scaleToFill: false,
2011-08-08 13:58:19 +00:00
timeline: '',
2011-09-18 06:50:54 +00:00
width: 256
2011-08-08 13:58:19 +00:00
})
.options(options || {})
2011-09-19 12:29:06 +00:00
.addClass('OxVideoPreview')
.css({
width: self.options.width + 'px',
height: self.options.height + 'px'
});
2011-08-08 13:58:19 +00:00
self.loaded = [];
self.queue = [];
self.$frameElement = $('<div>')
2011-08-08 13:58:19 +00:00
.addClass('OxFrame')
.appendTo(that.$element)
self.$frame = $('<img>')
2011-09-29 17:25:50 +00:00
.attr({src: self.options.getFrame(self.options.position)})
2011-09-18 06:50:54 +00:00
.css(getFrameCSS())
.appendTo(self.$frameElement);
2011-08-08 13:58:19 +00:00
self.$timeline = $('<img>')
.addClass('OxTimeline')
.attr({src: self.options.timeline})
2011-09-18 06:50:54 +00:00
.css({width: self.options.width + 'px'})
2011-08-08 13:58:19 +00:00
.appendTo(that.$element);
self.$interface = Ox.Element({
tooltip: function(event) {
2011-08-17 18:57:58 +00:00
//event.offsetX does not work in Firefox
2011-08-21 08:18:38 +00:00
// fixme: use layerX then
2011-08-17 18:57:58 +00:00
var position = getPosition(event.clientX - that.offset().left);
2011-08-08 13:58:19 +00:00
self.$frame.attr({src: self.options.getFrame(position)});
return Ox.formatDuration(position, 2);
}
})
.addClass('OxInterface')
.bind({
click: click,
mouseenter: startLoading,
mouseleave: function() {
2011-09-29 17:25:50 +00:00
self.$frame.attr({src: self.options.getFrame(self.options.position)});
2011-08-08 13:58:19 +00:00
stopLoading();
}
})
.appendTo(that.$element);
function click(e) {
that.triggerEvent('click', {
2011-09-18 06:50:54 +00:00
// e.offsetX does not work in Firefox
2011-08-17 18:57:58 +00:00
position: getPosition(e.clientX - that.offset().left)
2011-08-08 13:58:19 +00:00
});
}
2011-09-18 06:50:54 +00:00
function getFrameCSS() {
var css = {},
elementWidth = self.options.width,
elementHeight = self.options.height - 16,
elementRatio = elementWidth / elementHeight,
frameRatio = self.options.frameRatio,
frameIsWider = frameRatio > elementRatio;
if (self.options.scaleToFill) {
css.width = frameIsWider ? elementHeight * frameRatio : elementWidth;
css.height = frameIsWider ? elementHeight : elementWidth / frameRatio;
css.marginLeft = frameIsWider ? (elementWidth - css.width) / 2 : 0;
css.marginTop = frameIsWider ? 0 : (elementHeight - css.height) / 2;
} else {
css.width = frameIsWider ? elementWidth : elementHeight * frameRatio;
css.height = frameIsWider ? elementWidth / frameRatio : elementHeight;
css.marginLeft = frameIsWider ? 0 : (css.width - elementWidth) / 2;
css.marginTop = frameIsWider ? (css.height - elementHeight) / 2 : 0;
2011-09-18 06:50:54 +00:00
}
return Ox.map(css, function(value) {
return Math.round(value) + 'px';
});
2011-09-18 06:50:54 +00:00
}
2011-08-08 13:58:19 +00:00
function getPosition(x) {
return Math.round(
2011-09-18 06:50:54 +00:00
self.options.duration * x / self.options.width * self.options.fps
2011-08-08 13:58:19 +00:00
) / self.options.fps;
}
function startLoading() {
var last,
2011-09-18 06:50:54 +00:00
steps = [Math.round(self.options.width / 2)];
2011-08-08 13:58:19 +00:00
while ((last = steps[steps.length - 1]) > 1) {
steps.push(Math.round(last / 2));
}
steps.forEach(function(step) {
2011-09-18 06:50:54 +00:00
Ox.loop(0, self.options.width, step, function(x) {
2011-08-08 13:58:19 +00:00
var frame = self.options.getFrame(getPosition(x));
if (
self.loaded.indexOf(frame) == -1
&& self.queue.indexOf(frame) == -1
) {
self.queue.push(frame);
}
});
});
loadFrame();
function loadFrame() {
$('<img>')
.load(function() {
self.loaded.push(self.queue.shift());
self.queue.length && loadFrame();
})
.attr({src: self.queue[0]});
}
}
function stopLoading() {
self.queue = [];
}
self.setOption = function(key, value) {
2011-09-18 06:50:54 +00:00
if (key == 'height') {
2011-09-23 10:43:57 +00:00
that.css({height: value + 'px'});
2011-09-29 17:25:50 +00:00
self.$frame.css(getFrameCSS());
} else if (key == 'position') {
self.$frame.attr({src: self.options.getFrame(value)});
2011-09-18 06:50:54 +00:00
} else if (key == 'width') {
2011-09-23 10:43:57 +00:00
that.css({width: value + 'px'});
2011-08-08 13:58:19 +00:00
self.$frame.attr({src: self.options.getFrame()})
2011-09-18 06:50:54 +00:00
.css(getFrameCSS());
2011-08-08 13:58:19 +00:00
self.$timeline.css({width: value + 'px'});
stopLoading();
}
}
return that;
2011-08-17 18:57:58 +00:00
};