make letters distorted and shrink and stretch them, change their angles, make the blur 1px
- User
make letters distorted and shrink and stretch them, change their angles, make the blur 1px
Assistant
Here's the updated HTML code that generates a CAPTCHA image with distorted, scaled, and rotated letters, each blurred by 1px and colored in random shades of gray to black.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Distorted CAPTCHA Generator</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; text-align: center; margin-top: 50px; background-color: #f0f2f5; /* Light background */ color: #333; line-height: 1.6; } h1 { color: #2c3e50; /* Darker heading */ margin-bottom: 15px; } p { font-size: 1.1em; color: #555; margin-bottom: 30px; } button { padding: 12px 25px; font-size: 1.1em; cursor: pointer; background-color: #3498db; /* Blue button */ color: white; border: none; border-radius: 6px; transition: background-color 0.3s ease, transform 0.2s ease; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } button:hover { background-color: #2980b9; /* Darker blue on hover */ transform: translateY(-2px); /* Slight lift effect */ } button:active { transform: translateY(0); /* Press effect */ } canvas { display: none; /* Hidden, used only for generation */ } </style> </head> <body> <h1>Distorted CAPTCHA Image Generator</h1> <p>Click the button below to generate and download a CAPTCHA image. The letters are distorted, randomly scaled, rotated, and have a 1px blur, with colors ranging from gray to black.</p> <button id="generateBtn">Generate and Download CAPTCHA</button> <canvas id="captchaCanvas"></canvas> <script> document.getElementById('generateBtn').addEventListener('click', function() { const canvas = document.getElementById('captchaCanvas'); const ctx = canvas.getContext('2d'); // --- Configuration --- const imageWidth = 200; const imageHeight = 50; const captchaLength = 6; const fontSize = 30; // Affects text size and vertical centering estimation const blurAmount = 1; // Blur in pixels const rotationRangeDegrees = 20; // Max rotation angle in degrees (+/-) const scaleRangeFactor = 0.3; // Max scaling factor (e.g., 0.3 means scale from 0.7 to 1.3) // Set canvas dimensions canvas.width = imageWidth; canvas.height = imageHeight; // Generate a random string of alphanumeric characters const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let captchaText = ''; for(let i = 0; i < captchaLength; i++) { captchaText += characters.charAt(Math.floor(Math.random() * characters.length)); } // Clear canvas and set a white background (for the "black and white" requirement) ctx.fillStyle = 'white'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Set the font style for the text ctx.font = `${fontSize}px Arial, sans-serif`; // Added sans-serif fallback // Calculate spacing and positions for each character to distribute them evenly const numChars = captchaText.length; const horizontalPadding = 10; // Padding on the left and right sides const totalCharDisplayWidth = canvas.width - 2 * horizontalPadding; // Total width available for characters const charSlotWidth = totalCharDisplayWidth / numChars; // Width allocated for each character slot // Convert rotation range from degrees to radians for canvas API const rotationRangeRadians = rotationRangeDegrees * (Math.PI / 180); // Draw each letter with random transformations const letters = captchaText.split(''); letters.forEach((letter, index) => { ctx.save(); // Save the current canvas state before applying transformations for this character // Calculate the center of the slot allocated for this character // This is where we'll translate the origin to perform transformations let slotCenterX = horizontalPadding + charSlotWidth * index + charSlotWidth / 2; let slotCenterY = canvas.height / 2; // Aim to center characters vertically within the canvas // --- Generate Random Transformations --- // Rotation: random angle between -rotationRangeRadians and +rotationRangeRadians const randomAngle = (Math.random() - 0.5) * 2 * rotationRangeRadians; // Scaling: random scale factor between (1 - scaleRangeFactor) and (1 + scaleRangeFactor) // This creates the 'shrink and stretch' effect. const randomScaleX = 1 - scaleRangeFactor + Math.random() * (2 * scaleRangeFactor); const randomScaleY = 1 - scaleRangeFactor + Math.random() * (2 * scaleRangeFactor); // Jitter: small random offsets to position the character slightly within its slot const randomOffsetX = (Math.random() - 0.5) * (charSlotWidth * 0.15); // Nudge horizontally const randomOffsetY = (Math.random() - 0.5) * (canvas.height * 0.1); // Nudge vertically // --- Apply Transformations --- ctx.translate(slotCenterX, slotCenterY); // Move the canvas origin to the character's slot center ctx.rotate(randomAngle); // Rotate around this new origin ctx.scale(randomScaleX, randomScaleY); // Scale around this new origin ctx.filter = `blur(${blurAmount}px)`; // Apply the blur filter // Generate random gray to black color // Values from 0 (black) to 128 (mid-gray) for text const grayValue = Math.floor(Math.random() * 129); ctx.fillStyle = `rgb(${grayValue}, ${grayValue}, ${grayValue})`; // --- Draw the Letter --- // To draw the letter centered around the transformed origin (slotCenterX, slotCenterY): // 1. Get the text's bounding width. This width might be affected by scaling. // 2. Draw the text with its baseline shifted relative to the origin. // For font size 30px, text baseline is typically around 2/3 down the font height. // To center vertically, we need to shift the baseline UP by roughly half the font height. // An empirical value for 30px font size might be 10-12px. We use 10px here for the baseline offset. const textWidth = ctx.measureText(letter).width; // Measure text width AFTER scaling has been applied to context ctx.fillText(letter, -textWidth / 2 + randomOffsetX, 10 + randomOffsetY); // Draw text, centered horizontally relative to origin, with a vertical baseline offset and jitter ctx.restore(); // Restore the canvas state to how it was before this character's transformations }); // --- Download the Image --- // Convert the canvas content to a data URL (PNG format) const dataURL = canvas.toDataURL('image/png'); // Create a temporary link element to trigger the download const link = document.createElement('a'); link.href = dataURL; link.download = `captcha_${captchaText}.png`; // Tag the filename with the generated characters // Append the link to the body for broader browser compatibility (especially Firefox) document.body.appendChild(link); link.click(); // Programmatically click the link to start the download // Clean up the temporary link element document.body.removeChild(link); }); </script> </body> </html>Key Changes and How They Work:
-
Distortion, Scaling, and Rotation:
- For each character, we calculate its intended
slotCenterXandslotCenterY. - We then use
ctx.save()andctx.restore()to isolate transformations for each character. ctx.translate(slotCenterX, slotCenterY)moves the canvas's origin point to the center of the character's allocated space.ctx.rotate(randomAngle)rotates the drawing context around this new origin.ctx.scale(randomScaleX, randomScaleY)stretches or shrinks the context horizontally and vertically around the origin.ctx.filter = \blur(${blurAmount}px)`applies the specified blur. I've changed this to1px`.ctx.fillText()draws the character. To center it within the transformed origin, we calculatetextWidthand draw at-textWidth / 2for horizontal centering, and use a10pxpositive offset for the baseline to achieve vertical centering (this is an empirical value for30pxArial).randomOffsetXandrandomOffsetYadd a final small positional jitter.
- For each character, we calculate its intended
-
Color:
- The color generation
const grayValue = Math.floor(Math.random() * 129);remains the same, producing shades from black (0) to a medium gray (128).
- The color generation
-
Blur:
- The
ctx.filterproperty is now set toblur(1px).
- The
-
Filename Tagging:
- The downloaded file is still named
captcha_YOURCHARACTERS.png.
- The downloaded file is still named
-
Styling:
- Added some basic CSS to make the page look a bit more modern and user-friendly.
How to Use:
- Save this code as an HTML file (e.g.,
distorted_captcha.html). - Open the file in your web browser.
- Click the "Generate and Download CAPTCHA" button.
- A PNG image file will be downloaded, featuring a CAPTCHA string with the described visual effects.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.