ultrawidify/src/ext/lib/video-transform/Zoom.ts

107 lines
2.7 KiB
TypeScript
Raw Normal View History

import Debug from '../../conf/Debug';
2021-02-08 22:45:51 +01:00
import Logger from '../Logger';
import VideoData from '../video-data/VideoData';
// računa približevanje ter računa/popravlja odmike videa
// calculates zooming and video offsets/panning
class Zoom {
2021-02-08 22:45:51 +01:00
//#region flags
//#endregion
//#region helper objects
conf: VideoData;
logger: Logger;
//#endregion
//#region misc data
scale: number = 1;
2021-10-31 23:19:32 +01:00
scaleY: number = 1;
2021-02-08 22:45:51 +01:00
logScale: number = 0;
2021-10-31 23:19:32 +01:00
logScaleY: number = 0;
scaleStep: number = 0.1;
2021-02-08 22:45:51 +01:00
minScale: number = -1; // 50% (log2(0.5) = -1)
maxScale: number = 3; // 800% (log2(8) = 3)
//#endregion
2019-09-03 22:55:10 +02:00
constructor(videoData) {
2019-09-03 23:01:23 +02:00
this.conf = videoData;
this.logger = videoData.logger;
2018-05-12 02:51:58 +02:00
}
2018-05-12 02:51:58 +02:00
reset(){
this.scale = 1;
2019-11-02 02:45:24 +01:00
this.logScale = 0;
2018-05-12 02:51:58 +02:00
}
2021-10-31 23:19:32 +01:00
/**
* Increases zoom by a given amount. Does not allow per-axis zoom.
* Will set zoom level to x axis (+ given amount) if x and y zooms differ.
* @param amount
2025-04-20 16:11:17 +02:00
* @param axis leave undefined to apply zoom to both axes
2021-10-31 23:19:32 +01:00
*/
2025-04-20 16:11:17 +02:00
zoomStep(amount: number, axis?: 'x' | 'y') {
let newLog = axis === 'y' ? this.logScaleY : this.logScale;
newLog += amount;
newLog = Math.min(Math.max(newLog, this.minScale), this.maxScale);
// if axis is undefined, both of this statements should trigger)
if (axis !== 'y') {
this.logScale = newLog;
}
2025-04-20 16:11:17 +02:00
if (axis !== 'x') {
this.logScaleY = newLog;
}
2021-10-31 23:19:32 +01:00
this.scale = Math.pow(2, this.logScale);
2025-04-20 16:11:17 +02:00
this.scaleY = Math.pow(2, this.logScaleY);
2019-11-02 02:45:24 +01:00
2021-10-31 23:19:32 +01:00
this.logger.log('info', 'debug', "[Zoom::zoomStep] changing zoom by", amount, ". New zoom level:", this.scale);
this.processZoom();
2018-05-12 02:51:58 +02:00
}
setZoom(scale: number, axis?: 'x' |'y', noAnnounce?){
2018-09-19 22:52:53 +02:00
// NOTE: SCALE IS NOT LOGARITHMIC
2019-01-03 02:07:16 +01:00
if(scale < Math.pow(2, this.minScale)) {
scale = this.minScale;
2019-01-03 02:07:16 +01:00
} else if (scale > Math.pow(2, this.maxScale)) {
scale = this.maxScale;
}
2021-10-31 23:19:32 +01:00
switch (axis) {
case 'x':
this.scale = scale;
break;
case 'y':
this.scaleY = scale;
break;
default:
this.scale = scale;
this.scaleY = scale;
}
this.processZoom();
}
processZoom() {
2025-04-20 16:11:17 +02:00
this.conf.resizer.toFixedAr();
this.conf.resizer.applyScaling({xFactor: this.scale, yFactor: this.scaleY}, {noAnnounce: true});
2018-05-12 02:51:58 +02:00
}
applyZoom(stretchFactors){
2019-02-15 00:26:54 +01:00
if (!stretchFactors) {
return;
}
this.logger.log('info', 'debug', "[Zoom::setZoom] Applying zoom. Stretch factors pre:", stretchFactors, " —> scale:", this.scale);
2019-01-03 02:07:16 +01:00
stretchFactors.xFactor *= this.scale;
stretchFactors.yFactor *= this.scale;
2019-01-03 02:07:16 +01:00
this.logger.log('info', 'debug', "[Zoom::setZoom] Applying zoom. Stretch factors post:", stretchFactors);
2018-05-12 02:51:58 +02:00
}
}
export default Zoom;