MousePosition

Reactive tracking of mouse position. Track cursor coordinates globally or relative to an element.

Loading demo...

Usage

angular-ts
import { Component } from '@angular/core';
import { mousePosition } from '@signality/core';

@Component({
  template: `
    <p>Mouse: {{ mousePos().x }}, {{ mousePos().y }}</p>
  `,
})
export class MousePositionDemo {
  readonly mousePos = mousePosition(); 
}

Options

OptionTypeDefaultDescription
targetMaybeElementSignal<Element> | WindowwindowElement or window to track mousePosition on
type'page' | 'client' | 'screen''page'Coordinate type
touchbooleantrueTrack touch events too
initialValue{ x: number, y: number }{ x: 0, y: 0 }Initial position
injectorInjector-Optional injector for DI context

Return Value

The mousePosition() function returns a Signal<MousePosition>:

typescript
interface MousePosition {
  x: number;
  y: number;
}
PropertyDescription
xX coordinate (depends on type: pageX, clientX, or screenX)
yY coordinate (depends on type: pageY, clientY, or screenY)

Examples

Cursor follower

angular-ts
import { Component } from '@angular/core';
import { mousePosition } from '@signality/core';

@Component({
  template: `
    <div
      class="cursor-follower"
      [style.left.px]="position().x - 20"
      [style.top.px]="position().y - 20"
    ></div>
  `,
  styles: `
    .cursor-follower {
      position: fixed;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background: rgba(147, 51, 234, 0.3);
      pointer-events: none;
      transition: transform 0.1s ease;
    }
  `,
})
export class CursorFollower {
  readonly position = mousePosition({ type: 'client' });
}

Track mousePosition on a specific element

angular-ts
import { Component, viewChild, ElementRef } from '@angular/core';
import { mousePosition } from '@signality/core';

@Component({
  template: `
    <div #area class="tracking-area">
      <p>Position: {{ position().x }}, {{ position().y }}</p>
    </div>
  `,
})
export class ElementMouseTracker {
  readonly area = viewChild<ElementRef>('area');
  readonly position = mousePosition({ target: this.area });
}

SSR Compatibility

On the server, the signal returns the initial value:

  • x0
  • y0

Type Definitions

typescript
interface MousePosition {
  readonly x: number;
  readonly y: number;
}

type MouseCoordinateType = 'page' | 'client' | 'screen';

interface MousePositionOptions extends WithInjector {
  readonly target?: MaybeElementSignal<Element> | Window;
  readonly type?: MouseCoordinateType;
  readonly touch?: boolean;
  readonly initialValue?: MousePosition;
}

function mousePosition(options?: MousePositionOptions): Signal<MousePosition>;
Edit this page on GitHub Last updated: Mar 19, 2026, 23:28:23