-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add inertia to drag-to-pan interaction (#240)
* feat: drag inertia PoC, still work in progress * feat: cancel inertia on mouse down event * chore: improvements * feat: improve the detection of mouse deceleration * chore: improve cooperation with picking, wheeling, etc * chore: adjust half life
- Loading branch information
Showing
8 changed files
with
274 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @template T | ||
*/ | ||
export default class RingBuffer { | ||
/** @type {T[]} */ | ||
#buffer; | ||
|
||
#index = 0; | ||
|
||
#length = 0; | ||
|
||
/** | ||
* @param {number} size | ||
*/ | ||
constructor(size) { | ||
this.#buffer = new Array(size); | ||
} | ||
|
||
/** @param {T} value */ | ||
push(value) { | ||
this.#buffer[this.#index] = value; | ||
this.#index = (this.#index + 1) % this.size; | ||
this.#length = Math.min(this.#length + 1, this.size); | ||
} | ||
|
||
/** | ||
* @returns {T[]} | ||
*/ | ||
get() { | ||
const b = this.#buffer; | ||
return this.#length < this.size | ||
? b.slice(0, this.#length) | ||
: b.slice(this.#index, this.size).concat(b.slice(0, this.#index)); | ||
} | ||
|
||
get size() { | ||
return this.#buffer.length; | ||
} | ||
|
||
get length() { | ||
return this.#length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import RingBuffer from "./ringBuffer.js"; | ||
|
||
describe("ringBuffer", () => { | ||
test("Empty buffer", () => { | ||
const buffer = new RingBuffer(10); | ||
expect(buffer.length).toBe(0); | ||
expect(buffer.get()).toEqual([]); | ||
}); | ||
|
||
test("Partially filled buffer", () => { | ||
const buffer = new RingBuffer(10); | ||
buffer.push(1); | ||
buffer.push(2); | ||
buffer.push(3); | ||
expect(buffer.length).toBe(3); | ||
expect(buffer.get()).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test("Full buffer", () => { | ||
const buffer = new RingBuffer(3); | ||
buffer.push(1); | ||
buffer.push(2); | ||
buffer.push(3); | ||
expect(buffer.length).toBe(3); | ||
expect(buffer.get()).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test("Overfilled buffer", () => { | ||
const buffer = new RingBuffer(3); | ||
buffer.push(1); | ||
buffer.push(2); | ||
buffer.push(3); | ||
buffer.push(4); | ||
buffer.push(5); | ||
expect(buffer.length).toBe(3); | ||
expect(buffer.get()).toEqual([3, 4, 5]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.