Skip to content

Commit ad00523

Browse files
committed
docs(examples): add pointer events demo
1 parent 5e83307 commit ad00523

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

apps/examples/src/app/misc/misc.routes.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ const routes: Routes = [
1616
},
1717
},
1818
},
19+
{
20+
path: 'pointer-events',
21+
loadComponent: () => import('./pointer-events/pointer-events'),
22+
data: {
23+
credits: {
24+
title: 'Pointer Events',
25+
link: 'https://docs.tresjs.org/api/events.html',
26+
class: 'text-white',
27+
},
28+
},
29+
},
1930
{
2031
path: '',
2132
redirectTo: 'basic',
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { NgtCanvas, NgtCanvasContent } from 'angular-three/dom';
3+
import { SceneGraph } from './scene';
4+
5+
@Component({
6+
template: `
7+
<ngt-canvas [camera]="{ position: [11, 11, 11], fov: 45, near: 0.1, far: 1000 }" [lookAt]="[-8, 3, -3]">
8+
<app-scene-graph *canvasContent />
9+
</ngt-canvas>
10+
`,
11+
changeDetection: ChangeDetectionStrategy.OnPush,
12+
host: { class: 'pointer-events' },
13+
imports: [NgtCanvas, NgtCanvasContent, SceneGraph],
14+
})
15+
export default class PoointEvents {}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild } from '@angular/core';
2+
import { injectBeforeRender, NgtArgs, NgtThreeElements } from 'angular-three';
3+
import { NgtsOrbitControls } from 'angular-three-soba/controls';
4+
import * as THREE from 'three';
5+
6+
@Component({
7+
selector: 'app-scene-graph',
8+
template: `
9+
<ngt-color *args="['#201919']" attach="background" />
10+
11+
<ngt-ambient-light [intensity]="0.5" />
12+
<ngt-spot-light [position]="[0, 8, 4]" [intensity]="Math.PI" [decay]="0" [angle]="2" />
13+
14+
<ngt-group #group>
15+
@for (x of positions; track $index) {
16+
@for (y of positions; track $index) {
17+
@for (z of positions; track $index) {
18+
<ngt-mesh
19+
[position]="[x, y, z]"
20+
(pointerenter)="$event.stopPropagation(); onPointerEnter(material)"
21+
(pointerleave)="$event.stopPropagation(); onPointerLeave(material)"
22+
>
23+
<ngt-box-geometry />
24+
<ngt-mesh-standard-material #material color="#efefef" [roughness]="0.5" [metalness]="0.5" />
25+
</ngt-mesh>
26+
}
27+
}
28+
}
29+
</ngt-group>
30+
31+
<ngts-orbit-controls />
32+
`,
33+
imports: [NgtsOrbitControls, NgtArgs],
34+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
35+
changeDetection: ChangeDetectionStrategy.OnPush,
36+
})
37+
export class SceneGraph {
38+
protected readonly Math = Math;
39+
40+
private groupRef = viewChild.required<ElementRef<THREE.Group>>('group');
41+
42+
protected readonly positions = [-2.5, 0, 2.5];
43+
44+
constructor() {
45+
injectBeforeRender(({ delta }) => {
46+
this.groupRef().nativeElement.rotation.y += delta * 0.25;
47+
});
48+
}
49+
50+
onPointerEnter(material: NgtThreeElements['ngt-mesh-standard-material']) {
51+
(material as THREE.MeshStandardMaterial).color.set('mediumpurple');
52+
}
53+
54+
onPointerLeave(material: NgtThreeElements['ngt-mesh-standard-material']) {
55+
(material as THREE.MeshStandardMaterial).color.set('#efefef');
56+
}
57+
}

0 commit comments

Comments
 (0)