Creates a new Captcha instance with specified canvas dimensions.
Initializes the underlying HTML5 canvas and 2D rendering context with optimized settings for CAPTCHA generation. The canvas is configured for high-quality text rendering and precise drawing operations.
Canvas width in pixels (default: 300)
Canvas height in pixels (default: 100)
Expected number of characters for coordinate calculation (default: 6)
Protected
_Protected
_Protected
_Protected
_Protected
_Protected
_Protected
_Protected
_Gets the current CAPTCHA text that has been drawn on the canvas.
Returns the text content that users need to enter to solve the CAPTCHA.
This value is set when drawCaptcha()
is called and represents the
solution to the generated CAPTCHA image.
The CAPTCHA text string, or empty string if no text has been set
Gets the generated CAPTCHA image as a PNG buffer.
Returns either a Promiseasync
property.
PNG image buffer (Promise in async mode, Buffer in sync mode)
Draws a background image on the CAPTCHA canvas.
The image is automatically scaled to fit the entire canvas dimensions, providing a background that increases visual complexity and security. This should typically be called before drawing text and other elements.
Pre-loaded image object (use resolveImage()
to load from file/URL)
The Captcha instance for method chaining
Adds decoy characters to the CAPTCHA for enhanced security against OCR.
Decoy characters are randomly positioned fake characters that confuse automated solving attempts while remaining distinguishable from real text by humans through opacity, size, or positioning differences.
Configuration for decoy character appearance
The Captcha instance for method chaining
const captcha = new Captcha(300, 100);
captcha
.addDecoy({ total: 25, opacity: 0.3 })
.drawCaptcha({ text: 'HELLO' });
Draws connecting trace lines between CAPTCHA characters for enhanced security.
Trace lines connect the character positions, making character segmentation
significantly more difficult for automated systems while maintaining human
readability. The lines follow the character coordinates established by drawCaptcha()
.
Configuration for trace line appearance
The Captcha instance for method chaining
const captcha = new Captcha(300, 100);
captcha
.drawCaptcha({ text: 'HELLO' })
.drawTrace({ color: '#95a5a6', size: 3 });
const captcha = new Captcha(350, 120);
captcha
.drawCaptcha({ text: 'VERIFY', size: 45 })
.drawTrace({
color: '#bdc3c7',
size: 2,
opacity: 0.6
});
const captcha = new Captcha(400, 150);
captcha
.addDecoy({ total: 20, opacity: 0.2 })
.drawCaptcha({ text: 'SECURE', size: 50 })
.drawTrace({
color: '#e74c3c',
size: 5,
opacity: 0.8
});
Note: Call drawCaptcha()
before drawTrace()
to ensure proper character coordinate calculation.
Draws the main CAPTCHA text on the canvas with specified styling options.
This is the core method for rendering the CAPTCHA text that users need to solve. Supports both single configuration objects and arrays for multi-styled text segments. Character positions are automatically calculated and stored for trace line generation.
Single text configuration or array of configurations for multi-styled segments
The Captcha instance for method chaining
const captcha = new Captcha(300, 100);
captcha.drawCaptcha({
text: 'HELLO',
size: 40,
color: '#2c3e50'
});
const captcha = new Captcha(350, 120);
captcha.drawCaptcha({
text: 'SECURE',
size: 50,
font: 'Arial',
rotate: 15, // Random rotation up to ±15 degrees per character
skew: true, // Apply random skewing transformation
opacity: 0.85
});
const captcha = new Captcha(400, 150);
captcha.drawCaptcha({
text: 'RAINBOW',
size: 45,
colors: ['#e74c3c', '#f39c12', '#f1c40f', '#27ae60', '#3498db', '#9b59b6']
});
const captcha = new Captcha(450, 150);
captcha.drawCaptcha([
{ text: 'SEC', size: 50, color: '#e74c3c', font: 'Arial', rotate: 10 },
{ text: 'URE', size: 45, color: '#27ae60', font: 'Times', skew: true }
]);
Core CAPTCHA class for low-level canvas operations and image generation.
This class provides direct access to the underlying canvas operations for creating CAPTCHA images. It offers more granular control compared to CaptchaGenerator but requires manual coordination of drawing operations. Use CaptchaGenerator for most use cases unless you need fine-grained control over the rendering process.
Example: Basic usage
Example: Advanced multi-step generation