captcha-canvas API Documentation - v3.3.4
    Preparing search index...

    Function createCaptcha

    • Creates a CAPTCHA image asynchronously with automatic security features.

      This is the simplest way to generate a CAPTCHA with sensible defaults and automatic security features including decoy characters, trace lines, and random text generation. The function handles the complete generation process and returns both the image and solution.

      Parameters

      • width: number

        Width of the CAPTCHA image in pixels

      • height: number

        Height of the CAPTCHA image in pixels

      • option: CreateCaptchaOptions = {}

        Optional configuration for customizing CAPTCHA appearance and security

      Returns CaptchaValue

      Object containing the image promise and solution text

      import { createCaptcha } from 'captcha-canvas';
      import fs from 'fs';

      const { image, text } = createCaptcha(300, 100);
      const buffer = await image;

      fs.writeFileSync('captcha.png', buffer);
      console.log('Solution:', text);
      const { image, text } = createCaptcha(400, 150, {
      captcha: {
      text: 'HELLO',
      size: 60,
      colors: ['#e74c3c', '#3498db', '#27ae60']
      }
      });
      const { image, text } = createCaptcha(350, 120, {
      captcha: { characters: 8, size: 45 },
      trace: { color: '#95a5a6', size: 4, opacity: 0.8 },
      decoy: { total: 50, opacity: 0.3, size: 18 }
      });
      import { resolveImage } from 'captcha-canvas';

      const background = await resolveImage('./noise-pattern.jpg');
      const { image, text } = createCaptcha(300, 100, {
      background,
      captcha: { opacity: 0.9 } // Make text more visible over background
      });
      app.get('/captcha', async (req, res) => {
      const { image, text } = createCaptcha(300, 100);

      // Store solution in session for verification
      req.session.captchaSolution = text;

      const buffer = await image;
      res.type('png').send(buffer);
      });