@ginden/blinkstick-v2
Version:
Improved Blickstick API for Node.js
123 lines • 4.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnimationBuilder = void 0;
const simple_frame_1 = require("./frame/simple-frame");
const utils_1 = require("../utils");
const pulse_1 = require("./common/pulse");
const combine_1 = require("./helpers/combine");
const wait_frame_1 = require("./frame/wait-frame");
const node_util_1 = require("node:util");
const wrap_generator_function_in_object_1 = require("./helpers/wrap-generator-function-in-object");
const morph_complex_1 = require("./common/morph-complex");
const assert_fps_below_100_1 = require("./helpers/assert-fps-below-100");
const repeat_1 = require("./helpers/repeat");
const transform_each_frame_1 = require("./helpers/transform-each-frame");
const tsafe_1 = require("tsafe");
/**
* Our primary class for building complex animations.
* You SHOULD use static methods to create an instance of this class,
* but you are an adult. You can extend this class and use it as you want.
* @category Animation
*/
class AnimationBuilder {
constructor() {
this.subAnimations = [];
}
static startWithBlack(ms) {
const builder = new AnimationBuilder();
return builder.addStaticFrame(new simple_frame_1.SimpleFrame([0, 0, 0], ms));
}
static startWithColor(color, ms) {
const builder = new AnimationBuilder();
return builder.addStaticFrame(new simple_frame_1.SimpleFrame((0, utils_1.colorInputToRgbTuple)(color), ms));
}
assertNoGenerators() {
for (const [i, animation] of this.subAnimations.entries()) {
if (node_util_1.types.isGeneratorObject(animation)) {
throw new Error(`This operation is not supported, because animation ${i} is a generator. Wrap generator function through ${wrap_generator_function_in_object_1.wrapGeneratorForAnimation.name} function`);
}
}
}
/**
* Forks the current animation builder, allowing to create a new animation
* that is a copy of the current one.
* If any of the animations are generators, an error will be thrown.
*/
fork() {
this.assertNoGenerators();
const builder = new AnimationBuilder();
builder.subAnimations = [...this.subAnimations];
return builder;
}
/*
* Adds a static frame to the animation.
*/
stillColor(color, ms) {
return this.addStaticFrame(new simple_frame_1.SimpleFrame((0, utils_1.colorInputToRgbTuple)(color), ms));
}
addStaticFrame(simpleFrame) {
this.subAnimations.push([simpleFrame]);
return this;
}
addPulse(color, overMs, steps = overMs / 60) {
(0, assert_fps_below_100_1.assertFpsBelow100)(overMs, steps);
this.subAnimations.push((0, pulse_1.pulse)((0, utils_1.colorInputToRgbTuple)(color), { steps, overMs }));
return this;
}
/**
* Appends a new animation to the current one.
* @param animation
*/
append(animation) {
this.subAnimations.push(animation);
return this;
}
/**
* Appends a new animation to the current one, but provides smooth transition between last frame of the current animation
* and first frame of the new one.
*/
smoothTransitionToAnimation(animation, ms, steps = ms / 60) {
this.assertNoGenerators();
this.subAnimations = [(0, morph_complex_1.morphComplex)((0, combine_1.combine)(...this.subAnimations), animation, ms, steps)];
return this;
}
/**
* Morphs the current animation to the given color.
* @param color
* @param ms
*/
morphToColor(color, ms) {
this.assertNoGenerators();
this.subAnimations = [
(0, morph_complex_1.morphComplex)((0, combine_1.combine)(...this.subAnimations), [simple_frame_1.SimpleFrame.colorAndDuration((0, utils_1.colorInputToRgbTuple)(color), ms)], ms),
];
return this;
}
/**
* Waits for the given time, retaining current color and state.
* @param ms
*/
wait(ms) {
this.subAnimations.push([new wait_frame_1.WaitFrame(ms)]);
}
/**
* Repeats the current animation the given number of times.
* @param times
*/
repeat(times) {
this.assertNoGenerators();
this.subAnimations = [(0, repeat_1.repeat)((0, combine_1.combine)(...this.subAnimations), times)];
return this;
}
transformEachFrame(transform) {
this.assertNoGenerators();
this.subAnimations = [(0, transform_each_frame_1.transformEachFrame)((0, combine_1.combine)(...this.subAnimations), transform)];
return this;
}
build() {
(0, tsafe_1.assert)(this.subAnimations.length > 0, 'No animations to build');
return (0, combine_1.combine)(...this.subAnimations);
}
}
exports.AnimationBuilder = AnimationBuilder;
//# sourceMappingURL=animation-builder.js.map