Skip to content

Commit

Permalink
fix: don't forget to remove ScrollFling's and FlingRotation's ren…
Browse files Browse the repository at this point in the history
…der tasks (i.e. terminate any

animations) on `stop()`.
  • Loading branch information
trusktr committed Jun 6, 2021
1 parent 8a1b4b7 commit ded9fe2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/lume/src/interaction/FlingRotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type FlingRotationOptions = Pick<FlingRotation, 'rotationYTarget'> &
>

export class FlingRotation {
/** The object that will be rotated on Y. */
/** The object that will be rotated on Y. Required. */
readonly rotationYTarget!: Node

/**
Expand Down Expand Up @@ -49,14 +49,7 @@ export class FlingRotation {
*/
readonly interactionContainer: Document | ShadowRoot | Element = document

constructor(
// {
// rotationYTarget,
// rotationXTarget = rotationYTarget.children[0] as Node,
// interactionInitiator = rotationXTarget,
// }
options: FlingRotationOptions,
) {
constructor(options: FlingRotationOptions) {
Object.assign(this, options)

// FlingRotation is dependent on tree structure (unless otherwise
Expand All @@ -70,20 +63,21 @@ export class FlingRotation {

#onPointerDown = () => {
// Stop rotation if any.
this.rotationXTarget.rotation = () => false
this.rotationYTarget.rotation = () => false

let deltaX = 0
let deltaY = 0

this.#onMove = (event: PointerEvent) => {
deltaX = event.movementY * 0.2
this.rotationXTarget.getRotation().x = clamp(
this.rotationXTarget.getRotation().x + deltaX,
this.rotationXTarget.rotation.x = clamp(
this.rotationXTarget.rotation.x + deltaX,
this.minFlingRotationX,
this.maxFlingRotationX,
)
deltaY = -event.movementX * 0.2
this.rotationYTarget.getRotation().y += deltaY
this.rotationYTarget.rotation.y += deltaY
}

// @ts-ignore, whyyyy TypeScript TODO fix TypeScript lib.dom types.
Expand Down Expand Up @@ -149,13 +143,17 @@ export class FlingRotation {
if (!this.#isStarted) return this
this.#isStarted = false

// Stop any current animation.
this.rotationXTarget.rotation = () => false
this.rotationYTarget.rotation = () => false

this.interactionInitiator.removeEventListener('pointerdown', this.#onPointerDown)
// @ts-ignore, whyyyy TypeScript TODO fix TypeScript lib.dom types.
this.interactionInitiator.addEventListener('dragstart', this.#onDragStart)
this.interactionInitiator.removeEventListener('dragstart', this.#onDragStart)
// @ts-ignore, whyyyy TypeScript TODO fix TypeScript lib.dom types.
if (this.#onMove) this.interactionContainer.removeEventListener('pointermove', this.#onMove)
// @ts-ignore, whyyyy TypeScript TODO fix TypeScript lib.dom types.
if (this.#onPointerUp) this.interactionContainer.addEventListener('pointerup', this.#onPointerUp)
if (this.#onPointerUp) this.interactionContainer.removeEventListener('pointerup', this.#onPointerUp)

return this
}
Expand Down
3 changes: 3 additions & 0 deletions packages/lume/src/interaction/ScrollFling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export class ScrollFling {
if (!this.#isStarted) return this
this.#isStarted = false

// Stop any current animation, if any.
if (this.#task) Motor.removeRenderTask(this.#task)

// @ts-ignore, whyyyyy TypeScript
this.target.removeEventListener('wheel', this.#onWheel)

Expand Down

0 comments on commit ded9fe2

Please sign in to comment.