Added pluggable throw behavior.
This commit is contained in:
parent
47992755e2
commit
8b33bc0536
@ -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<buffer; i++) {
|
||||
velocity.t += dt
|
||||
this.velocities.push(velocity)
|
||||
}
|
||||
}
|
||||
|
||||
meanVelocity(milliseconds = 30) {
|
||||
this.addVelocity({ x: 0, y: 0, number: 1 })
|
||||
// this.addVelocity({ x: 0, y: 0, number: 1 })
|
||||
let sum = { x: 0, y: 0 }
|
||||
let count = 0
|
||||
let t = 0
|
||||
@ -181,6 +220,7 @@ class Throwable {
|
||||
|
||||
startThrow() {
|
||||
this.velocity = this.meanVelocity()
|
||||
console.log("startThrow", this.velocity)
|
||||
if (this.velocity != null) {
|
||||
// Call next velocity to ansure that specializations
|
||||
// that use keepOnStage are called
|
||||
@ -200,9 +240,6 @@ class Throwable {
|
||||
|
||||
recurseAnimateThrow() {
|
||||
ThrowableObjects.add(this)
|
||||
if (ThrowableRequests == null) {
|
||||
ThrowableRequests = startThrowableLoop()
|
||||
}
|
||||
}
|
||||
|
||||
animateThrow(time) {
|
||||
@ -223,11 +260,11 @@ class Throwable {
|
||||
|
||||
this.onDragUpdate(d)
|
||||
if (dt == 0 || this.needsAnimation()) {
|
||||
this.recurseAnimateThrow()
|
||||
thrower.animateThrow(this)
|
||||
return
|
||||
} else {
|
||||
if (this.isOutside()) {
|
||||
this.recurseAnimateThrow()
|
||||
thrower.animateThrow(this)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user