Skip to content

Commit 075a1a2

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
fix(soba): move effects into constructor
1 parent ae10eac commit 075a1a2

File tree

6 files changed

+20
-35
lines changed

6 files changed

+20
-35
lines changed

apps/demo/src/app/app.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { NgTemplateOutlet } from '@angular/common';
21
import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component, signal } from '@angular/core';
32
import { NgtCanvas, extend } from 'angular-three';
3+
import { NgtsOrbitControls } from 'angular-three-soba/controls';
44
import * as THREE from 'three';
55

66
extend(THREE);
@@ -39,8 +39,9 @@ export class Cube {
3939
<ngt-spot-light [position]="5" />
4040
<ngt-point-light [position]="-5" />
4141
<app-cube />
42+
<ngts-orbit-controls />
4243
`,
43-
imports: [Cube, NgTemplateOutlet],
44+
imports: [Cube, NgtsOrbitControls],
4445
schemas: [CUSTOM_ELEMENTS_SCHEMA],
4546
changeDetection: ChangeDetectionStrategy.OnPush,
4647
})

libs/soba/abstractions/src/text-3d/text-3d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export class NgtsText3D extends NgtSignalStore<NgtsText3DState> {
134134
readonly geometryArgs = computed(() => {
135135
const fontData = this.#fontData();
136136
if (!fontData) return null;
137+
137138
const text = this.select('text');
138139
const size = this.select('size');
139140
const height = this.select('height');

libs/soba/abstractions/src/text/text.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
EventEmitter,
66
Injector,
77
Input,
8-
NgZone,
98
Output,
109
computed,
1110
effect,
@@ -216,7 +215,6 @@ export class NgtsText extends NgtSignalStore<NgtsTextState> {
216215

217216
readonly troikaText = new Text();
218217

219-
readonly #zone = inject(NgZone);
220218
readonly #injector = inject(Injector);
221219
readonly #store = inject(NgtStore);
222220

@@ -227,13 +225,8 @@ export class NgtsText extends NgtSignalStore<NgtsTextState> {
227225
inject(DestroyRef).onDestroy(() => {
228226
this.troikaText.dispose();
229227
});
230-
}
231-
232-
ngOnInit() {
233-
this.#zone.runOutsideAngular(() => {
234-
this.#preloadFont();
235-
this.#syncText();
236-
});
228+
this.#preloadFont();
229+
this.#syncText();
237230
}
238231

239232
#preloadFont() {

libs/soba/controls/src/orbit-controls/orbit-controls.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
inject,
88
Injector,
99
Input,
10-
NgZone,
11-
OnInit,
1210
Output,
1311
} from '@angular/core';
1412
import { injectBeforeRender, injectNgtRef, NgtArgs, NgtSignalStore, NgtStore, NgtVector3 } from 'angular-three';
@@ -36,7 +34,7 @@ declare global {
3634
imports: [NgtArgs],
3735
schemas: [CUSTOM_ELEMENTS_SCHEMA],
3836
})
39-
export class NgtsOrbitControls extends NgtSignalStore<NgtsOrbitControlsState> implements OnInit {
37+
export class NgtsOrbitControls extends NgtSignalStore<NgtsOrbitControlsState> {
4038
@Input() controlsRef = injectNgtRef<OrbitControls>();
4139

4240
@Input() set camera(camera: THREE.Camera) {
@@ -69,7 +67,6 @@ export class NgtsOrbitControls extends NgtSignalStore<NgtsOrbitControlsState> im
6967

7068
readonly #store = inject(NgtStore);
7169
readonly #injector = inject(Injector);
72-
readonly #zone = inject(NgZone);
7370

7471
readonly args = computed(() => [this.controlsRef.nativeElement]);
7572
readonly damping = this.select('enableDamping');
@@ -85,15 +82,11 @@ export class NgtsOrbitControls extends NgtSignalStore<NgtsOrbitControlsState> im
8582
},
8683
{ priority: -1 }
8784
);
88-
}
8985

90-
ngOnInit() {
91-
this.#zone.runOutsideAngular(() => {
92-
this.#setControls();
93-
this.#connectElement();
94-
this.#makeControlsDefault();
95-
this.#setEvents();
96-
});
86+
this.#setControls();
87+
this.#connectElement();
88+
this.#makeControlsDefault();
89+
this.#setEvents();
9790
}
9891

9992
#setControls() {

libs/soba/staging/src/center/center.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
inject,
88
Injector,
99
Input,
10-
OnInit,
1110
Output,
1211
} from '@angular/core';
1312
import { extend, injectNgtRef, NgtSignalStore, type NgtGroup } from 'angular-three';
@@ -54,7 +53,7 @@ declare global {
5453
`,
5554
schemas: [CUSTOM_ELEMENTS_SCHEMA],
5655
})
57-
export class NgtsCenter extends NgtSignalStore<NgtsCenterState> implements OnInit {
56+
export class NgtsCenter extends NgtSignalStore<NgtsCenterState> {
5857
@Input() centerRef = injectNgtRef<Group>();
5958

6059
readonly outerRef = injectNgtRef<Group>();
@@ -124,9 +123,6 @@ export class NgtsCenter extends NgtSignalStore<NgtsCenterState> implements OnIni
124123

125124
constructor() {
126125
super({ precise: true });
127-
}
128-
129-
ngOnInit(): void {
130126
this.#setPosition();
131127
}
132128

libs/soba/staging/src/float/float.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ export class NgtsFloat extends NgtSignalStore<NgtsFloatState> {
5858

5959
constructor() {
6060
super({ speed: 1, rotationIntensity: 1, floatIntensity: 1, floatingRange: [-0.1, 0.1], enabled: true });
61-
injectBeforeRender(this.onBeforeRender.bind(this));
61+
injectBeforeRender(this.#onBeforeRender.bind(this));
6262
}
6363

64-
private onBeforeRender({ clock }: NgtRenderState) {
65-
if (!this.floatRef.untracked) return;
64+
#onBeforeRender({ clock }: NgtRenderState) {
65+
const float = this.floatRef.untracked;
66+
if (!float) return;
6667
const { enabled, speed, floatingRange, floatIntensity, rotationIntensity } = this.get();
6768
if (!enabled || speed === 0) return;
6869

6970
const t = this.#offset + clock.getElapsedTime();
70-
this.floatRef.untracked.rotation.x = (Math.cos((t / 4) * speed) / 8) * rotationIntensity;
71-
this.floatRef.untracked.rotation.y = (Math.sin((t / 4) * speed) / 8) * rotationIntensity;
72-
this.floatRef.untracked.rotation.z = (Math.sin((t / 4) * speed) / 20) * rotationIntensity;
71+
float.rotation.x = (Math.cos((t / 4) * speed) / 8) * rotationIntensity;
72+
float.rotation.y = (Math.sin((t / 4) * speed) / 8) * rotationIntensity;
73+
float.rotation.z = (Math.sin((t / 4) * speed) / 20) * rotationIntensity;
7374
let yPosition = Math.sin((t / 4) * speed) / 10;
7475
yPosition = THREE.MathUtils.mapLinear(yPosition, -0.1, 0.1, floatingRange[0] ?? -0.1, floatingRange[1] ?? 0.1);
75-
this.floatRef.untracked.position.y = yPosition * floatIntensity;
76+
float.position.y = yPosition * floatIntensity;
7677
}
7778
}

0 commit comments

Comments
 (0)