Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat: add Canvas-based camera scaling with multiple scale modes

esengine4d6baa406c51
diff --git a/editor/src/math/Transform.ts b/editor/src/math/Transform.ts
index 7c4215aa8..94a292299 100644
--- a/editor/src/math/Transform.ts
+++ b/editor/src/math/Transform.ts
@@ -217,6 +217,12 @@ export function getUIRectFromEntity(entity: EntityDataLike): UIRectLike | null {
}
export function getEntitySize(entity: EntityDataLike): { x: number; y: number } {
+ const canvas = entity.components.find(c => c.type === 'Canvas');
+ if (canvas?.data?.designResolution) {
+ const res = canvas.data.designResolution as { x: number; y: number };
+ return { x: res.x, y: res.y };
+ }
+
const uiRect = getUIRectFromEntity(entity);
if (uiRect?.size) {
return uiRect.size;
diff --git a/editor/src/scene/EditorSceneManager.ts b/editor/src/scene/EditorSceneManager.ts
index f5a5a3899..852cbf93f 100644
--- a/editor/src/scene/EditorSceneManager.ts
+++ b/editor/src/scene/EditorSceneManager.ts
@@ -439,22 +439,22 @@ export class EditorSceneManager {
}
}
- return this.getCanvasSize();
- }
-
- private getCanvasSize(): { x: number; y: number } {
- if (this.sceneData_) {
- for (const entity of this.sceneData_.entities) {
- const canvas = entity.components.find(c => c.type === 'Canvas');
- if (canvas?.data) {
- const resolution = (canvas.data as any).designResolution;
- if (resolution) {
- return { x: resolution.x, y: resolution.y };
- }
- }
+ return this.findCanvasAncestorSize(entity);
+ }
+
+ private findCanvasAncestorSize(entity: EntityData): { x: number; y: number } {
+ let current = entity;
+ while (current.parent !== null) {
+ const parent = this.entityDataMap_.get(current.parent);
+ if (!parent) break;
+ const canvas = parent.components.find(c => c.type === 'Canvas');
+ if (canvas?.data?.designResolution) {
+ const res = canvas.data.designResolution as { x: number; y: number };
+ return { x: res.x, y: res.y };
}
+ current = parent;
}
- return { x: 1920, y: 1080 };
+ return { x: 0, y: 0 };
}
private resolveAssetRef(ref: string): string {
diff --git a/editor/src/sync/RuntimeSyncService.ts b/editor/src/sync/RuntimeSyncService.ts
index 2fa1ef222..40aabe1b7 100644
--- a/editor/src/sync/RuntimeSyncService.ts
+++ b/editor/src/sync/RuntimeSyncService.ts
@@ -65,6 +65,30 @@ export class RuntimeSyncService {
private onPropertyChange(event: PropertyChangeEvent): void {
this.scheduleEntityUpdate(event.entity);
+
+ if (event.componentType === 'Canvas') {
+ this.syncCanvasToCamera(event.entity);
+ }
+ }
+
+ private syncCanvasToCamera(canvasEntityId: number): void {
+ const canvasData = this.store_.getEntityData(canvasEntityId);
+ if (!canvasData) return;
+
+ const canvasComp = canvasData.components.find(c => c.type === 'Canvas');
+ const resolution = canvasComp?.data?.designResolution as { x: number; y: number } | undefined;
+ if (!resolution) return;
+
+ const orthoSize = resolution.y / 2;
+
+ for (const entity of this.store_.scene.entities) {
+ const cameraComp = entity.components.find(c => c.type === 'Camera');
+ if (!cameraComp) continue;
+
+ this.store_.updatePropertyDirect(entity.id, 'Camera', 'orthoSize', orthoSize);
+ this.scheduleEntityUpdate(entity.id);
+ break;
+ }
}
private onHierarchyChange(event: HierarchyChangeEvent): void {
diff --git a/editor/src/types/ProjectTypes.ts b/editor/src/types/ProjectTypes.ts
index 579564ea1..4f2708e08 100644
--- a/editor/src/types/ProjectTypes.ts
+++ b/editor/src/types/ProjectTypes.ts
@@ -17,6 +17,7 @@ export interface ProjectConfig {
created: string;
modified: string;
spineVersion?: SpineVersion;
+ designResolution?: { width: number; height: number };
}
// =============================================================================
diff --git a/sdk/src/app.ts b/sdk/src/app.ts
index b967e3f5c..d12f3b084 100644
--- a/sdk/src/app.ts
+++ b/sdk/src/app.ts
@@ -310,6 +310,46 @@ const IDENTITY = new Float32Array([
0, 0, 0, 1,
]);
+function findCanvasData(registry: CppRegistry, scanLimit: number): {
+ designResolution: { x: number; y: number };
+ scaleMode: number;
+ matchWidthOrHeight: number;
+} | null {
+ for (let e = 0; e < scanLimit; e++) {
+ if (!registry.valid(e) || !registry.hasCanvas(e)) continue;
+ const canvas = registry.getCanvas(e) as {
+ designResolution: { x: number; y: number };
+ scaleMode: number;
+ matchWidthOrHeight: number;
+ };
+ return canvas;
+ }
+ return null;
+}
+
+function computeEffectiveOrthoSize(
+ baseOrthoSize: number,
+ designAspect: number,
+ actualAspect: number,
+ scaleMode: number,
+ matchWidthOrHeight: number,
+): number {
+ const orthoForWidth = baseOrthoSize * designAspect / actualAspect;
+ const orthoForHeight = baseOrthoSize;
+
+ switch (scaleMode) {
+ case 0: return orthoForWidth;
+ case 1: return orthoForHeight;
+ case 2: return Math.max(orthoForWidth, orthoForHeight);
+ case 3: return Math.min(orthoForWidth, orthoForHeight);
+ case 4: {
+ const t = matchWidthOrHeight;
+ return Math.pow(orthoForWidth, 1 - t) * Math.pow(orthoForHeight, t);
+ }
+ default: return orthoForHeight;
+ }
+}
+
function computeViewProjection(registry: CppRegistry, width: number, height: number): Float32Array {
const count = registry.entityCount();
const scanLimit = count + 1000;
@@ -337,7 +377,18 @@ function computeViewProjection(registry: CppRegistry, width: number, height: num
let projection: Float32Array;
if (camera.projectionType === 1) {
- const halfH = camera.orthoSize;
+ let halfH = camera.orthoSize;
+
+ const canvas = findCanvasData(registry, scanLimit);
+ if (canvas) {
+ const baseOrthoSize = canvas.designResolution.y / 2;
+ const designAspect = canvas.designResolution.x / canvas.designResolution.y;
+ halfH = computeEffectiveOrthoSize(
+ baseOrthoSize, designAspect, aspect,
+ canvas.scaleMode, canvas.matchWidthOrHeight,
+ );
+ }
+
const halfW = halfH * aspect;
projection = ortho(-halfW, halfW, -halfH, halfH, -camera.farPlane, camera.farPlane);
} else {