A Svelte 5 library for handling TUIO (Tangible User Interface Objects) protocol events through WebSocket connections. Built with runes and reactive state management for tracking tangible objects on interactive surfaces.
useTUIO().This library is designed to receive TUIO messages from a TUIO server (such as TouchDesigner) and make them available in your Svelte web frontend.
TUIOProvider that connects to your TUIO server{
"timestamp": 123456789,
"touchesStart": [...],
"touchesMove": [...],
"touchesEnd": [...],
"touchesNoChange": [...]
}
npm install svelte-tuio
The simplest way to use the library is with TUIOProvider, which automatically creates a TUIOHandler for you:
<script>
import { TUIOProvider } from 'svelte-tuio';
import { SvelteSocket } from '@hardingjam/svelte-socket';
let { children } = $props();
// Create WebSocket connection to your TUIO server
// Replace with your TouchDesigner WebSocket server address
const svelteSocket = new SvelteSocket('ws://localhost:8080');
</script>
<TUIOProvider {svelteSocket}>
<!-- Your app components can now access TUIO data via useTUIO() -->
{@render children?.()}
</TUIOProvider>
Custom Handler Configuration:
You can also create your own TUIOHandler with custom callbacks and pass it to the provider:
<script>
import { TUIOProvider, TUIOHandler } from 'svelte-tuio';
import { SvelteSocket } from '@hardingjam/svelte-socket';
let { children } = $props();
const svelteSocket = new SvelteSocket('ws://localhost:8080');
const tuioHandler = new TUIOHandler({
svelteSocket,
onFingerTouchEnd: (u, v) => {
console.log(`Touch at ${u}, ${v}`);
},
onPlaceTangible: (touch) => {
console.log(`Tangible ${touch.classId} placed`);
}
});
</script>
<TUIOProvider {tuioHandler}>
<!-- Your app components can now access TUIO data via useTUIO() -->
{@render children?.()}
</TUIOProvider>
Your TouchDesigner project needs to run a WebSocket server that sends TUIO events in the JSON format shown above.
Note: An example Python callbacks file for TouchDesigner is included in this package at
src/python/example_tuio_callbacks.py
Access reactive TUIO state in your components using $derived:
<script>
import { useTUIO } from 'svelte-tuio';
const tuioHandler = useTUIO();
// Access reactive state with $derived
let tangibles = $derived(tuioHandler.tangiblesManager.tangibles);
let tangibleClassIds = $derived(tuioHandler.tangiblesManager.tangibleClassIds);
let isConnected = $derived(tuioHandler.svelteSocket.isConnected);
// Derived computations
let tangibleCount = $derived(tangibles.length);
let hasTangibles = $derived(tangibleCount > 0);
</script>
<div>
<p>Connected: {isConnected}</p>
<p>Active tangibles: {tangibleCount}</p>
{#if hasTangibles}
{#each tangibles as tangible (tangible.classId)}
<div>
Tangible {tangible.classId} at ({tangible.u.toFixed(2)}, {tangible.v.toFixed(2)})
</div>
{/each}
{:else}
<p>No tangibles detected</p>
{/if}
</div>
<script>
import { TangiblesDebugger } from 'svelte-tuio';
</script>
<TangiblesDebugger />
TUIOProviderWrapper component that sets up TUIO context for your app.
Props:
svelteSocket - SvelteSocket instance from @hardingjam/svelte-socket (automatically creates handler)tuioHandler - Optional pre-configured TUIOHandler instanceTUIOHandlerMain class for managing TUIO WebSocket connections.
import type { TUIOHandlerConfig } from 'svelte-tuio';
import { SvelteSocket } from '@hardingjam/svelte-socket';
const svelteSocket = new SvelteSocket('ws://localhost:8080');
const config: TUIOHandlerConfig = {
svelteSocket,
onFingerTouchEnd: (u, v) => {
/* ... */
},
onFingerTouchStart: (u, v) => {
/* ... */
},
onPlaceTangible: (touch) => {
/* ... */
},
onRemoveTangible: (touch) => {
/* ... */
},
onMoveTangible: (touch) => {
/* ... */
},
debounceTime: 100 // Optional: throttle callbacks to once per 100ms
};
const handler = new TUIOHandler(config);
// Properties
handler.tangiblesManager; // TangiblesManager instance
handler.touchZones; // Array of touch zones ($state)
handler.svelteSocket; // SvelteSocket instance
Touch zones allow you to define regions of the screen and attach callbacks for touch/tangible events within those regions. Touch zones are user-managed - you register zones and implement your own logic to check if touches fall within zones.
import type { TouchZone } from 'svelte-tuio';
const tuioHandler = useTUIO();
// Define a touch zone
const zone: TouchZone = {
id: 'my-zone',
u: 0.1, // Left edge (normalized 0-1)
v: 0.1, // Bottom edge (normalized 0-1)
normalisedWidth: 0.3,
normalisedHeight: 0.3,
onPlaceTangible: (touch) => {
console.log('Tangible placed in zone', touch);
},
onRemoveTangible: (touch) => {
console.log('Tangible removed from zone', touch);
},
onMoveTangible: (touch) => {
console.log('Tangible moved in zone', touch);
}
};
// Register the zone
tuioHandler.registerTouchZone(zone);
// Remove a zone when done
tuioHandler.unregisterTouchZone('my-zone');
// You must implement your own logic to check if touches are within zones
// and call the appropriate zone callbacks
Note: The library provides touch zone registration and storage, but you are responsible for implementing the hit detection logic and calling zone callbacks.
TangiblesManagerManages tangible objects with reactive state. All tangible operations are handled internally by TUIOHandler.
// Public Properties (reactive $state - use with $derived)
manager.tangibles; // TUIOTouch[] - All active tangibles with full data
manager.tangibleClassIds; // number[] - Just the class IDs for performance
Example: Accessing Reactive State
<script>
import { useTUIO } from 'svelte-tuio';
const tuioHandler = useTUIO();
const manager = tuioHandler.tangiblesManager;
// Use $derived to reactively access state
let tangibles = $derived(manager.tangibles);
let classIds = $derived(manager.tangibleClassIds);
// Derived computations update automatically
let specificTangible = $derived(tangibles.find((t) => t.classId === 14));
let hasSpecificTangible = $derived(classIds.includes(14));
</script>
{#if specificTangible}
<p>Tangible 14 is at position: {specificTangible.u}, {specificTangible.v}</p>
{/if}
useTUIO()Hook to access TUIOHandler from context.
const tuioHandler = useTUIO();
The library provides optional callbacks for all TUIO events. By default:
onFingerTouchEnd - Uses defaultSimulateClick to click HTML elementsonFingerTouchStart - Does nothing (no-op)onPlaceTangible, onRemoveTangible, onMoveTangible) - Automatically update TangiblesManager state, custom callbacks are called in addition to state updatesCustom Callbacks:
You can provide custom callbacks for any TUIO event:
<script>
import { TUIOProvider, TUIOHandler } from 'svelte-tuio';
import { SvelteSocket } from '@hardingjam/svelte-socket';
const svelteSocket = new SvelteSocket('ws://localhost:8080');
const tuioHandler = new TUIOHandler({
svelteSocket,
onFingerTouchEnd: (u, v) => {
console.log(`Finger touch end at: ${u}, ${v}`);
},
onFingerTouchStart: (u, v) => {
console.log(`Finger touch start at: ${u}, ${v}`);
},
onPlaceTangible: (touch) => {
console.log(`Tangible ${touch.classId} placed`);
},
onRemoveTangible: (touch) => {
console.log(`Tangible ${touch.classId} removed`);
},
onMoveTangible: (touch) => {
console.log(`Tangible ${touch.classId} moved to ${touch.u}, ${touch.v}`);
}
});
</script>
<TUIOProvider {tuioHandler}>
<!-- Your app -->
</TUIOProvider>
Note: Custom tangible callbacks are called in addition to the automatic TangiblesManager updates, allowing you to add extra functionality without losing the built-in state management.
For performance optimization, especially with high-frequency touch events, you can throttle callback invocations using the debounceTime option:
const tuioHandler = new TUIOHandler({
svelteSocket,
debounceTime: 100, // Minimum 100ms between callback invocations
onMoveTangible: (touch) => {
// This will only be called at most once per 100ms per tangible
console.log(`Tangible ${touch.classId} moved`);
}
});
How it works:
debounceTime sets the minimum time (in milliseconds) between callback invocations0 (default) to disable throttlingUse cases:
onMoveTangible to reduce expensive operations during draggingFull TypeScript support with exported types:
import type { TUIOTouch, TUIOEvent, TouchZone, TUIOHandlerConfig } from 'svelte-tuio';
npm install
npm run dev
npm run test
npm run build
MIT