114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
// Bouncing Ball preset for LEDLab
|
|
|
|
const BasePreset = require('./base-preset');
|
|
const { createFrame, toIndex } = require('./frame-utils');
|
|
|
|
class BouncingBallPreset extends BasePreset {
|
|
constructor(width = 16, height = 16) {
|
|
super(width, height);
|
|
this.position = 0;
|
|
this.velocity = 0;
|
|
this.defaultParameters = {
|
|
speed: 1.0,
|
|
ballSize: 1,
|
|
trailLength: 5,
|
|
color: 'ff8000',
|
|
brightness: 1.0,
|
|
};
|
|
}
|
|
|
|
init() {
|
|
super.init();
|
|
this.position = Math.random() * (this.width * this.height - 1);
|
|
this.velocity = this.randomVelocity();
|
|
}
|
|
|
|
randomVelocity() {
|
|
const min = 0.15;
|
|
const max = 0.4;
|
|
const sign = Math.random() < 0.5 ? -1 : 1;
|
|
return (min + Math.random() * (max - min)) * sign;
|
|
}
|
|
|
|
rebound(sign) {
|
|
this.velocity = this.randomVelocity() * sign;
|
|
}
|
|
|
|
mix(a, b, t) {
|
|
return a + (b - a) * t;
|
|
}
|
|
|
|
renderFrame() {
|
|
const frame = createFrame(this.width, this.height);
|
|
const speed = this.getParameter('speed') || 1.0;
|
|
const ballSize = this.getParameter('ballSize') || 1;
|
|
const trailLength = this.getParameter('trailLength') || 5;
|
|
const color = this.getParameter('color') || 'ff8000';
|
|
const brightness = this.getParameter('brightness') || 1.0;
|
|
|
|
const dt = 0.016; // Assume 60 FPS
|
|
this.position += this.velocity * speed * dt * 60;
|
|
|
|
// Handle boundary collisions
|
|
if (this.position < 0) {
|
|
this.position = -this.position;
|
|
this.rebound(1);
|
|
} else if (this.position > this.width * this.height - 1) {
|
|
this.position = (this.width * this.height - 1) - (this.position - (this.width * this.height - 1));
|
|
this.rebound(-1);
|
|
}
|
|
|
|
const activeIndex = Math.max(0, Math.min(this.width * this.height - 1, Math.round(this.position)));
|
|
|
|
// Render ball and trail
|
|
for (let i = 0; i < frame.length; i++) {
|
|
if (i === activeIndex) {
|
|
// Main ball
|
|
frame[i] = color;
|
|
} else {
|
|
// Trail effect
|
|
const distance = Math.abs(i - this.position);
|
|
if (distance <= trailLength) {
|
|
const intensity = Math.max(0, 1 - distance / trailLength);
|
|
const trailColor = this.hexToRgb(color);
|
|
|
|
const r = Math.round(this.mix(0, trailColor.r, intensity * brightness));
|
|
const g = Math.round(this.mix(20, trailColor.g, intensity * brightness));
|
|
const b = Math.round(this.mix(40, trailColor.b, intensity * brightness));
|
|
|
|
frame[i] = r.toString(16).padStart(2, '0') +
|
|
g.toString(16).padStart(2, '0') +
|
|
b.toString(16).padStart(2, '0');
|
|
}
|
|
}
|
|
}
|
|
|
|
return frame;
|
|
}
|
|
|
|
hexToRgb(hex) {
|
|
const r = parseInt(hex.slice(0, 2), 16);
|
|
const g = parseInt(hex.slice(2, 4), 16);
|
|
const b = parseInt(hex.slice(4, 6), 16);
|
|
return { r, g, b };
|
|
}
|
|
|
|
getMetadata() {
|
|
return {
|
|
name: 'Bouncing Ball',
|
|
description: 'A ball that bounces around the matrix with a trailing effect',
|
|
parameters: {
|
|
speed: { type: 'range', min: 0.1, max: 3.0, step: 0.1, default: 1.0 },
|
|
ballSize: { type: 'range', min: 1, max: 5, step: 1, default: 1 },
|
|
trailLength: { type: 'range', min: 1, max: 10, step: 1, default: 5 },
|
|
color: { type: 'color', default: 'ff8000' },
|
|
brightness: { type: 'range', min: 0.1, max: 1.0, step: 0.1, default: 1.0 },
|
|
},
|
|
width: this.width,
|
|
height: this.height,
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = BouncingBallPreset;
|