npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-canvas-img

v1.1.6

Published

A lightweight React component to load images into a canvas and read pixel data (color and coordinates) on hover or click, with full TypeScript support.

Readme

react-canvas-img 🎨

npm version npm downloads license

A lightweight React component to load images into a <canvas> and read pixel data (color + coordinates) for all React events (click, hover, pointer, touch, drag, keyboard, focus, wheel, clipboard).
Fully written in TypeScript with support for refs, hooks, and callbacks.

✨ Features

  • Load images into <canvas> easily
  • Get pixel color + coordinates for mouse, pointer, touch, keyboard, focus, scroll, and clipboard events
  • Built-in onHoverPixel alias (fires on onMouseMove)
  • Toggle between canvas coordinates and original image coordinates
  • Exposes canvas context and element via onCanvasReady
  • Built-in zoom & pan (hold Shift + Scroll to zoom, Shift + Right Click + Drag to pan)
  • Supports refs for direct canvas access
  • Written in TypeScript – full type definitions included
  • Lightweight, no external dependencies

⚠️ Versioning Note

  • Versions with .stable (e.g., "1.1.5-stable") are official stable releases → recommended for production.
  • Other versions (without .stable) may be experimental or under development → not recommended for production.

📦 Installation

npm install react-canvas-img@stable
npm install react-canvas-img

# or
yarn add react-canvas-img@stable
yarn add react-canvas-img

🚀 Usage

import { useState } from "react";
import CanvasImage, { type PixelCoords, type PixelColor } from "react-canvas-img";

export default function App() {

  const canvasRef = useRef<HTMLCanvasElement | null>(null);
  const [clickedPixel, setClickedPixel] = useState<{
    coords: PixelCoords;
    color: PixelColor;
  } | null>(null);

  return (
    <CanvasImage
      src="/sample.png"
      width={400}     // optional → default: auto
      height={300}    // optional → default: auto
      useOriginalCoords={true} // optional → default: true
      style={{}}
      divClassName=""
      divStyle={{}}

      // Mouse
      onClickPixel={(coords, color) => setClickedPixel({ coords, color })}
      onHoverPixel={(coords, color) => console.log("Hover:", coords, color)}

      // Pointer
      onPointerMovePixel={(coords, color) => console.log("Pointer:", coords, color)}

      // Touch
      onTouchStartPixel={(coords, color) => console.log("Touch start:", coords, color)}

      // Lifecycle
      onCanvasReady={(ctx, canvas) => {
        canvasRef.current = canvas;
      }}
    />
  );
}

🎮 Demo Example

📚 API

Core Props

| Prop | Type | Default | Description | | ------------------- | -------------------------------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------- | | src | string \| File | — | Image source (URL or File) | | width | number | auto | Width of canvas (auto = natural width) | | height | number | auto | Height of canvas (auto = natural height) | | useOriginalCoords | boolean | true | If true, returns coordinates relative to original image size. If false, returns canvas coordinates | | onCanvasReady | (ctx: CanvasRenderingContext2D, canvas: HTMLCanvasElement) => void | — | Called when the canvas is initialized |

Event Props

Each event is available with the suffix Pixel, e.g. onClickPixel. They all share the same signature:

(coords: PixelCoords, color: PixelColor, event: React.SyntheticEvent<HTMLCanvasElement>) => void

🖱 Mouse Events

onClickPixel, onContextMenuPixel, onDoubleClickPixel, onDragPixel, onDragEndPixel, onDragEnterPixel, onDragExitPixel, onDragLeavePixel, onDragOverPixel, onDragStartPixel, onDropPixel, onMouseDownPixel, onMouseUpPixel, onMouseEnterPixel, onMouseLeavePixel, onMouseMovePixel, onHoverPixel (alias of onMouseMovePixel), onMouseOutPixel, onMouseOverPixel

⌨️ Keyboard Events (if focusable with tabIndex)

onKeyDownPixel, onKeyPressPixel, onKeyUpPixel

👀 Focus Events

onFocusPixel, onBlurPixel

✍️ Pointer Events (better than mouse for touch/pen)

onPointerDownPixel, onPointerMovePixel, onPointerUpPixel, onPointerCancelPixel, onGotPointerCapturePixel, onLostPointerCapturePixel, onPointerEnterPixel, onPointerLeavePixel, onPointerOverPixel, onPointerOutPixel

🤏 Touch Events

onTouchStartPixel, onTouchMovePixel, onTouchEndPixel, onTouchCancelPixel

🖱 Wheel & Scroll Events

onWheelPixel, onScrollPixel

📋 Clipboard Events (if focusable)

onCopyPixel, onCutPixel, onPastePixel

Ref (optional)

interface CanvasImageRef {
  getCanvas(): HTMLCanvasElement | null;
  getContext(): CanvasRenderingContext2D | null;
}

🧪 Development

git clone https://github.com/neerajrp1999/react-canvas-img.git
cd react-canvas-img
npm install
npm run build

📌 Roadmap

  • [ ] Add support for annotations
  • [ ] Performance optimizations for large images
  • [x] Enable zooming and panning

🤝 Contributing

PRs, issues, and suggestions are welcome! Please open an issue first to discuss changes.

📄 License

MIT © Neeraj Prajapati (malum)

🔗 Links