diff --git a/lib/scatter.js b/lib/scatter.js index 91cc2b3..599b64d 100644 --- a/lib/scatter.js +++ b/lib/scatter.js @@ -89,22 +89,39 @@ export class ResizeEvent extends BaseEvent { * @constructor */ -let ThrowableObjects = new Set() -let ThrowableRequests = null -function stepThrowAnimation() { - let clones = [...ThrowableObjects] - ThrowableObjects.clear() - for(let obj of clones) { - obj.animateThrow() +class RequestFrameThrower { + /** Implemenents the standard throw behavior. */ + animateThrow(throwable) { + /*** Calls the animateThrow method in sync with the display link. */ + requestAnimationFrame(throwable.animateThrow.bind(throwable)) } } -function startThrowableLoop() { - setInterval(stepThrowAnimation, 33) - return true +class TimeoutThrower { + + constructor(delay=20) { + this.throwables = new Set() + this.delay = delay + setTimeout(this.animateStep.bind(this), this.delay) + } + + animateThrow(throwable) { + this.throwables.add(throwable) + } + + animateStep() { + let active = [...this.throwables] + this.throwables.clear() + for(let throwable of active) { + throwable.animateThrow() + } + setTimeout(this.animateStep.bind(this), this.delay) + } } +let thrower = new RequestFrameThrower() + class Throwable { constructor({ movableX = true, @@ -127,6 +144,14 @@ class Throwable { //console.log("onThrowFinished", onThrowFinished) } + static defaultThrow() { + thrower = new RequestFrameThrower() + } + + static timeoutThrow() { + thrower = new TimeoutThrower() + } + observeVelocity() { this.lastframe = performance.now() } @@ -155,8 +180,22 @@ class Throwable { } } + addTestVelocity(delta, dt=20, buffer = 5) { + let t = performance.now() + let velocity = { + t: t, + dt: dt, + dx: delta.x , + dy: delta.y + } + for(let i=0; i