Skip to content

Commit ae5ec13

Browse files
committed
fix(core): make sure camera and dpr is set correctly initially
1 parent 675883c commit ae5ec13

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

libs/core/src/lib/roots.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function injectCanvasRootInitializer(injector?: Injector) {
112112
if (!state.camera || (state.camera === lastCamera && !is.equ(lastCamera, cameraOptions, shallowLoose))) {
113113
lastCamera = cameraOptions;
114114
const isCamera = is.camera(cameraOptions);
115-
let camera = isCamera ? cameraOptions : makeCameraInstance(orthographic, state.size);
115+
let camera = isCamera ? cameraOptions : makeCameraInstance(orthographic, sizeOptions ?? state.size);
116116

117117
if (!isCamera) {
118118
camera.position.z = 5;
@@ -260,7 +260,7 @@ export function injectCanvasRootInitializer(injector?: Injector) {
260260
if (gl.setClearAlpha) {
261261
gl.setClearAlpha(0);
262262
}
263-
gl.setPixelRatio(makeDpr(state.viewport.dpr ?? dpr));
263+
gl.setPixelRatio(makeDpr(state.viewport.dpr));
264264
gl.setSize(sizeOptions?.width ?? state.size.width, sizeOptions?.height ?? state.size.height);
265265

266266
if (
@@ -305,9 +305,7 @@ export function injectCanvasRootInitializer(injector?: Injector) {
305305
export type NgtCanvasConfigurator = ReturnType<ReturnType<typeof injectCanvasRootInitializer>>;
306306

307307
function computeInitialSize(canvas: NgtCanvasElement, defaultSize?: NgtSize): NgtSize {
308-
if (defaultSize) {
309-
return defaultSize;
310-
}
308+
if (defaultSize) return defaultSize;
311309

312310
if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement && canvas.parentElement) {
313311
return canvas.parentElement.getBoundingClientRect();

libs/core/src/lib/store.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DOCUMENT } from '@angular/common';
12
import { ElementRef, InjectOptions, InjectionToken, effect, inject } from '@angular/core';
23
import { Subject } from 'rxjs';
34
import { Clock, Vector2, Vector3 } from 'three';
@@ -10,6 +11,8 @@ import { updateCamera } from './utils/update';
1011

1112
function storeFactory(previousStore: NgtSignalStore<NgtState> | null) {
1213
const loop = injectLoop();
14+
const document = inject(DOCUMENT);
15+
const window = document.defaultView || undefined;
1316

1417
// NOTE: using Subject because we do not care about late-subscribers
1518
const pointerMissed$ = new Subject<MouseEvent>();
@@ -88,8 +91,8 @@ function storeFactory(previousStore: NgtSignalStore<NgtState> | null) {
8891

8992
size: { width: 0, height: 0, top: 0, left: 0, updateStyle: false },
9093
viewport: {
91-
initialDpr: 0,
92-
dpr: 0,
94+
initialDpr: window?.devicePixelRatio || 1,
95+
dpr: window?.devicePixelRatio || 1,
9396
width: 0,
9497
height: 0,
9598
top: 0,

0 commit comments

Comments
 (0)