Created
July 5, 2019 14:21
-
-
Save carolina-vallejo/c0a55c0def4e1bc98204ceff36df62f6 to your computer and use it in GitHub Desktop.
mouse events binded rxjs.ts
This file contains hidden or 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 { Component, OnInit, ElementRef } from '@angular/core'; | |
import { fromEvent } from 'rxjs'; | |
import { skipUntil, takeUntil } from 'rxjs/operators'; | |
@Component({ | |
selector: 'app-board', | |
templateUrl: './board.component.html', | |
styleUrls: ['./board.component.scss'] | |
}) | |
export class BoardComponent implements OnInit { | |
private mousedown$; | |
private mouseup$; | |
private mouseMoveSub; | |
constructor(private elm: ElementRef) {} | |
ngOnInit() { | |
this.mousedown$ = fromEvent(this.elm.nativeElement, 'mousedown'); | |
this.mouseup$ = fromEvent(this.elm.nativeElement, 'mouseup'); | |
this.mousedown$.subscribe(_ => { | |
console.log('onMouse down'); | |
}); | |
this.mouseup$.subscribe(_ => { | |
console.log('onMouse up'); | |
this.register(); | |
}); | |
} | |
private register() { | |
if (this.mouseMoveSub) { | |
this.mouseMoveSub.unsubscribe(); | |
} | |
let mousemove$ = fromEvent(this.elm.nativeElement, 'mousemove'); | |
mousemove$ = mousemove$.pipe(skipUntil(this.mousedown$)); | |
mousemove$ = mousemove$.pipe(takeUntil(this.mouseup$)); | |
console.log('regsitered', mousemove$); | |
this.mouseMoveSub = mousemove$.subscribe(e => { | |
console.log('on mouse release'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment