Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active September 22, 2022 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderofsalvation/c11903dc247452d36540f75f3cb07f68 to your computer and use it in GitHub Desktop.
Save coderofsalvation/c11903dc247452d36540f75f3cb07f68 to your computer and use it in GitHub Desktop.
AFRAME.utils.throttleTicks() allows central (throttle)control of all AFRAME components-ticks (without having to patch community components)
/*
* Usage:
*
* AFRAME.utils.throttleTicks({
* 'aabb-collider':{ "*": 150 },
* 'texturescroll':{ "*": 50 },
* 'foo': { "*": AFRAME.utils.device.isMobile() ? 150 : 50 }
* }, true)
*
*/
AFRAME.utils.throttleTicks = (opts, debug) => {
for ( let i in AFRAME.components ) {
let com = AFRAME.components[i]
let opt = opts[i]
let proto = com.Component.prototype
if( opt && proto.tick ){
proto.init = function(init){
return function(){
init.apply(this, arguments)
this.tickOrig = this.tick
this.throttleTicks = {}
for ( let i in opt )
this.throttleTicks[i] = AFRAME.utils.throttleTick(this.tickOrig, opt[i], this )
this.tick = function(t, dt){
let f = this.throttleTicks[ '#'+String(this.el.id ) ] ||
this.throttleTicks[ '.'+String(this.el.className) ] ||
this.throttleTicks[ '*' ]
f(t, dt)
}
}
}(proto.init)
}else if(debug && proto.tick) console.warn(`throttleTicks: ${i} was not specified`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment