Worker Pool
The Worker Pool offloads heavy computations from the main thread for smooth UI.
Overview
PinePaper uses Web Workers for:
- Poisson disk sampling
- Golden ratio distribution
- Path simplification
- Color calculations
- Scene serialization
Availability
if (app.workerPool.isAvailable()) {
// Workers ready
}
Async Methods
Poisson Disk Sampling
const points = await app.workerPool.poissonDiskSampling(
800, // width
600, // height
50 // minimum distance
);
// Returns: [{ x, y }, ...]
Golden Ratio Distribution
const points = await app.workerPool.goldenRatioDistribution(
100, // count
800, // width
600 // height
);
Path Simplification
const simplified = await app.workerPool.simplifyPath(
points, // array of {x, y}
5 // tolerance
);
Color Calculations
const colors = await app.workerPool.calculateColors(
positions, // array of {x, y}
palette, // color array
0.01 // noise scale
);
Scene Serialization
// Serialize (for saving)
const json = await app.workerPool.serializeScene(sceneData);
// Deserialize (for loading)
const scene = await app.workerPool.deserializeScene(jsonString);
Performance
Statistics
const stats = app.workerPool.getStats();
// {
// tasksCompleted: 42,
// tasksFailed: 0,
// averageTaskTime: 12.5,
// workersActive: 2
// }
Warm Up
Pre-initialize workers for faster first use:
await app.workerPool.warmUp();
Configuration
const pool = new WorkerPool({
maxWorkers: navigator.hardwareConcurrency || 4,
timeout: 30000 // 30 seconds
});
Fallback
If Workers are unavailable, methods fall back to main thread execution automatically.
Need more processing power? Web Workers help, but they’re still limited by your device’s hardware. For computationally intensive operations (large scenes, complex generators, batch processing), request server-side processing. We’re considering a paid API for heavy workloads based on user demand.
Best Practices
- Use for operations taking >16ms
- Batch multiple calculations when possible
- Use
warmUp()during app initialization - Check
isAvailable()before relying on workers