Added cancelAnimationFrame

This commit is contained in:
Uwe Oestermeier 2023-07-20 11:44:13 +02:00
parent 1a0b9d55c1
commit 52ccb49fa8

View File

@ -106,6 +106,7 @@ class Throwable {
this.velocity = null this.velocity = null
this.timestamp = null this.timestamp = null
this.onThrowFinished = onThrowFinished this.onThrowFinished = onThrowFinished
this._requestAnimation = 0
//console.log("onThrowFinished", onThrowFinished) //console.log("onThrowFinished", onThrowFinished)
} }
@ -198,6 +199,56 @@ class Throwable {
this.onDragUpdate(d) this.onDragUpdate(d)
if (dt == 0 || this.needsAnimation()) { if (dt == 0 || this.needsAnimation()) {
if (this._requestAnimation != 0) {
console.log("cancelAnimationFrame")
cancelAnimationFrame(this._requestAnimation)
}
this._requestAnimation = requestAnimationFrame(this.animateThrow.bind(this))
return
} else {
if (this.isOutside()) {
if (this._requestAnimation != 0) {
console.log("cancelAnimationFrame")
cancelAnimationFrame(this._requestAnimation)
}
this._requestAnimation = requestAnimationFrame(this.animateThrow.bind(this))
return
}
}
}
this.onDragComplete()
if (this.onThrowFinished != null) {
this.onThrowFinished()
}
}
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)) requestAnimationFrame(this.animateThrow.bind(this))
return return
} else { } else {
@ -205,7 +256,7 @@ class Throwable {
requestAnimationFrame(this.animateThrow.bind(this)) requestAnimationFrame(this.animateThrow.bind(this))
return return
} }
} } */
} }
this.onDragComplete() this.onDragComplete()
if (this.onThrowFinished != null) { if (this.onThrowFinished != null) {