import { _decorator, Component, Node, CameraComponent, view } from 'cc'; const { ccclass, property } = _decorator; @ccclass('CameraFitWidth') export class CameraFitWidth extends Component { private camera!:CameraComponent; private _defaultTanFov!: number; // LIFE-CYCLE CALLBACKS: onLoad () { this.camera = this.getComponent(CameraComponent); this._defaultTanFov = Math.tan(this.camera.fov / 180 * Math.PI); this.updateFov(); window.addEventListener('resize', this.updateFov); } updateFov = () => { let tan2 = view.getVisibleSize().height / view.getDesignResolutionSize().height * this._defaultTanFov; this.camera.fov = Math.atan(tan2) / Math.PI * 180; } onDestroy() { window.removeEventListener('resize', this.updateFov); } }