Moved functions that calculate video crop from Resizer.js to Scaler.js. Resizer.js is not fixed to account for this yet.

This commit is contained in:
Tamius Han 2018-05-06 21:32:18 +02:00
parent fa5e3909e6
commit 390348202e
4 changed files with 219 additions and 44 deletions

View File

@ -44,58 +44,58 @@ var _res_char = function(newAr, video, player){
_res_setVideoAr(new_ar, video, player);
}
// Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi".
// Približevanje opuščeno.
// handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho
var _res_legacyAr = function(action){
var vid = GlobalVars.video;
var ar;
// // Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi".
// // Približevanje opuščeno.
// // handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho
// var _res_legacyAr = function(action){
// var vid = GlobalVars.video;
// var ar;
if (!vid) {
if(Debug.debug){
console.log("[Resizer.js::_res_legacyAr] No video??",vid)
}
// if (!vid) {
// if(Debug.debug){
// console.log("[Resizer.js::_res_legacyAr] No video??",vid)
// }
return;
}
// return;
// }
if(! GlobalVars.playerDimensions ){
ar = screen.width / screen.height;
}
else{
ar = GlobalVars.playerDimensions.width / GlobalVars.playerDimensions.height;
}
// if(! GlobalVars.playerDimensions ){
// ar = screen.width / screen.height;
// }
// else{
// ar = GlobalVars.playerDimensions.width / GlobalVars.playerDimensions.height;
// }
// POMEMBNO: GlobalVars.lastAr je potrebno nastaviti šele po tem, ko kličemo _res_setAr(). _res_setAr() predvideva,
// da želimo nastaviti statično (type: 'static') razmerje stranic — tudi, če funkcijo kličemo tu oz. v ArDetect.
//
// IMPORTANT NOTE: GlobalVars.lastAr needs to be set after _res_setAr() is called, as _res_setAr() assumes we're
// setting a static aspect ratio (even if the function is called from here or ArDetect).
// // POMEMBNO: GlobalVars.lastAr je potrebno nastaviti šele po tem, ko kličemo _res_setAr(). _res_setAr() predvideva,
// // da želimo nastaviti statično (type: 'static') razmerje stranic — tudi, če funkcijo kličemo tu oz. v ArDetect.
// //
// // IMPORTANT NOTE: GlobalVars.lastAr needs to be set after _res_setAr() is called, as _res_setAr() assumes we're
// // setting a static aspect ratio (even if the function is called from here or ArDetect).
var fileAr = vid.videoWidth / vid.videoHeight;
// var fileAr = vid.videoWidth / vid.videoHeight;
if (action == "fitw"){
_res_setAr( ar > fileAr ? ar : fileAr);
}
else if(action == "fith"){
_res_setAr( ar < fileAr ? ar : fileAr);
}
else if(action == "reset"){
if(Debug.debug){
console.log("[Resizer.js::_res_legacyAr] Resetting aspect ratio to", fileAr)
}
// if (action == "fitw"){
// _res_setAr( ar > fileAr ? ar : fileAr);
// }
// else if(action == "fith"){
// _res_setAr( ar < fileAr ? ar : fileAr);
// }
// else if(action == "reset"){
// if(Debug.debug){
// console.log("[Resizer.js::_res_legacyAr] Resetting aspect ratio to", fileAr)
// }
_res_setAr(fileAr);
GlobalVars.lastAr = {type: "original"};
return;
}
else if(action == "autoar" || action == "auto"){
GlobalVars.lastAr = {type: "auto", ar: null};
ArDetect.init();
}
// _res_setAr(fileAr);
// GlobalVars.lastAr = {type: "original"};
// return;
// }
// else if(action == "autoar" || action == "auto"){
// GlobalVars.lastAr = {type: "auto", ar: null};
// ArDetect.init();
// }
GlobalVars.lastAr = {type: "legacy", action: action};
}
// GlobalVars.lastAr = {type: "legacy", action: action};
// }
var _res_setAr = function(ar){
if(Debug.debug)

148
js/modules/Scaler.js Normal file
View File

@ -0,0 +1,148 @@
// računa velikost videa za približevanje/oddaljevanje
// does video size calculations for zooming/cropping
class Scaler {
// internal variables
// functions
constructor() {
}
static modeToAr(mode, video, playerDimensions){
// Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi".
// Približevanje opuščeno.
// handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho
var ar;
if (!video) {
if(Debug.debug){
console.log("[Scaler.js::modeToAr] No video??",video)
}
return null;
}
if(! playerDimensions ){
ar = screen.width / screen.height;
}
else {
ar = playerDimensions.width / playerDimensions.height;
}
// POMEMBNO: GlobalVars.lastAr je potrebno nastaviti šele po tem, ko kličemo _res_setAr(). _res_setAr() predvideva,
// da želimo nastaviti statično (type: 'static') razmerje stranic — tudi, če funkcijo kličemo tu oz. v ArDetect.
//
// IMPORTANT NOTE: GlobalVars.lastAr needs to be set after _res_setAr() is called, as _res_setAr() assumes we're
// setting a static aspect ratio (even if the function is called from here or ArDetect).
var fileAr = video.videoWidth / video.videoHeight;
if (action == "fitw"){
return ar > fileAr ? ar : fileAr;
}
else if(action == "fith"){
return ar < fileAr ? ar : fileAr;
}
else if(action == "reset"){
if(Debug.debug){
console.log("[Resizer.js::_res_legacyAr] Resetting aspect ratio to", fileAr)
}
return fileAr;
}
return null;
}
static calculateCrop(mode, video, playerDimensions) {
// if 'ar' is string, we'll handle that in legacy wrapper
var ar = 0;
if(isNaN(mode)){
ar = this.modeToAr(ar);
} else {
ar = mode;
}
// handle fuckie-wuckies
if (! ar){
return null;
}
if(Debug.debug)
console.log("[Scaler::calculateCrop] trying to set ar. args are: ar->",ar,"; playerDimensions->",playerDimensions.width, "×", playerDimensions.height, "| obj:", GlobalVars.playerDimensions);
if(!video || video.videoWidth == 0 || video.videoHeight == 0){
if(Debug.debug)
console.log("[Scaler::calculateCrop] ERROR — no video detected.")
return {error: "no_video"};
}
if(! playerDimensions || playerDimensions.width === 0 || playerDimensions.height){
if(Debug.debug)
console.log("[Scaler::calculateCrop] ERROR — no (or invalid) playerDimensions:",playerDimensions);
return {error: "playerDimensions_error"};
}
// zdaj lahko končno začnemo računati novo velikost videa
// we can finally start computing required video dimensions now:
// Dejansko razmerje stranic datoteke/<video> značke
// Actual aspect ratio of the file/<video> tag
var fileAr = video.videoWidth / video.videoHeight;
var playerAr = playerDimensions.width / playerDimensions.height;
if(mode == "default" || !ar)
ar = fileAr;
if(Debug.debug)
console.log("[Scaler::calculateCrop] ar is " ,ar, ", file ar is", fileAr, ", playerDimensions are ", GlobalVars.playerDimensions.width, "×", GlobalVars.playerDimensions.height, "| obj:", GlobalVars.playerDimensions);
var videoDimensions = {
width: 0,
height: 0,
actualWidth: 0, // width of the video (excluding pillarbox) when <video> tag height is equal to width
actualHeight: 0, // height of the video (excluding letterbox) when <video> tag height is equal to height
}
if(Debug.debug){
console.log("[Scaler::calculateCrop] Player dimensions?", GlobalVars.playerDimensions.width, "×", GlobalVars.playerDimensions.height, "| obj:", GlobalVars.playerDimensions);
}
if( fileAr < ar ){
// imamo letterbox zgoraj in spodaj -> spremenimo velikost videa (a nikoli širše od ekrana)
// letterbox -> change video size (but never to wider than monitor width)
videoDimensions.width = Math.min(GlobalVars.playerDimensions.height * ar, GlobalVars.playerDimensions.width);
videoDimensions.height = videoDimensions.width * (1/fileAr);
}
else{
videoDimensions.height = Math.min(GlobalVars.playerDimensions.width / ar, GlobalVars.playerDimensions.height);
videoDimensions.width = videoDimensions.height * fileAr;
}
// izračunamo, kako visok/širok je video (brez črnih obrob). Če se željeno razmerje stranic
// ne ujema z razmerjem stranic predvajalnika, potem bomo še vedno videli črno obrobo bodisi
// zgoraj in spodaj, bodisi levo in desno. Zato v videoDimensions vključimo tudi dejansko
// velikost videa, da lahko Stretcher.js izračuna faktorje raztegovanja.
// Če je razmerje stranic predvajalnika širše kot želeno razmerje stranic, potem bosta `height`
// in `actualHeight` enaka, `actualWidth` pa bo izračunan na podlagi višine (in obratno).
if (ar > playerAr){
videoDimensions.actualHeight = videoDimensions.height;
videoDimensions.actualWidth = videoDimensions.height * ar;
} else {
videoDimensions.actualWidth = videoDimensions.width;
videoDimensions.actualHeight = videoDimensions.width / ar;
}
if(Debug.debug){
console.log("[Scaler::calculateCrop] Video dimensions: ", videoDimensions.width, "×", videoDimensions.height, "(obj:", videoDimensions, "); playerDimensions:",GlobalVars.playerDimensions.width, "×", GlobalVars.playerDimensions.height, "(obj:", GlobalVars.playerDimensions, ")");
}
return videoDimensions;
}
}

15
js/modules/Stretcher.js Normal file
View File

@ -0,0 +1,15 @@
// računa vrednosti za transform-scale (x, y)
// transform: scale(x,y) se uporablja za raztegovanje videa, ne pa za približevanje
// calculates values for transform scale(x, y)
// transform: scale(x,y) is used for stretching, not zooming.
class Stretcher {
// internal variables
// functions
constructor() {
}
}

12
js/modules/Zoom.js Normal file
View File

@ -0,0 +1,12 @@
// računa približevanje ter računa/popravlja odmike videa
class Zoom {
// internal variables
// functions
constructor() {
}
}