Dev HelpersOpen library
← Back

Normalized pointer coordinates

Import and render <NormalizedPointerCoordinates /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/NormalizedPointerCoordinates.tsx

import { useState } from "react";

export default function NormalizedPointerCoordinates({
  width = 12,
  height = 8,
}: { width?: number; height?: number } = {}) {
  const [xy, set] = useState([0, 0]);
  return (
    <div
      onPointerMove={(e) => {
        const r = e.currentTarget.getBoundingClientRect();
        set([(e.clientX - r.left) / r.width, (e.clientY - r.top) / r.height]);
      }}
      style={{ height: 120, border: "1px solid" }}
    >
      x {xy[0].toFixed(2)} · y {xy[1].toFixed(2)}
    </div>
  );
}