Skip to content

Commit cb49470

Browse files
feat(position): dialog box position, dynamically face camera
1 parent 905a776 commit cb49470

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

examples/advanced/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</head>
1010
<body>
1111
<a-scene inspector="url: https://aframe.io/releases/0.3.0/aframe-inspector.min.js">
12-
<a-camera id="camera" wasd-controls-enabled="false" cursor="rayOrigin: mouse"></a-camera>
12+
<a-camera id="camera" wasd-controls-enabled="true" cursor="rayOrigin: mouse"></a-camera>
1313
<a-sky color="#24CAFF"></a-sky>
1414
<a-entity
1515
id="dialog"

index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ AFRAME.registerComponent('dialog-popup', {
5252
type: 'string',
5353
default: 'click'
5454
},
55-
defaultOpen: {
56-
type: 'bool',
57-
default: false
58-
},
5955
openIconImage: {
6056
type: 'asset',
6157
default: ''
@@ -117,13 +113,23 @@ AFRAME.registerComponent('dialog-popup', {
117113
*/
118114
init() {
119115
this.spawnEntities();
116+
this.cameraEl = document.querySelector('[camera]');
117+
},
118+
/**
119+
* If the component is open, ensure it always faces the camera.
120+
*/
121+
tick() {
122+
if (this.isOpen) {
123+
this.positionDialogPlane();
124+
}
120125
},
121126
/**
122127
* Handles opening and closing the dialog plane.
123128
*/
124129
toggleDialogOpen() {
125130
this.isOpen = !this.isOpen;
126131
if (this.dialogPlane) {
132+
this.positionDialogPlane();
127133
this.dialogPlane.setAttribute('visible', this.isOpen);
128134
}
129135
},
@@ -306,13 +312,9 @@ AFRAME.registerComponent('dialog-popup', {
306312
} = this.data;
307313

308314
const plane = document.createElement('a-entity');
315+
plane.setAttribute('id', `${this.el.getAttribute('id')}--dialog-plane`);
309316

310-
// The dialog should always be a little closer to the camera than the icon.
311-
const position = Object.assign({}, this.el.getAttribute('position'));
312-
position.z += 1;
313-
314-
plane.setAttribute('visible', this.data.defaultOpen);
315-
plane.setAttribute('position', position);
317+
plane.setAttribute('visible', false);
316318
plane.setAttribute('geometry', {
317319
primitive: 'plane',
318320
width: width + padding,
@@ -330,8 +332,17 @@ AFRAME.registerComponent('dialog-popup', {
330332
plane.appendChild(this.generateBody());
331333

332334
this.dialogPlane = plane;
335+
333336
return plane;
334337
},
338+
positionDialogPlane() {
339+
if (this.dialogPlane) {
340+
const vector = this.dialogPlane.object3D.parent.worldToLocal(
341+
this.cameraEl.object3D.getWorldPosition()
342+
);
343+
this.dialogPlane.object3D.lookAt(vector);
344+
}
345+
},
335346
spawnEntities() {
336347
const wrapper = document.createElement('a-entity');
337348
wrapper.setAttribute('id', `${this.el.getAttribute('id')}--wrapper`);

0 commit comments

Comments
 (0)