feat: even more matrix stream examples
This commit is contained in:
165
test/pixelstream/voxel-fireflies.js
Normal file
165
test/pixelstream/voxel-fireflies.js
Normal file
@@ -0,0 +1,165 @@
|
||||
const dgram = require('dgram');
|
||||
|
||||
const {
|
||||
addHexColor,
|
||||
clamp,
|
||||
createFrame,
|
||||
fadeFrame,
|
||||
frameToPayload,
|
||||
hexToRgb,
|
||||
samplePalette,
|
||||
toIndex,
|
||||
} = require('./shared-frame-utils');
|
||||
|
||||
const DEFAULT_PORT = 4210;
|
||||
const DEFAULT_WIDTH = 16;
|
||||
const DEFAULT_HEIGHT = 16;
|
||||
const DEFAULT_INTERVAL_MS = 55;
|
||||
const DEFAULT_FIREFLY_COUNT = 18;
|
||||
const HOVER_SPEED = 0.6;
|
||||
const GLOW_SPEED = 1.8;
|
||||
const TRAIL_DECAY = 0.8;
|
||||
|
||||
const paletteStops = [
|
||||
{ stop: 0.0, color: hexToRgb('02030a') },
|
||||
{ stop: 0.2, color: hexToRgb('031c2d') },
|
||||
{ stop: 0.4, color: hexToRgb('053d4a') },
|
||||
{ stop: 0.6, color: hexToRgb('107b68') },
|
||||
{ stop: 0.8, color: hexToRgb('14c491') },
|
||||
{ stop: 1.0, color: hexToRgb('f2ffd2') },
|
||||
];
|
||||
|
||||
const host = process.argv[2];
|
||||
const port = parseInt(process.argv[3] || String(DEFAULT_PORT), 10);
|
||||
const width = parseInt(process.argv[4] || String(DEFAULT_WIDTH), 10);
|
||||
const height = parseInt(process.argv[5] || String(DEFAULT_HEIGHT), 10);
|
||||
const intervalMs = parseInt(process.argv[6] || String(DEFAULT_INTERVAL_MS), 10);
|
||||
const fireflyCount = parseInt(process.argv[7] || String(DEFAULT_FIREFLY_COUNT), 10);
|
||||
|
||||
if (!host) {
|
||||
console.error('Usage: node voxel-fireflies.js <device-ip> [port] [width] [height] [interval-ms] [firefly-count]');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (Number.isNaN(port) || Number.isNaN(width) || Number.isNaN(height) || Number.isNaN(intervalMs) || Number.isNaN(fireflyCount)) {
|
||||
console.error('Invalid numeric argument. Expected integers for port, width, height, interval-ms, and firefly-count.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (width <= 0 || height <= 0) {
|
||||
console.error('Matrix dimensions must be positive integers.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (fireflyCount <= 0) {
|
||||
console.error('Firefly count must be a positive integer.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const socket = dgram.createSocket('udp4');
|
||||
const isBroadcast = host === '255.255.255.255' || host.endsWith('.255');
|
||||
const frame = createFrame(width, height);
|
||||
const fireflies = createFireflies(fireflyCount, width, height);
|
||||
const frameTimeSeconds = intervalMs / 1000;
|
||||
|
||||
if (isBroadcast) {
|
||||
socket.bind(() => {
|
||||
socket.setBroadcast(true);
|
||||
});
|
||||
}
|
||||
|
||||
socket.on('error', (error) => {
|
||||
console.error('Socket error:', error.message);
|
||||
});
|
||||
|
||||
function createFireflies(count, matrixWidth, matrixHeight) {
|
||||
const list = [];
|
||||
for (let index = 0; index < count; ++index) {
|
||||
list.push(spawnFirefly(matrixWidth, matrixHeight));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
function spawnFirefly(matrixWidth, matrixHeight) {
|
||||
return {
|
||||
x: Math.random() * (matrixWidth - 1),
|
||||
y: Math.random() * (matrixHeight - 1),
|
||||
targetX: Math.random() * (matrixWidth - 1),
|
||||
targetY: Math.random() * (matrixHeight - 1),
|
||||
glowPhase: Math.random() * Math.PI * 2,
|
||||
dwell: 1 + Math.random() * 2,
|
||||
};
|
||||
}
|
||||
|
||||
function updateFirefly(firefly, deltaSeconds) {
|
||||
const dx = firefly.targetX - firefly.x;
|
||||
const dy = firefly.targetY - firefly.y;
|
||||
const distance = Math.hypot(dx, dy);
|
||||
|
||||
if (distance < 0.2) {
|
||||
firefly.dwell -= deltaSeconds;
|
||||
if (firefly.dwell <= 0) {
|
||||
firefly.targetX = Math.random() * (width - 1);
|
||||
firefly.targetY = Math.random() * (height - 1);
|
||||
firefly.dwell = 1 + Math.random() * 2;
|
||||
}
|
||||
} else {
|
||||
const speed = HOVER_SPEED * (0.8 + Math.random() * 0.4);
|
||||
firefly.x += (dx / distance) * speed * deltaSeconds;
|
||||
firefly.y += (dy / distance) * speed * deltaSeconds;
|
||||
}
|
||||
|
||||
firefly.glowPhase += deltaSeconds * GLOW_SPEED * (0.7 + Math.random() * 0.6);
|
||||
}
|
||||
|
||||
function drawFirefly(firefly) {
|
||||
const baseGlow = (Math.sin(firefly.glowPhase) + 1) * 0.5;
|
||||
const col = Math.round(firefly.x);
|
||||
const row = Math.round(firefly.y);
|
||||
|
||||
for (let dy = -1; dy <= 1; ++dy) {
|
||||
for (let dx = -1; dx <= 1; ++dx) {
|
||||
const sampleX = col + dx;
|
||||
const sampleY = row + dy;
|
||||
if (sampleX < 0 || sampleX >= width || sampleY < 0 || sampleY >= height) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const distance = Math.hypot(dx, dy);
|
||||
const falloff = clamp(1 - distance * 0.7, 0, 1);
|
||||
const intensity = baseGlow * falloff;
|
||||
if (intensity <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
frame[toIndex(sampleX, sampleY, width)] = samplePalette(paletteStops, intensity);
|
||||
if (distance === 0) {
|
||||
addHexColor(frame, toIndex(sampleX, sampleY, width), 'ffd966', intensity * 1.6);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function generateFrame() {
|
||||
fadeFrame(frame, TRAIL_DECAY);
|
||||
|
||||
fireflies.forEach((firefly) => {
|
||||
updateFirefly(firefly, frameTimeSeconds);
|
||||
drawFirefly(firefly);
|
||||
});
|
||||
|
||||
return frameToPayload(frame);
|
||||
}
|
||||
|
||||
function sendFrame() {
|
||||
const payload = generateFrame();
|
||||
const message = Buffer.from(payload, 'utf8');
|
||||
socket.send(message, port, host);
|
||||
}
|
||||
|
||||
setInterval(sendFrame, intervalMs);
|
||||
|
||||
console.log(
|
||||
`Streaming voxel fireflies to ${host}:${port} (${width}x${height}, interval=${intervalMs}ms, fireflies=${fireflyCount})`
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user