scale-and-responsive
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseScale and Responsive Design
缩放与响应式设计
How to use the ScaleManager for scaling, centering, fullscreen, orientation handling, and responsive resize in Phaser 4.
Key source paths: , , ,
Related skills: ../game-setup-and-config/SKILL.md
src/scale/ScaleManager.jssrc/scale/const/src/scale/events/src/core/typedefs/ScaleConfig.js如何在Phaser 4中使用ScaleManager实现缩放、居中、全屏、方向处理和响应式窗口调整。
核心源码路径: 、、、
相关技能: ../game-setup-and-config/SKILL.md
src/scale/ScaleManager.jssrc/scale/const/src/scale/events/src/core/typedefs/ScaleConfig.jsQuick Start
快速入门
Scale-to-fit with centering -- the most common setup for responsive games:
js
const config = {
type: Phaser.AUTO,
scale: {
parent: 'game-container',
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 800,
height: 600
},
scene: MyScene
};
const game = new Phaser.Game(config);The config object is parsed into a instance which the reads during boot. The ScaleManager sets the canvas element size and applies CSS scaling to fit it within its parent.
scalePhaser.Core.ConfigScaleManager带居中的适配缩放——响应式游戏最常用的设置:
js
const config = {
type: Phaser.AUTO,
scale: {
parent: 'game-container',
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 800,
height: 600
},
scene: MyScene
};
const game = new Phaser.Game(config);scalePhaser.Core.ConfigCore Concepts
核心概念
ScaleManager (src/scale/ScaleManager.js)
ScaleManager(src/scale/ScaleManager.js)
The ScaleManager is created during the Game boot sequence and is accessible at or from within a Scene. It extends .
game.scalethis.scaleEventEmitterThree internal Size components drive all calculations:
| Component | Property | Purpose |
|---|---|---|
| | The unmodified game dimensions from config. Used for world bounds, cameras. Read via |
| | The auto-rounded gameSize. Sets the actual |
| | The CSS-scaled canvas size after applying scale mode, parent bounds, and zoom. Sets |
Scaling works by keeping the canvas element dimensions fixed (baseSize) and stretching it via CSS properties (displaySize). This is equivalent to CSS but without browser prefix issues.
transform-scaleThe property () holds the ratio and is used internally for input coordinate transformation.
displayScalePhaser.Math.Vector2baseSize / canvasBoundsScaleManager在游戏启动过程中创建,可通过或场景内的访问。它继承自。
game.scalethis.scaleEventEmitter所有计算由三个内部Size组件驱动:
| 组件 | 属性 | 用途 |
|---|---|---|
| | 配置中未修改的游戏尺寸,用于世界边界、相机设置。可通过 |
| | 自动取整后的gameSize,设置实际的 |
| | 应用缩放模式、父容器边界和缩放后的CSS缩放canvas尺寸,设置 |
缩放的原理是保持canvas元素尺寸固定(baseSize),通过CSS属性(displaySize)拉伸它。这等效于CSS的,但不会有浏览器前缀问题。
transform-scaledisplayScalePhaser.Math.Vector2baseSize / canvasBoundsScale Modes (src/scale/const/SCALE_MODE_CONST.js)
缩放模式(src/scale/const/SCALE_MODE_CONST.js)
All modes are on and are set via in config:
Phaser.Scale.ScaleModesscale.mode| Constant | Value | Behavior |
|---|---|---|
| 0 | No automatic scaling. Canvas uses config width/height. You manage sizing yourself. If you resize the canvas externally, call |
| 1 | Height adjusts automatically to maintain aspect ratio based on width. |
| 2 | Width adjusts automatically to maintain aspect ratio based on height. |
| 3 | Scales to fit inside parent while preserving aspect ratio. May leave empty space (letterbox/pillarbox). Most commonly used mode. |
| 4 | Scales to cover the entire parent while preserving aspect ratio. May extend beyond parent bounds (content gets cropped). |
| 5 | Canvas element itself is resized to fill parent. No CSS scaling -- 1:1 pixel mapping. The |
| 6 | Hybrid of RESIZE and FIT. The visible area resizes to fill the parent, and the canvas scales to fit inside that area. Added in v3.80. |
Shorthand constants are also available directly on : , , etc.
Phaser.ScalePhaser.Scale.FITPhaser.Scale.RESIZE所有模式都在上,可通过配置中的设置:
Phaser.Scale.ScaleModesscale.mode| 常量 | 值 | 行为 |
|---|---|---|
| 0 | 无自动缩放,canvas使用配置的宽高。需自行管理尺寸。如果外部调整canvas大小,需调用 |
| 1 | 根据宽度自动调整高度,保持宽高比。 |
| 2 | 根据高度自动调整宽度,保持宽高比。 |
| 3 | 在保持宽高比的同时缩放以适配父容器,可能会留下空白区域(黑边),是最常用的模式。 |
| 4 | 在保持宽高比的同时缩放以覆盖整个父容器,可能超出父容器边界(内容会被裁剪)。 |
| 5 | canvas元素本身调整大小以填充父容器,无CSS缩放——像素1:1映射。 |
| 6 | RESIZE和FIT的混合模式,可视区域调整大小以填充父容器,canvas缩放以适配该区域,在v3.80版本新增。 |
简写常量也可直接在上使用:、等。
Phaser.ScalePhaser.Scale.FITPhaser.Scale.RESIZECenter Modes (src/scale/const/CENTER_CONST.js)
居中模式(src/scale/const/CENTER_CONST.js)
Set via in config. Centering is achieved by setting CSS and on the canvas:
scale.autoCentermarginLeftmarginTop| Constant | Value | Behavior |
|---|---|---|
| 0 | No auto-centering (default). |
| 1 | Center horizontally and vertically within parent. |
| 2 | Center horizontally only. |
| 3 | Center vertically only. |
The parent element must have calculable bounds. If the parent has no defined width/height, centering will not work correctly.
通过配置中的设置,居中是通过设置canvas的CSS和实现的:
scale.autoCentermarginLeftmarginTop| 常量 | 值 | 行为 |
|---|---|---|
| 0 | 无自动居中(默认)。 |
| 1 | 在父容器内水平和垂直居中。 |
| 2 | 仅水平居中。 |
| 3 | 仅垂直居中。 |
父元素必须有可计算的边界,如果父元素没有定义宽高,居中将无法正常工作。
Zoom (src/scale/const/ZOOM_CONST.js)
缩放(src/scale/const/ZOOM_CONST.js)
Set via in config. Multiplies the display size:
scale.zoom| Constant | Value | Behavior |
|---|---|---|
| 1 | No zoom (default). |
| 2 | 2x zoom -- good for pixel art at low base resolution. |
| 4 | 4x zoom. |
| -1 | Automatically calculates the largest integer zoom that fits in the parent. |
You can also pass any numeric value. The zoom affects CSS display size but not the canvas resolution.
通过配置中的设置,会乘以显示尺寸:
scale.zoom| 常量 | 值 | 行为 |
|---|---|---|
| 1 | 无缩放(默认)。 |
| 2 | 2倍缩放——适合低基础分辨率的像素画。 |
| 4 | 4倍缩放。 |
| -1 | 自动计算适配父容器的最大整数缩放比例。 |
也可以传入任何数值,缩放会影响CSS显示尺寸,但不会改变canvas分辨率。
Orientation (src/scale/const/ORIENTATION_CONST.js)
方向(src/scale/const/ORIENTATION_CONST.js)
Read via . Values are strings:
game.scale.orientation- =
Phaser.Scale.Orientation.LANDSCAPE'landscape-primary' - =
Phaser.Scale.Orientation.LANDSCAPE_SECONDARY'landscape-secondary' - =
Phaser.Scale.Orientation.PORTRAIT'portrait-primary' - =
Phaser.Scale.Orientation.PORTRAIT_SECONDARY'portrait-secondary'
Convenience booleans: , (device orientation), , (game dimensions).
game.scale.isPortraitgame.scale.isLandscapegame.scale.isGamePortraitgame.scale.isGameLandscape可通过读取,值为字符串:
game.scale.orientation- =
Phaser.Scale.Orientation.LANDSCAPE'landscape-primary' - =
Phaser.Scale.Orientation.LANDSCAPE_SECONDARY'landscape-secondary' - =
Phaser.Scale.Orientation.PORTRAIT'portrait-primary' - =
Phaser.Scale.Orientation.PORTRAIT_SECONDARY'portrait-secondary'
便捷布尔值:、(设备方向)、、(游戏尺寸方向)。
game.scale.isPortraitgame.scale.isLandscapegame.scale.isGamePortraitgame.scale.isGameLandscapeCommon Patterns
常见模式
FIT Mode with Centering (Most Common)
带居中的FIT模式(最常用)
js
scale: {
parent: 'game-container',
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 1280,
height: 720
}The game maintains its 16:9 aspect ratio and centers within the parent. Empty space appears as letterbox/pillarbox bars. Style the parent's background color to control bar appearance.
js
scale: {
parent: 'game-container',
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 1280,
height: 720
}游戏保持16:9的宽高比并在父容器内居中,空白区域会显示为黑边,可通过设置父容器的背景色控制黑边外观。
Responsive Resize (Dynamic Canvas Size)
响应式窗口调整(动态Canvas尺寸)
js
scale: {
parent: 'game-container',
mode: Phaser.Scale.RESIZE,
width: '100%',
height: '100%'
}js
// In your scene -- respond to size changes:
create() {
this.scale.on('resize', this.handleResize, this);
}
handleResize(gameSize, baseSize, displaySize) {
this.cameras.resize(gameSize.width, gameSize.height);
// Reposition UI elements based on new dimensions
}Width/height accept percentage strings (e.g., ) which resolve against the parent size. If the parent has no size, they fall back to / .
'100%'window.innerWidthwindow.innerHeightjs
scale: {
parent: 'game-container',
mode: Phaser.Scale.RESIZE,
width: '100%',
height: '100%'
}js
// 在场景中——响应尺寸变化:
create() {
this.scale.on('resize', this.handleResize, this);
}
handleResize(gameSize, baseSize, displaySize) {
this.cameras.resize(gameSize.width, gameSize.height);
// 根据新尺寸重新定位UI元素
}宽/高支持百分比字符串(如),会根据父容器尺寸解析。如果父容器没有尺寸,会回退到 / 。
'100%'window.innerWidthwindow.innerHeightFullscreen Toggle
全屏切换
js
// Must be called from a pointerup gesture (not pointerdown)
this.input.on('pointerup', () => {
if (this.scale.isFullscreen) {
this.scale.stopFullscreen();
} else {
this.scale.startFullscreen();
}
});
// Or use the convenience method:
this.input.on('pointerup', () => {
this.scale.toggleFullscreen();
});- -- requests browser fullscreen. Default options:
startFullscreen(fullscreenOptions).{ navigationUI: 'hide' } - -- exits fullscreen mode.
stopFullscreen() - -- toggles between the two.
toggleFullscreen(fullscreenOptions) - -- read-only boolean for current state.
isFullscreen
If no is configured, Phaser creates a temporary , moves the canvas into it, and sends that div fullscreen. The div is removed when leaving fullscreen.
fullscreenTarget<div>For iframes, the attribute is required.
allowfullscreenjs
// 必须从pointerup手势调用(不能是pointerdown)
this.input.on('pointerup', () => {
if (this.scale.isFullscreen) {
this.scale.stopFullscreen();
} else {
this.scale.startFullscreen();
}
});
// 或者使用便捷方法:
this.input.on('pointerup', () => {
this.scale.toggleFullscreen();
});- ——请求浏览器全屏,默认选项:
startFullscreen(fullscreenOptions)。{ navigationUI: 'hide' } - ——退出全屏模式。
stopFullscreen() - ——在两种状态间切换。
toggleFullscreen(fullscreenOptions) - ——只读布尔值,表示当前状态。
isFullscreen
如果未配置,Phaser会创建一个临时,将canvas移入其中并将该div设为全屏,退出全屏时会移除该div。
fullscreenTarget<div>对于iframe,需要属性。
allowfullscreenMobile Orientation Handling
移动端方向处理
js
create() {
this.scale.on('orientationchange', (orientation) => {
if (orientation === Phaser.Scale.LANDSCAPE) {
// Show game UI
} else {
// Show "rotate device" message
}
});
// Lock orientation (mobile browsers only, limited support):
this.scale.lockOrientation('landscape');
}js
create() {
this.scale.on('orientationchange', (orientation) => {
if (orientation === Phaser.Scale.LANDSCAPE) {
// 显示游戏UI
} else {
// 显示"旋转设备"提示
}
});
// 锁定方向(仅移动端浏览器,支持有限):
this.scale.lockOrientation('landscape');
}Fixed Size (No Scaling)
固定尺寸(无缩放)
js
scale: {
mode: Phaser.Scale.NONE,
width: 800,
height: 600
}In NONE mode, if you change the canvas size externally you must call to update all internal components including input coordinates.
game.scale.resize(newWidth, newHeight)js
scale: {
mode: Phaser.Scale.NONE,
width: 800,
height: 600
}在NONE模式下,如果外部修改canvas尺寸,必须调用更新所有内部组件,包括输入坐标。
game.scale.resize(newWidth, newHeight)Pixel Art with Max Zoom
带最大缩放的像素画
js
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 320,
height: 240,
zoom: Phaser.Scale.MAX_ZOOM
},
pixelArt: trueMAX_ZOOMpixelArt: truejs
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: 320,
height: 240,
zoom: Phaser.Scale.MAX_ZOOM
},
pixelArt: trueMAX_ZOOMpixelArt: trueSnap Values for Grid Alignment
网格对齐的吸附值
js
scale: {
mode: Phaser.Scale.FIT,
width: 800,
height: 600,
snap: { width: 16, height: 16 }
}When the browser resizes, dimensions snap down to the nearest grid multiple (using floor). Best used with FIT mode. Can also be set at runtime: or reset with .
game.scale.setSnap(16, 16)game.scale.setSnap()js
scale: {
mode: Phaser.Scale.FIT,
width: 800,
height: 600,
snap: { width: 16, height: 16 }
}浏览器调整大小时,尺寸会向下吸附到最近的网格倍数(使用floor计算),最适合FIT模式。也可在运行时设置:,或使用重置。
game.scale.setSnap(16, 16)game.scale.setSnap()Min/Max Size Constraints
最小/最大尺寸限制
js
scale: {
mode: Phaser.Scale.FIT,
width: 800,
height: 600,
min: { width: 400, height: 300 },
max: { width: 1600, height: 1200 }
}The display size is clamped to these bounds during scaling calculations.
js
scale: {
mode: Phaser.Scale.FIT,
width: 800,
height: 600,
min: { width: 400, height: 300 },
max: { width: 1600, height: 1200 }
}显示尺寸在缩放计算时会被限制在这些边界内。
Configuration Reference
配置参考
ScaleConfig (src/core/typedefs/ScaleConfig.js)
ScaleConfig(src/core/typedefs/ScaleConfig.js)
All properties go inside the object in your game config:
scale| Property | Type | Default | Description |
|---|---|---|---|
| | | Base game width. Strings like |
| | | Base game height. Strings like |
| | | Canvas zoom factor. Use |
| | | Parent DOM element or its ID. |
| | | Allow ScaleManager to set parent/body CSS height to 100%. |
| | | Scale mode constant (see table above). |
| | - | Minimum canvas dimensions. |
| | - | Maximum canvas dimensions. |
| | - | Snap values for resize rounding. |
| | | Floor display/style sizes for low-powered device performance. |
| | | Auto-centering mode (see table above). |
| | | Milliseconds between parent size checks (fallback polling). |
| | | Element to send fullscreen. If not set, Phaser creates a wrapper div. |
所有属性都放在游戏配置的对象中:
scale| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| | | 游戏基础宽度, |
| | | 游戏基础高度, |
| | | Canvas缩放因子,使用 |
| | | 父DOM元素或其ID, |
| | | 允许ScaleManager将父容器/body的CSS高度设置为100%。 |
| | | 缩放模式常量(见上表)。 |
| | - | Canvas最小尺寸。 |
| | - | Canvas最大尺寸。 |
| | - | 窗口调整时的取整吸附值。 |
| | | 对显示/样式尺寸取整,提升低性能设备的运行表现。 |
| | | 自动居中模式(见上表)。 |
| | | 父容器尺寸检查的间隔毫秒数(备用轮询机制)。 |
| | | 要设为全屏的元素,如果未设置,Phaser会创建一个包装div。 |
Events (src/scale/events/)
事件(src/scale/events/)
All events are on and are emitted by the ScaleManager instance ():
Phaser.Scale.Eventsgame.scale| Event | String Value | Callback Signature | When |
|---|---|---|---|
| | | Any resize, refresh, or scale change. The three Size parameters have |
| | | Device orientation changes. |
| | | Browser successfully enters fullscreen. |
| | | Browser leaves fullscreen (via code or user ESC). |
| | | Fullscreen request was denied by the browser. |
| | | Browser does not support the Fullscreen API. |
所有事件都在上,由ScaleManager实例()触发:
Phaser.Scale.Eventsgame.scale| 事件 | 字符串值 | 回调签名 | 触发时机 |
|---|---|---|---|
| | | 任何窗口调整、刷新或缩放变化时触发,三个Size参数包含 |
| | | 设备方向变化时触发, |
| | | 浏览器成功进入全屏时触发。 |
| | | 浏览器退出全屏时触发(通过代码或用户按ESC键)。 |
| | | 全屏请求被浏览器拒绝时触发。 |
| | | 浏览器不支持全屏API时触发。 |
API Quick Reference
API快速参考
Key Properties (read-only unless noted)
关键属性(除非注明否则为只读)
| Property | Type | Description |
|---|---|---|
| | Game width (from |
| | Game height (from |
| | Currently in fullscreen mode. |
| | Device is in portrait orientation. |
| | Device is in landscape orientation. |
| | Game dimensions are taller than wide. |
| | Game dimensions are wider than tall. |
| | Current scale mode constant. |
| | Current orientation string. |
| | Current zoom factor. |
| | Whether sizes are auto-floored. |
| | Current center mode constant. |
| | Computed parent dimensions. |
| | Unmodified game dimensions. |
| | Canvas element dimensions. |
| | CSS-scaled display dimensions. |
| | Ratio of baseSize to canvasBounds (used for input mapping). |
| | The game canvas element. |
| | DOM bounding rect of the canvas. |
| | The parent DOM element. |
| | True if parent is |
| | Polling interval in ms (writable). |
| 属性 | 类型 | 描述 |
|---|---|---|
| | 游戏宽度(来自 |
| | 游戏高度(来自 |
| | 当前是否处于全屏模式。 |
| | 设备是否为竖屏方向。 |
| | 设备是否为横屏方向。 |
| | 游戏尺寸是否为竖屏(高大于宽)。 |
| | 游戏尺寸是否为横屏(宽大于高)。 |
| | 当前缩放模式常量。 |
| | 当前方向字符串。 |
| | 当前缩放因子。 |
| | 是否自动对尺寸取整。 |
| | 当前居中模式常量。 |
| | 计算后的父容器尺寸。 |
| | 未修改的游戏尺寸。 |
| | Canvas元素尺寸。 |
| | CSS缩放后的显示尺寸。 |
| | baseSize与canvasBounds的比例(用于输入映射)。 |
| | 游戏canvas元素。 |
| | Canvas的DOM边界矩形。 |
| | 父DOM元素。 |
| | 父容器是否为 |
| | 轮询间隔毫秒数(可写)。 |
Key Methods
关键方法
| Method | Returns | Description |
|---|---|---|
| | For NONE mode: sets game, base, and display sizes directly. Updates canvas element and CSS. |
| | For scaled modes (FIT, etc.): changes the base game size and re-applies scaling. |
| | Manually set parent dimensions (useful when |
| | Change zoom factor at runtime. |
| | Set zoom to maximum integer that fits parent. |
| | Set or reset snap grid values. |
| | Calculate (but don't apply) the max zoom. |
| | Get the visible area rectangle, optionally for a specific camera. |
| | Force recalculation of scale, bounds, orientation. Emits RESIZE. |
| | Enter fullscreen. Must be called from |
| | Exit fullscreen. |
| | Toggle fullscreen state. |
| | Attempt to lock screen orientation (mobile only). |
| | Convert DOM pageX to game coordinate. |
| | Convert DOM pageY to game coordinate. |
| | Recalculate parent size. Returns true if changed. |
| | Recalculate and apply centering margins. |
| | Update |
| 方法 | 返回值 | 描述 |
|---|---|---|
| | 适用于NONE模式:直接设置游戏、基础和显示尺寸,更新canvas元素和CSS。 |
| | 适用于缩放模式(如FIT等):修改基础游戏尺寸并重新应用缩放。 |
| | 手动设置父容器尺寸(当 |
| | 在运行时修改缩放因子。 |
| | 将缩放设置为适配父容器的最大整数比例。 |
| | 设置或重置吸附网格值。 |
| | 计算(但不应用)最大缩放比例。 |
| | 获取可视区域矩形,可指定特定相机。 |
| | 强制重新计算缩放、边界和方向,触发RESIZE事件。 |
| | 进入全屏,必须从 |
| | 退出全屏。 |
| | 切换全屏状态。 |
| | 尝试锁定屏幕方向(仅移动端)。 |
| | 将DOM的pageX转换为游戏坐标。 |
| | 将DOM的pageY转换为游戏坐标。 |
| | 重新计算父容器尺寸,返回是否有变化。 |
| | 重新计算并应用居中边距。 |
| | 根据canvas的bounding client rect更新 |
Gotchas
注意事项
-
Parent element must have dimensions. The ScaleManager relies on the parent's computed CSS size. An unstyledhas zero height, which breaks centering and scaling. Either give it explicit CSS dimensions or let
<div>(the default) set body/parent to 100% height.expandParent: true -
No padding on the parent. The ScaleManager does not account for parent padding. Apply padding to a wrapper element instead, or use margins on the parent's parent.
-
Do not style the canvas directly. The ScaleManager controls,
canvas.style.width,canvas.style.height, andmarginLeft. External CSS on the canvas will conflict.marginTop -
Fullscreen requires, not
pointerup. On touch devices,pointerdownfullscreen requests are blocked unless the document already received touch input. Always usepointerdownfor reliable behavior.pointerup -
Fullscreen in iframes needs. Without this attribute on the iframe element, fullscreen requests will silently fail.
allowfullscreen -
RESIZE mode and GPU fill-rate. RESIZE creates a 1:1 pixel canvas matching the parent. On high-resolution displays this can be very large. Monitor performance on low-end devices.
-
vs
resize(). UsesetGameSize()only in NONE mode (it sets everything directly). Useresize()when using FIT/ENVELOP/etc. (it preserves the scale mode calculations).setGameSize() -
iOS height quirks. When the parent is the window on iOS, the ScaleManager usesto work around Safari's dynamic toolbar affecting
GetInnerHeight.getBoundingClientRect -
Percentage strings require a sized parent. Settingor
width: '100%'resolves againstheight: '100%'. If the parent has no size, it falls back toparentSize/window.innerWidth.innerHeight -
is a fallback poll. Modern browsers dispatch
resizeIntervalevents, but the ScaleManager also polls everyresizems (default 500) to catch edge cases on older browsers.resizeInterval
-
父元素必须有尺寸:ScaleManager依赖父容器的计算后CSS尺寸,未设置样式的高度为0,会破坏居中与缩放效果。可以给父元素设置明确的CSS尺寸,或让默认的
<div>将body/父容器高度设为100%。expandParent: true -
父元素不要加内边距:ScaleManager不会考虑父元素的内边距,可将内边距应用到包装元素,或在父元素的父元素上使用外边距。
-
不要直接设置canvas样式:ScaleManager会控制、
canvas.style.width、canvas.style.height和marginLeft,外部对canvas的CSS设置会产生冲突。marginTop -
全屏需要而非
pointerup:在触摸设备上,pointerdown触发的全屏请求会被拦截,除非文档已接收过触摸输入,始终使用pointerdown才能保证可靠运行。pointerup -
iframe中的全屏需要属性:如果iframe没有该属性,全屏请求会静默失败。
allowfullscreen -
RESIZE模式与GPU填充率:RESIZE会创建与父容器1:1像素匹配的canvas,在高分辨率显示器上尺寸可能很大,需关注低性能设备的运行表现。
-
与
resize()的区别:仅在NONE模式下使用setGameSize()(直接设置所有尺寸),在FIT/ENVELOP等模式下使用resize()(保留缩放模式计算逻辑)。setGameSize() -
iOS高度问题:当父容器是iOS设备的窗口时,ScaleManager会使用来规避Safari动态工具栏影响
GetInnerHeight的问题。getBoundingClientRect -
百分比字符串需要父容器有尺寸:设置或
width: '100%'会根据height: '100%'解析,如果父容器没有尺寸,会回退到parentSize/window.innerWidth。innerHeight -
是备用轮询机制:现代浏览器会触发
resizeInterval事件,但ScaleManager仍会每隔resize毫秒(默认500)轮询一次,以兼容旧浏览器的边缘情况。resizeInterval
Source File Map
源码文件映射
| File | Purpose |
|---|---|
| Main class -- all scaling, centering, fullscreen, orientation logic. Access via |
| Scale mode constants: NONE, WIDTH_CONTROLS_HEIGHT, HEIGHT_CONTROLS_WIDTH, FIT, ENVELOP, RESIZE, EXPAND. |
| Center mode constants: NO_CENTER, CENTER_BOTH, CENTER_HORIZONTALLY, CENTER_VERTICALLY. |
| Zoom constants: NO_ZOOM, ZOOM_2X, ZOOM_4X, MAX_ZOOM. |
| Orientation string constants: LANDSCAPE, LANDSCAPE_SECONDARY, PORTRAIT, PORTRAIT_SECONDARY. |
| Resize event definition. Callback receives gameSize, baseSize, displaySize, previousWidth, previousHeight. |
| Emitted on successful fullscreen entry. |
| Emitted when leaving fullscreen. |
| Emitted when fullscreen request is denied. |
| Emitted when browser lacks Fullscreen API support. |
| Emitted on device orientation change. Callback receives orientation string. |
| JSDoc typedef for the |
| The Size component class used by gameSize, baseSize, displaySize. |
| iOS-specific workaround for getting accurate viewport height. |
| Determines current screen orientation from dimensions. |
| 文件 | 用途 |
|---|---|
| 主类——包含所有缩放、居中、全屏、方向逻辑,可通过 |
| 缩放模式常量:NONE、WIDTH_CONTROLS_HEIGHT、HEIGHT_CONTROLS_WIDTH、FIT、ENVELOP、RESIZE、EXPAND。 |
| 居中模式常量:NO_CENTER、CENTER_BOTH、CENTER_HORIZONTALLY、CENTER_VERTICALLY。 |
| 缩放常量:NO_ZOOM、ZOOM_2X、ZOOM_4X、MAX_ZOOM。 |
| 方向字符串常量:LANDSCAPE、LANDSCAPE_SECONDARY、PORTRAIT、PORTRAIT_SECONDARY。 |
| 窗口调整事件定义,回调接收gameSize、baseSize、displaySize、previousWidth、previousHeight。 |
| 成功进入全屏时触发。 |
| 退出全屏时触发。 |
| 全屏请求被拒绝时触发。 |
| 浏览器不支持全屏API时触发。 |
| 设备方向变化时触发,回调接收方向字符串。 |
| |
| gameSize、baseSize、displaySize使用的Size组件类。 |
| iOS专用的获取准确视口高度的解决方案。 |
| 根据尺寸判断当前屏幕方向。 |