MCPcopy Create free account
hub / github.com/esengine/esengine / useEngine

Function useEngine

packages/editor-app/src/hooks/useEngine.ts:101–230  ·  view source on GitHub ↗
(
    canvasIdOrOptions: string | UseEngineOptions,
    autoInit?: boolean
)

Source from the content-addressed store, hash-verified

99export function useEngine(canvasId: string, autoInit?: boolean): UseEngineReturn;
100export function useEngine(options: UseEngineOptions): UseEngineReturn;
101export function useEngine(
102 canvasIdOrOptions: string | UseEngineOptions,
103 autoInit?: boolean
104): UseEngineReturn {
105 // Parse options
106 const options: UseEngineOptions = typeof canvasIdOrOptions === 'string'
107 ? {
108 viewportId: canvasIdOrOptions, // Use canvasId as viewportId for backward compatibility
109 canvasId: canvasIdOrOptions,
110 showGrid: true,
111 showGizmos: true,
112 autoInit
113 }
114 : {
115 showGrid: true,
116 showGizmos: true,
117 autoInit: true,
118 ...canvasIdOrOptions
119 };
120
121 const engineRef = useRef<EngineService>(EngineService.getInstance());
122 const statsIntervalRef = useRef<number | null>(null);
123 const viewportRegisteredRef = useRef(false);
124
125 const [state, setState] = useState<EngineState>({
126 initialized: engineInitialized,
127 running: false,
128 fps: 0,
129 drawCalls: 0,
130 spriteCount: 0,
131 error: null
132 });
133
134 // Initialize engine and register viewport
135 useEffect(() => {
136 if (!options.autoInit) return;
137
138 const init = async () => {
139 try {
140 // Initialize engine with primary canvas (first viewport)
141 await initializeEngine(options.canvasId);
142 setState((prev) => ({ ...prev, initialized: true, error: null }));
143
144 // Register this viewport
145 if (!viewportRegisteredRef.current) {
146 engineRef.current.registerViewport(options.viewportId, options.canvasId);
147 engineRef.current.setViewportConfig(
148 options.viewportId,
149 options.showGrid ?? true,
150 options.showGizmos ?? true
151 );
152 viewportRegisteredRef.current = true;
153 }
154
155 // Start stats update interval
156 if (!statsIntervalRef.current) {
157 statsIntervalRef.current = window.setInterval(() => {
158 const stats = engineRef.current.getStats();

Callers 1

ViewportFunction · 0.90

Calls 7

initFunction · 0.70
stopMethod · 0.65
updateMethod · 0.65
loadTextureMethod · 0.65
getInstanceMethod · 0.45
startMethod · 0.45
createSpriteEntityMethod · 0.45

Tested by

no test coverage detected