Added pluggable throw behavior.
This commit is contained in:
parent
47992755e2
commit
8b33bc0536
@ -89,22 +89,39 @@ export class ResizeEvent extends BaseEvent {
|
|||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let ThrowableObjects = new Set()
|
|
||||||
let ThrowableRequests = null
|
|
||||||
|
|
||||||
function stepThrowAnimation() {
|
class RequestFrameThrower {
|
||||||
let clones = [...ThrowableObjects]
|
/** Implemenents the standard throw behavior. */
|
||||||
ThrowableObjects.clear()
|
animateThrow(throwable) {
|
||||||
for(let obj of clones) {
|
/*** Calls the animateThrow method in sync with the display link. */
|
||||||
obj.animateThrow()
|
requestAnimationFrame(throwable.animateThrow.bind(throwable))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function startThrowableLoop() {
|
class TimeoutThrower {
|
||||||
setInterval(stepThrowAnimation, 33)
|
|
||||||
return true
|
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 {
|
class Throwable {
|
||||||
constructor({
|
constructor({
|
||||||
movableX = true,
|
movableX = true,
|
||||||
@ -127,6 +144,14 @@ class Throwable {
|
|||||||
//console.log("onThrowFinished", onThrowFinished)
|
//console.log("onThrowFinished", onThrowFinished)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static defaultThrow() {
|
||||||
|
thrower = new RequestFrameThrower()
|
||||||
|
}
|
||||||
|
|
||||||
|
static timeoutThrow() {
|
||||||
|
thrower = new TimeoutThrower()
|
||||||
|
}
|
||||||
|
|
||||||
observeVelocity() {
|
observeVelocity() {
|
||||||
this.lastframe = performance.now()
|
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) {
|
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 sum = { x: 0, y: 0 }
|
||||||
let count = 0
|
let count = 0
|
||||||
let t = 0
|
let t = 0
|
||||||
@ -181,6 +220,7 @@ class Throwable {
|
|||||||
|
|
||||||
startThrow() {
|
startThrow() {
|
||||||
this.velocity = this.meanVelocity()
|
this.velocity = this.meanVelocity()
|
||||||
|
console.log("startThrow", this.velocity)
|
||||||
if (this.velocity != null) {
|
if (this.velocity != null) {
|
||||||
// Call next velocity to ansure that specializations
|
// Call next velocity to ansure that specializations
|
||||||
// that use keepOnStage are called
|
// that use keepOnStage are called
|
||||||
@ -200,9 +240,6 @@ class Throwable {
|
|||||||
|
|
||||||
recurseAnimateThrow() {
|
recurseAnimateThrow() {
|
||||||
ThrowableObjects.add(this)
|
ThrowableObjects.add(this)
|
||||||
if (ThrowableRequests == null) {
|
|
||||||
ThrowableRequests = startThrowableLoop()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
animateThrow(time) {
|
animateThrow(time) {
|
||||||
@ -223,11 +260,11 @@ class Throwable {
|
|||||||
|
|
||||||
this.onDragUpdate(d)
|
this.onDragUpdate(d)
|
||||||
if (dt == 0 || this.needsAnimation()) {
|
if (dt == 0 || this.needsAnimation()) {
|
||||||
this.recurseAnimateThrow()
|
thrower.animateThrow(this)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if (this.isOutside()) {
|
if (this.isOutside()) {
|
||||||
this.recurseAnimateThrow()
|
thrower.animateThrow(this)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user