Moved throws to a single loop.

This commit is contained in:
Uwe Oestermeier 2023-07-20 14:38:23 +02:00
parent 52ccb49fa8
commit 97fb30d741

View File

@ -88,6 +88,24 @@ export class ResizeEvent extends BaseEvent {
* *
* @constructor * @constructor
*/ */
let ThrowableObjects = []
let ThrowableRequests = null
function stepThrowAnimation() {
let clones = [...ThrowableObjects]
ThrowableObjects = []
for(let obj of clones) {
obj.animateThrow()
}
requestAnimationFrame(stepThrowAnimation)
}
function startThrowableLoop() {
requestAnimationFrame(stepThrowAnimation)
return true
}
class Throwable { class Throwable {
constructor({ constructor({
movableX = true, movableX = true,
@ -181,6 +199,13 @@ class Throwable {
return dt return dt
} }
recurseAnimateThrow() {
ThrowableObjects.push(this)
if (ThrowableRequests == null) {
ThrowableRequests = startThrowableLoop()
}
}
animateThrow(time) { animateThrow(time) {
if (this.velocity != null) { if (this.velocity != null) {
let dt = this._throwDeltaTime() let dt = this._throwDeltaTime()
@ -199,19 +224,11 @@ class Throwable {
this.onDragUpdate(d) this.onDragUpdate(d)
if (dt == 0 || this.needsAnimation()) { if (dt == 0 || this.needsAnimation()) {
if (this._requestAnimation != 0) { this.recurseAnimateThrow()
console.log("cancelAnimationFrame")
cancelAnimationFrame(this._requestAnimation)
}
this._requestAnimation = requestAnimationFrame(this.animateThrow.bind(this))
return return
} else { } else {
if (this.isOutside()) { if (this.isOutside()) {
if (this._requestAnimation != 0) { this.recurseAnimateThrow()
console.log("cancelAnimationFrame")
cancelAnimationFrame(this._requestAnimation)
}
this._requestAnimation = requestAnimationFrame(this.animateThrow.bind(this))
return return
} }
} }
@ -222,48 +239,6 @@ class Throwable {
} }
} }
animateThrowTween(time) {
if (this.velocity != null) {
let dt = this._throwDeltaTime()
// console.log("animateThrow", dt)
let next = this.nextVelocity(this.velocity)
let prevLength = Points.length(this.velocity)
let nextLength = Points.length(next)
if (nextLength > prevLength) {
let factor = nextLength / prevLength
next = Points.multiplyScalar(next, 1 / factor)
// console.log('Prevent acceleration', factor, this.velocity, next)
}
this.velocity = next
let d = Points.multiplyScalar(this.velocity, dt)
this._move(d)
this.onDragUpdate(d)
TweenMax.to(this, 0.5, { _dummy: 1,
ease: Linear.easeNone,
onComplete: () => {
if (dt == 0 || this.needsAnimation() || this.isOutside()) {
this.animateThrow()
}}})
/* if (dt == 0 || this.needsAnimation()) {
requestAnimationFrame(this.animateThrow.bind(this))
return
} else {
if (this.isOutside()) {
requestAnimationFrame(this.animateThrow.bind(this))
return
}
} */
}
this.onDragComplete()
if (this.onThrowFinished != null) {
this.onThrowFinished()
}
}
needsAnimation() { needsAnimation() {
if (this.velocity == null) { if (this.velocity == null) {
return false return false