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 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-konva-snapping

v2.1.1

Published

A lightweight React hook for adding snapping functionality to Konva.js elements. It allows objects to snap to stage borders, center, and other shapes, with customizable guidelines and snapping sensitivity.

Readme

use-konva-snapping

A lightweight React hook for adding snapping functionality to Konva.js elements. It enables elements to snap to stage borders, the center of the stage, and other shapes, with customizable guidelines and snapping sensitivity.


DEMO

Features

  • Snap to Stage Center: Snap elements to the center of the Konva stage.
  • Snap to Stage Borders: Snap elements to the edges of the stage.
  • Snap to Shapes: Snap elements to other shapes within the Konva stage.
  • Guidelines: Display visual guidelines when snapping, customizable in terms of color, thickness, and dash style.
  • Customizable Snap Range: Define how close elements need to be to another object for snapping to occur.
  • Supports Dragging & Resizing: Works with both dragging and resizing of Konva shapes.
  • Fully Configurable: Customize snapping behavior, guidelines, and more with simple options.

Installation

To install use-konva-snapping, you can use npm or yarn:

Using npm:

npm install use-konva-snapping

Using yarn:

yarn add use-konva-snapping

Usage

Once installed, you can use the useKonvaSnapping hook to enable snapping for Konva elements in your React app. Here's how to set it up:

Step 1: Import the Hook

Import the useKonvaSnapping hook into your React component.

import { useKonvaSnapping } from "use-konva-snapping";

Step 2: Use the Hook

Use the hook to get the snapping functionality and attach it to your Konva elements. Below is an example of a basic implementation where you can add draggable shapes and make them snap to the stage and other shapes.

"use client";

import React, { useRef, useState } from "react";

import {
  Stage,
  Layer,
  Rect,
  Circle,
  RegularPolygon,
  Transformer,
} from "react-konva";

import { useKonvaSnapping } from "use-konva-snapping";

const App = () => {
  const transformerRef = useRef(null); // Initialize transformerRef

  const [shapes, setShapes] = useState([
    {
      type: "rect",
      x: 60,
      y: 160,
      width: 100,
      height: 50,
      fill: "red",
      draggable: true,
    },

    {
      type: "rect",
      x: 260,
      y: 260,
      width: 100,
      height: 50,
      fill: "red",
      draggable: true,
    },
    {
      type: "circle",
      x: 200,
      y: 100,
      radius: 40,
      fill: "green",
      draggable: true,
    },

    {
      type: "triangle",
      x: 300,
      y: 150,
      radius: 50,
      sides: 3,
      fill: "blue",
      draggable: true,
    },
  ]);

  const { handleDragging, handleDragEnd, handleResizing, handleResizeEnd } =
    useKonvaSnapping({
      guidelineColor: "blue",
      guidelineDash: true,
      snapToStageCenter: true,
      snapRange: 5,
      guidelineThickness: 1,
      showGuidelines: true,
      snapToShapes: true,
      snapToStageBorders: true,
    });

  const handleSelect = (e) => {
    transformerRef.current.nodes([e.target]);
  };

  return (
    <Stage width={window.innerWidth} height={window.innerHeight}>
      <Layer>
        {shapes.map((shape, index) => {
          if (shape.type === "rect") {
            return (
              <Rect
                key={index}
                x={shape.x}
                y={shape.y}
                width={shape.width}
                height={shape.height}
                fill={shape.fill}
                draggable={shape.draggable}
                onDragMove={handleDragging}
                onDragEnd={handleDragEnd}
                onClick={handleSelect}
              />
            );
          }

          if (shape.type === "circle") {
            return (
              <Circle
                key={index}
                x={shape.x}
                y={shape.y}
                radius={shape.radius}
                fill={shape.fill}
                draggable={shape.draggable}
                onClick={handleSelect}
                onDragMove={handleDragging}
                onDragEnd={handleDragEnd}
              />
            );
          }

          if (shape.type === "triangle") {
            return (
              <RegularPolygon
                key={index}
                x={shape.x}
                y={shape.y}
                radius={shape.radius}
                sides={shape.sides}
                fill={shape.fill}
                draggable={shape.draggable}
                onClick={handleSelect}
                onDragMove={handleDragging}
                onDragEnd={handleDragEnd}
              />
            );
          }

          return null;
        })}

        <Transformer
          ref={transformerRef}
          onTransform={handleResizing}
          onTransformEnd={handleResizeEnd}
          keepRatio={false}
        />
      </Layer>
    </Stage>
  );
};
export default App;

Step 3: Customize Options (Optional)

useKonvaSnapping(params)

Parameters:

  • snapRange: (default: 3) Distance in pixels for snapping sensitivity.
  • guidelineColor: (default: "rgb(0, 161, 255)") Color of the snapping guidelines.
  • guidelineDash: (default: true) Whether the guidelines should be dashed.
  • showGuidelines: (default: true) Whether the guidelines should be displayed.
  • guidelineThickness: (default: 1) Thickness of the guidelines.
  • snapToStageCenter: (default: true) Whether to snap to the center of the stage.
  • snapToStageBorders: (default: true) Whether to snap to the edges of the stage.
  • snapToShapes: (default: true) Whether to snap to other shapes on the stage.

Methods Returned:

  • handleDragging: Attach this method to onDragMove of Konva elements.
  • handleResizing: Attach this method to onResize of Konva Transformer.
  • handleDragEnd: Attach this method to onDragEnd of Konva elements.
  • handleResizeEnd: Attach this method to onResizeEnd of Konva Transformer.

Author: Farid Methia
GitHub: faridmth
Email: [email protected]