Skip to content

Instantly share code, notes, and snippets.

@mfp22
Created July 10, 2023 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfp22/a4fa0131dffb0381a7ca8b72c4487e90 to your computer and use it in GitHub Desktop.
Save mfp22/a4fa0131dffb0381a7ca8b72c4487e90 to your computer and use it in GitHub Desktop.
Reactive Dragon Blinking and Running
import { CommonModule, NgClass } from '@angular/common';
import { Component } from '@angular/core';
import { UntilDestroy } from '@ngneat/until-destroy';
import { concat, timer } from 'rxjs';
import { delay, map, repeat, startWith, takeWhile, tap } from 'rxjs/operators';
@UntilDestroy()
@Component({
selector: 't-logo',
standalone: true,
imports: [NgClass, CommonModule],
templateUrl: './logo.component.html',
styleUrls: ['./logo.component.scss']
})
export class LogoComponent {
blinkingEyesClass$ = timer(0, 500).pipe(
startWith(0),
map((x) => x + 1),
takeWhile((x) => x < 6),
map((x) => `l${x % 2 === 0 ? 1 : 2}`)
);
runningClass$ = timer(0, 100).pipe(
startWith(0),
map((x) => x + 1),
takeWhile((x) => x <= 40),
map((x) => {
const range = Math.ceil(x / 10);
const side = range % 2 === 0 ? 'l' : 'r';
const runningLegState = x % 2 === 0 ? 3 : 4;
const legState = x === 40 ? 1 : runningLegState;
return `${side}${legState}`;
})
);
dragonNgClass$ = concat(this.blinkingEyesClass$, this.runningClass$).pipe(
delay(5000),
repeat(1000),
map((className) => ['bg dragon', className])
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment