refactor: 单独封装出一个 adjustCover 方法

This commit is contained in:
unanmed 2025-07-23 18:25:05 +08:00
parent 1a8b1f9a0e
commit f36b8b9145
2 changed files with 34 additions and 13 deletions

View File

@ -26,6 +26,7 @@ import { ExitFullscreen, Fullscreen, SoundVolume } from '../components';
import { mainSetting, triggerFullscreen } from '@motajs/legacy-ui';
import { saveLoad } from './save';
import { MainSceneUI } from './main';
import { adjustCover } from '../utils';
const enum TitleButton {
StartGame,
@ -59,19 +60,12 @@ export const GameTitle = defineComponent<GameTitleProps>(props => {
const bg = core.material.images.images['bg.webp'];
//#region 计算背景图
const aspect = bg.width / bg.height;
const canvasAspect = MAIN_WIDTH / MAIN_HEIGHT;
const [width, height] = (() => {
if (canvasAspect > aspect) {
const width = MAIN_WIDTH;
const height = width / aspect;
return [width, height];
} else {
const height = MAIN_HEIGHT;
const width = height * aspect;
return [width, height];
}
})();
const [width, height] = adjustCover(
bg.width,
bg.height,
MAIN_WIDTH,
MAIN_HEIGHT
);
//#region 标题设置

View File

@ -57,3 +57,30 @@ export function adjustGrid(
locs
};
}
/**
* `object-fit: cover`
* @param itemWidth
* @param itemHeight
* @param targetWidth
* @param targetHeight
* @returns
*/
export function adjustCover(
itemWidth: number,
itemHeight: number,
targetWidth: number,
targetHeight: number
): Readonly<LocArr> {
const aspect = itemWidth / itemHeight;
const canvasAspect = targetWidth / targetHeight;
if (canvasAspect > aspect) {
const width = targetWidth;
const height = width / aspect;
return [width, height];
} else {
const height = targetHeight;
const width = height * aspect;
return [width, height];
}
}