Skip to content

A lightweight, customizable slider captcha library for web applications. Provides a simple drag-to-verify mechanism to prevent bots, with support for vanilla JS, React, and TypeScript integrations. Includes examples, tests, and flexible styling options.

License

Notifications You must be signed in to change notification settings

AmazingDevTeam/slider-captcha-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 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

About

A lightweight, customizable slider captcha library for web applications. Provides a simple drag-to-verify mechanism to prevent bots, with support for vanilla JS, React, and TypeScript integrations. Includes examples, tests, and flexible styling options.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published