Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Observables mouse coordinates

Tags:

angular

I want to know how it's possible to get mouse coordinates X and Y using only Observables without http service using Angular 4.

like image 219
d0xzen Avatar asked Jul 11 '26 18:07

d0xzen


1 Answers

Http module is not a dependency for RxJS observables, so you don't have to use it. In fact, Http module itself uses RxJS to empower Http operands with reactive capabilities.

In order to subscribe to events like mousemove, you would simply import fromEvent and Observable and subscribe to its event emitter. Something like this should work:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';

Observable.fromEvent(document.body, 'mousemove').subscribe(e => {
  console.log(e.pageX, e.pageY)
})

Of course, use the appropriate event source (instead of document.body).

like image 172
dfsq Avatar answered Jul 13 '26 14:07

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!