Skip to content

Commit

Permalink
toggle global animation in live mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Algorush committed Dec 15, 2023
1 parent 526db89 commit 5be0d4c
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,38 @@ AFRAME.registerComponent('street', {
globalAnimated: { default: false },
length: { default: 60 } // new default of 60 from 0.4.4
},
toggleGlobalAnimation: function () {
const globalAnimated = this.data.globalAnimated;
const animatedElements = document.querySelectorAll('a-entity[animation__1]');
animatedElements.forEach(animatedElem => {
// hook to toggle animations that bypass play/pause events that are called by the inspector
animatedElem.components.animation__1.initialized = globalAnimated;
});

// toggle animations for elements with animation-mixer component
const animationMixerElements = document.querySelectorAll('a-entity[animation-mixer]');
animationMixerElements.forEach(animatedElem => {
if (globalAnimated) {
animatedElem.components['animation-mixer'].playAction();
} else {
animatedElem.components['animation-mixer'].stopAction();
}
});

// toggle wheel animation for all elements with wheel component
const wheelElements = document.querySelectorAll('a-entity[wheel]');
wheelElements.forEach(wheelElem => {
wheelElem.components['wheel'].data.isActive = globalAnimated;
});
},
update: function (oldData) { // fired once at start and at each subsequent change of a schema value
var data = this.data;

if (data.globalAnimated !== oldData.globalAnimated && oldData.JSON === data.JSON) {
this.toggleGlobalAnimation();
return;
}

if (data.JSON.length === 0) {
if (oldData.JSON !== undefined && oldData.JSON.length === 0) { return; } // this has happened before, surpress console log
console.log('[street]', 'No JSON provided yet, but it might be set at runtime');
Expand Down Expand Up @@ -486,7 +515,8 @@ AFRAME.registerComponent('street-environment', {
AFRAME.registerComponent('wheel', {
schema: {
speed: { type: 'number', default: 1 },
wheelDiameter: { type: 'number', default: 1 }
wheelDiameter: { type: 'number', default: 1 },
isActive: { type: 'boolean', default: true}
},

init: function () {
Expand All @@ -509,6 +539,8 @@ AFRAME.registerComponent('wheel', {
});
},
tick: function (t, dt) {
if (!this.data.isActive) return;

const speed = this.data.speed / 1000;
const wheelDiameter = this.data.wheelDiameter;

Expand Down

0 comments on commit 5be0d4c

Please sign in to comment.