From 52ccb49fa8de8e2e95847d8547028bab8f07aef5 Mon Sep 17 00:00:00 2001 From: Uwe Oestermeier Date: Thu, 20 Jul 2023 11:44:13 +0200 Subject: [PATCH] Added cancelAnimationFrame --- lib/scatter.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/scatter.js b/lib/scatter.js index efaada1..61a6d3a 100644 --- a/lib/scatter.js +++ b/lib/scatter.js @@ -106,6 +106,7 @@ class Throwable { this.velocity = null this.timestamp = null this.onThrowFinished = onThrowFinished + this._requestAnimation = 0 //console.log("onThrowFinished", onThrowFinished) } @@ -198,6 +199,56 @@ class Throwable { this.onDragUpdate(d) 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)) return } else { @@ -205,7 +256,7 @@ class Throwable { requestAnimationFrame(this.animateThrow.bind(this)) return } - } + } */ } this.onDragComplete() if (this.onThrowFinished != null) {