Created
July 10, 2023 06:43
-
-
Save mfp22/a4fa0131dffb0381a7ca8b72c4487e90 to your computer and use it in GitHub Desktop.
Reactive Dragon Blinking and Running
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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