sequencer.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { TerminalTyper } from './terminal-typer';
  2. import Timer = NodeJS.Timer;
  3. export class Sequencer {
  4. private readonly playTimer: { [name: string]: any; };
  5. private readonly onTransition?: (className: string) => void;
  6. constructor(private sceneElement: HTMLDivElement,
  7. private terminal: TerminalTyper,
  8. onTransitionFn?: (className: string) => void) {
  9. this.sceneElement = sceneElement;
  10. this.playTimer = {};
  11. this.onTransition = onTransitionFn;
  12. }
  13. async play() {
  14. Object.values(this.playTimer).forEach(k => clearTimeout(k));
  15. await this.sleep('initial', 0);
  16. this.scene0();
  17. await this.sleep('start-typing', 1000);
  18. this.scene1();
  19. await this.sleep('zoom-out', 1500);
  20. this.scene2();
  21. await this.sleep('data-flow', 1200);
  22. this.scene3();
  23. await this.sleep('websites', 1000);
  24. this.scene4();
  25. await this.sleep('logo', 2000);
  26. this.scene5();
  27. await this.sleep('final', 1000);
  28. this.scene6();
  29. }
  30. jumpTo(scene: 0 | 1 | 2 | 3 | 4 | 5 | 6) {
  31. Object.values(this.playTimer).forEach(k => clearTimeout(k));
  32. (this as any)['scene' + scene]();
  33. }
  34. private scene0() {
  35. this.setScene('scene-0');
  36. this.terminal.clear();
  37. }
  38. private scene1() {
  39. this.setScene('scene-1');
  40. this.terminal.start();
  41. }
  42. private scene2() {
  43. this.setScene('scene-2');
  44. this.terminal.fill();
  45. }
  46. private scene3() {
  47. this.setScene('scene-3');
  48. this.terminal.fill();
  49. }
  50. private scene4() {
  51. this.setScene('scene-4');
  52. this.terminal.fill();
  53. }
  54. private scene5() {
  55. this.setScene('scene-5');
  56. this.terminal.fill();
  57. }
  58. private scene6() {
  59. this.setScene('scene-6');
  60. this.terminal.fill();
  61. }
  62. private setScene(className: string) {
  63. this.sceneElement.classList.value = `visible scene ${className}`;
  64. if (typeof this.onTransition === 'function') {
  65. this.onTransition(className);
  66. }
  67. }
  68. private sleep(id: string, duration: number) {
  69. clearTimeout(this.playTimer[id]);
  70. return new Promise(resolve => {
  71. this.playTimer[id] = setTimeout(resolve, duration);
  72. });
  73. }
  74. }