@rcade/plugin-input-spinners
v0.2.2
Published
Input plugin for RCade's spinner controls (rotary encoders).
Readme
@rcade/plugin-input-spinners
Input plugin for RCade's spinner controls (rotary encoders).
Installation
npm install @rcade/plugin-input-spinnersUsage
Two patterns are available. Pick one, don't mix them.
Polling (recommended)
import { PLAYER_1, PLAYER_2 } from "@rcade/plugin-input-spinners";
function gameLoop() {
// Returns accumulated movement since last read, then resets to 0
const stepDelta = PLAYER_1.SPINNER.consume_step_delta();
paddleX += stepDelta * speed;
requestAnimationFrame(gameLoop);
}Angle Tracking
import { PLAYER_1 } from "@rcade/plugin-input-spinners";
function gameLoop() {
// Get cumulative angle in radians (automatically updated)
const angle = PLAYER_1.SPINNER.angle;
knob.rotation = angle;
requestAnimationFrame(gameLoop);
}
// Reset angle to 0 when needed
PLAYER_1.SPINNER.reset();Events
import { on } from "@rcade/plugin-input-spinners";
on("spin", ({ player, step_delta, step_resolution }) => {
console.log(`Player ${player} spun ${step_delta} steps`);
});API
PLAYER_1 / PLAYER_2
{
SPINNER: {
consume_step_delta(): number;
step_resolution: number;
angle: number;
reset(): void;
}
}consume_step_delta(): Returns accumulated movement since last call, then resets to 0.step_resolution: Encoder resolution (x steps per rotation).angle: Cumulative angle in radians (automatically updated).reset(): Resets the angle to 0.
STATUS
{
connected: boolean
}Events
on(event, callback)
Subscribe to spin events.
const unsubscribe = on("spin", (data) => {
// data: { player: 1 | 2, step_delta: number, step_resolution: number }
});
// Later: unsubscribe()off(event, callback)
Unsubscribe from spin events.
once(event, [filter], [callback])
Listen for a single spin event. Supports filtering by player and both callback and Promise styles.
// Promise style
const data = await once("spin");
// Promise with filter
const p1Data = await once("spin", { player: 1 });
// Callback style
const cancel = once("spin", (data) => { /* ... */ });Development
bun install