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

slider-captcha-js

v1.0.8

Published

A lightweight slider captcha component for JavaScript and React.

Readme

🎯 Slider Captcha Library

npm version npm downloads License: MIT

A lightweight, customizable slider captcha for JavaScript, React, and TypeScript.
Supports UMD + ESM builds, themes, async verification, and server integration.
Perfect for adding a secure, interactive human verification step to your apps.


🌐 Demo

A live demo is available on GitHub Pages:
https://amazingdevteam.github.io/slider-captcha-js


✨ Features

  • ⚡ Works in Vanilla JS, React, and TypeScript
  • 📦 UMD + ESM builds
  • 🎨 Customizable size, theme (light / dark), and image source
  • 🔄 Built-in refresh button with hover effects
  • 🔒 No backend required, but supports async onVerify and request() for server integration
  • 🪶 Lightweight and dependency-free
  • 🖼️ Canvas fallback with gradient if image fails to load
  • 🧩 Complex puzzle piece shape with shadows and outlines
  • 🚫 Auto-refresh after 3 failed attempts

⚙️ Options & Logic

Both the function API (sliderCaptcha) and the class API (new SliderCaptcha) accept an options object.

Validation & Image Source Logic

  • If request + onVerify are provided → the captcha will use server-side validation.
  • Otherwise → it falls back to client-side validation (local puzzle check).
  • If imageUrl is provided → it will use that as the custom image source.
  • Otherwise → it falls back to a default random image (picsum).

Available Options

| Option | Type | Default | Description | |---------------|-----------|-------------|-------------| | root | string / HTMLElement | null | Container element where captcha will render | | width | number / string | 320 | Width of the captcha | | height | number | 160 | Height of the captcha | | fit | "cover" / "contain" / "stretch" | "cover" | How the image should fit | | imageUrl | string / null | null | Custom image URL for the captcha background (overrides default/request) | | crossOrigin | string / null | null | Cross-origin setting for images | | theme | "light" / "dark" | "light" | Theme mode | | successText | string | "✅ Verified!" | Success message | | failText | string | "❌ Try again!" | Failure message | | onSuccess | function | () => {} | Callback when verification succeeds | | onFail | function | () => {} | Callback when verification fails | | onRefresh | function | () => {} | Callback when captcha is refreshed | | onVerify | function | null | Optional async callback for custom verification | | request | function | null | Optional async function returning { bgUrl, puzzleUrl } |


🚀 Installation

npm install slider-captcha-js

or via CDN:

<link rel="stylesheet" href="https://unpkg.com/slider-captcha-js/dist/slider-captcha.css" />
<script src="https://unpkg.com/slider-captcha-js/dist/slider-captcha.umd.js"></script>
<script>
  const captcha = new SliderCaptcha({
    root: "#stage",
    width: 320,
    height: 160,
    onSuccess: () => alert("Verified!"),
    onFail: () => alert("Try again"),
  });

  captcha.refresh();
</script>

💻 Usage

Vanilla JavaScript

<div id="stage"></div>
<script type="module">
  import { SliderCaptcha } from "slider-captcha-js";

  const captcha = new SliderCaptcha({
    root: "#stage",
    width: 320,
    height: 160,
    theme: "dark",
    onSuccess: () => alert("Verified!"),
    onFail: () => alert("Try again"),
  });

  captcha.refresh();
</script>

React (JavaScript)

import SliderCaptcha from "slider-captcha-js/react";
import { useRef } from "react";

function App() {
  const captchaRef = useRef(null);

  return (
    <div>
      <SliderCaptcha
        ref={captchaRef}
        width={320}
        height={160}
        theme="light"
        onSuccess={() => console.log("Verified!")}
        onFail={() => console.log("Try again")}
      />
      <button onClick={() => captchaRef.current?.refresh()}>Refresh</button>
    </div>
  );
}

React (TypeScript)

import SliderCaptcha, { type SliderCaptchaRef } from "slider-captcha-js/react";
import { useRef } from "react";

function App() {
  const captchaRef = useRef<SliderCaptchaRef>(null);

  return (
    <div>
      <SliderCaptcha
        ref={captchaRef}
        width={320}
        height={160}
        theme="light"
        onSuccess={() => console.log("Verified!")}
        onFail={() => console.log("Try again")}
      />
      <button onClick={() => captchaRef.current?.refresh()}>Refresh</button>
    </div>
  );
}

TypeScript

import { SliderCaptcha } from "slider-captcha-js";

const captcha = new SliderCaptcha({
  root: "#stage",
  width: 320,
  height: 160,
  onVerify: async ({ x, duration, trail }) => {
    // Custom async verification
    if (Math.abs(x - 100) < 6) return true;
    throw new Error("Verification failed");
  },
});

🔒 Backend Integration

sliderCaptcha({
  root: "#captcha",
  request: async () => {
    const res = await fetch("/api/captcha");
    return res.json(); // { bgUrl, puzzleUrl }
  },
  onVerify: async (data) => {
    const res = await fetch("/api/captcha/verify", {
      method: "POST",
      body: JSON.stringify(data),
    });
    const result = await res.json();
    if (!result.success) throw new Error("Invalid");
  },
});

🎨 Theming

.my-dark-theme .slider-captcha-bar {
  background: #222;
  color: #eee;
}
.my-dark-theme .slider-captcha-refresh {
  color: #0af;
}

🛠 Build

npm run build

Outputs:

  • dist/slider-captcha.umd.js
  • dist/slider-captcha.esm.js
  • dist/react-slider-captcha.js
  • dist/react-slider-captcha.esm.js

📜 License

MIT © 2025