Skip to content

Instantly share code, notes, and snippets.

@rayfranco
Created June 3, 2014 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayfranco/0910241a88f24f5fb903 to your computer and use it in GitHub Desktop.
Save rayfranco/0910241a88f24f5fb903 to your computer and use it in GitHub Desktop.
this.el = document.getElementById('video');
var minW = 1024;
var minH = 661;
var onResize = function() {
var ch, cr, cw, ph, pr, pw, css;
if (!this.el) {
return;
}
pw = this.el.videoWidth;
ph = this.el.videoHeight;
cw = window.innerWidth;
ch = window.innerHeight;
pr = pw / ph;
cr = cw / ch;
cw = cw > minW ? cw : minW;
ch = ch > minH ? ch : minH;
if (cr < pr) {
css = {
width: ch * pr,
height: ch,
top: 0,
left: -((ch * pr) - cw) * 0.5
};
} else {
css = {
width: cw,
height: cw / pr,
top: -((cw / pr) - ch) * 0.5,
left: 0
};
}
this.el.style.width = css.width+'px';
this.el.style.height = css.height+'px';
this.el.style.top = css.top+'px';
this.el.style.left = css.left+'px';
}.bind(this);
this.el.style.position = 'absolute';
// Listen to window resize event
window.addEventListener('resize',onResize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment