Skip to content

Commit

Permalink
listen to hashchange to dynamically move to the waypoint (fix #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentfretin committed Apr 5, 2024
1 parent 9490f4d commit c4bffac
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/components/waypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,25 +272,35 @@ AFRAME.registerComponent("waypoint", {
AFRAME.registerComponent("move-to-spawn-point", {
init() {
this.move = this.move.bind(this);
this.locationHashChanged = this.locationHashChanged.bind(this);
},

locationHashChanged() {
const hash = window.location.hash;
if (hash !== "") {
const waypoint = document.querySelector(hash);
if (waypoint) {
waypoint.emit("click", { withTransition: false });
return true;
}
}
return false;
},

play() {
this.el.sceneEl.addEventListener("waypoints-ready", this.move);
window.addEventListener("hashchange", this.locationHashChanged);
},

pause() {
this.el.sceneEl.removeEventListener("waypoints-ready", this.move);
window.removeEventListener("hashchange", this.locationHashChanged);
},

move() {
// If the url has a hash and the hash is a waypoint then spawn at that waypoint.
const hash = window.location.hash;
if (hash !== "") {
const waypoint = document.querySelector(hash);
if (waypoint) {
waypoint.emit("click", { withTransition: false });
return;
}
if (this.locationHashChanged()) {
return;
}

// Else spawn at the first defined spawn point or the center.
Expand Down

0 comments on commit c4bffac

Please sign in to comment.