@@ -8,7 +8,6 @@ import { logger } from '@/Core/util/logger';
88import { isIOS } from '@/Core/initializeScript' ;
99import { WebGALPixiContainer } from '@/Core/controller/stage/pixi/WebGALPixiContainer' ;
1010import { WebGAL } from '@/Core/WebGAL' ;
11- import { SCREEN_CONSTANTS } from '@/Core/util/constants' ;
1211import { addSpineBgImpl , addSpineFigureImpl } from '@/Core/controller/stage/pixi/spine' ;
1312// import { figureCash } from '@/Core/gameScripts/vocal/conentsCash'; // 如果要使用 Live2D,取消这里的注释
1413// import { Live2DModel, SoundManager } from 'pixi-live2d-display-webgal'; // 如果要使用 Live2D,取消这里的注释
@@ -79,8 +78,8 @@ export default class PixiStage {
7978 public notUpdateBacklogEffects = false ;
8079 public readonly figureContainer : PIXI . Container ;
8180 public figureObjects : Array < IStageObject > = [ ] ;
82- public stageWidth = SCREEN_CONSTANTS . width ;
83- public stageHeight = SCREEN_CONSTANTS . height ;
81+ public stageWidth = WebGAL . canvasWidth ;
82+ public stageHeight = WebGAL . canvasHeight ;
8483 public assetLoader = new PIXI . Loader ( ) ;
8584 public readonly backgroundContainer : PIXI . Container ;
8685 public backgroundObjects : Array < IStageObject > = [ ] ;
@@ -133,6 +132,7 @@ export default class PixiStage {
133132 app . renderer . autoResize = true ;
134133 const appRoot = document . getElementById ( 'root' ) ;
135134 if ( appRoot ) {
135+ this . setRootSize ( document ) ;
136136 app . renderer . resize ( appRoot . clientWidth , appRoot . clientHeight ) ;
137137 }
138138 if ( isIOS ) {
@@ -167,6 +167,122 @@ export default class PixiStage {
167167 this . initialize ( ) . then ( ( ) => { } ) ;
168168 }
169169
170+ private setRootSize ( document : Document ) {
171+ /**
172+ * 在窗口大小改变时进行强制缩放
173+ */
174+ const ua = navigator . userAgent ;
175+ const isIOSDevice = ! ! ua . match ( / \( i [ ^ ; ] + ; ( U ; ) ? C P U .+ M a c O S X / ) ;
176+ function resize ( ) {
177+ const targetHeight = WebGAL . canvasHeight ;
178+ const targetWidth = WebGAL . canvasWidth ;
179+
180+ const h = window . innerHeight ; // 窗口高度
181+ const w = window . innerWidth ; // 窗口宽度
182+ const zoomH = h / targetHeight ; // 以窗口高度为基准的变换比
183+ const zoomW = w / targetWidth ; // 以窗口宽度为基准的变换比
184+ const zoomH2 = w / targetHeight ; // 竖屏时以窗口高度为基础的变换比
185+ const zoomW2 = h / targetWidth ; // 竖屏时以窗口宽度为基础的变换比
186+ let mh = ( targetHeight - h ) / 2 ; // y轴移动距离
187+ let mw = ( targetWidth - w ) / 2 ; // x轴移动距离
188+ let mh2os = targetWidth / 2 - w / 2 ; // 竖屏时 y轴移动距离
189+ let mw2os = targetHeight / 2 - h / 2 ; // 竖屏时 x轴移动距离
190+ let transform = '' ;
191+ let ebgTransform = '' ;
192+ const root = document . getElementById ( 'root' ) ; // 获取根元素
193+ const title = document . getElementById ( 'Title_enter_page' ) ;
194+ const ebg = document . getElementById ( 'ebg' ) ;
195+ const elements = [ root , title ] ;
196+ if ( root ) {
197+ root . style . width = `${ targetWidth } px` ;
198+ root . style . height = `${ targetHeight } px` ;
199+ }
200+ if ( title ) {
201+ title . style . width = `${ targetWidth } px` ;
202+ title . style . height = `${ targetHeight } px` ;
203+ }
204+ if ( w > h ) {
205+ const ebg = document . getElementById ( 'ebg' ) ;
206+ if ( ebg ) {
207+ ebg . style . height = `100vh` ;
208+ ebg . style . width = `100vw` ;
209+ ebgTransform = '' ;
210+ }
211+ mw = - mw ;
212+ mh = - mh ;
213+ if ( w * ( targetHeight / targetWidth ) >= h ) {
214+ transform = `translate(${ mw } px, ${ mh } px) scale(${ zoomH } ,${ zoomH } )` ;
215+ }
216+ if ( w * ( targetHeight / targetWidth ) < h ) {
217+ transform = `translate(${ mw } px, ${ mh } px) scale(${ zoomW } ,${ zoomW } )` ;
218+ }
219+ } else {
220+ /**
221+ * 旋转
222+ */
223+ if ( ebg ) {
224+ ebg . style . height = `${ targetHeight } px` ;
225+ ebg . style . width = `${ targetWidth } px` ;
226+ }
227+ mw2os = - mw2os ;
228+ if ( h * ( targetHeight / targetWidth ) >= w ) {
229+ ebgTransform = `rotate(90deg) translate(${ mw2os } px, ${ mh2os } px) scale(${ zoomH2 * 1.75 } ,${ zoomH2 * 1.75 } )` ;
230+ transform = `rotate(90deg) translate(${ mw2os } px, ${ mh2os } px) scale(${ zoomH2 } ,${ zoomH2 } )` ;
231+ }
232+ if ( h * ( targetHeight / targetWidth ) < w ) {
233+ ebgTransform = `rotate(90deg) translate(${ mw2os } px, ${ mh2os } px) scale(${ zoomW2 * 1.75 } ,${ zoomW2 * 1.75 } )` ;
234+ transform = `rotate(90deg) translate(${ mw2os } px, ${ mh2os } px) scale(${ zoomW2 } ,${ zoomW2 } )` ;
235+ }
236+ /**
237+ * iOS 不强制旋转
238+ */
239+ if ( isIOSDevice ) {
240+ const zoomWi = w / targetWidth ;
241+ transform = `translate(${ - mw } px, ${ - mh } px) scale(${ zoomWi } ,${ zoomWi } )`
242+ }
243+ }
244+ if ( ebg ) {
245+ ebg . style . transform = ebgTransform ;
246+ }
247+ for ( const element of elements ) {
248+ if ( element ) {
249+ element . style . transform = transform ;
250+ }
251+ }
252+ }
253+
254+ if ( ! isIOSDevice ) {
255+ // 创建一个新的 meta 标签
256+ const meta = document . createElement ( 'meta' ) ;
257+ meta . name = "viewport" ;
258+ meta . content = "width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" ;
259+ // 将该标签添加到 head 中
260+ document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( meta ) ;
261+ resize ( ) ;
262+ window . onload = resize ;
263+ window . onresize = resize ;
264+ // 监听键盘 F11 事件,全屏时触发页面调整
265+ document . onkeydown = function ( event ) {
266+ const e = event ;
267+ if ( e && e . key === 'F11' ) {
268+ setTimeout ( ( ) => {
269+ resize ( ) ;
270+ } , 100 ) ;
271+ }
272+ } ;
273+ } else {
274+ // ios
275+ const meta = document . createElement ( 'meta' ) ;
276+ meta . name = "viewport" ;
277+ meta . content = "width=device-width, initial-scale=0.22, minimum-scale=0.01, maximum-scale=1" ;
278+ document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( meta ) ;
279+ const style = document . createElement ( 'style' ) ;
280+ style . type = 'text/css' ;
281+ style . textContent = '* { font-synthesis: none !important; }' ;
282+ document . head . appendChild ( style ) ;
283+ }
284+ }
285+
170286 public getFigureObjects ( ) {
171287 return this . figureObjects ;
172288 }
0 commit comments