From 030bfa94d6949fb9c78319a11489a51359fcecc3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 4 Oct 2024 14:21:54 -0500 Subject: [PATCH] captcha: rename puzzle to bg, and piece to puzzle --- src/controllers/api/captcha.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/controllers/api/captcha.ts b/src/controllers/api/captcha.ts index cf68d673..9fc34de8 100644 --- a/src/controllers/api/captcha.ts +++ b/src/controllers/api/captcha.ts @@ -20,7 +20,7 @@ const captchas = new TTLCache(); /** Puzzle captcha controller. */ export const captchaController: AppController = async (c) => { - const { puzzle, piece, solution } = await generateCaptcha( + const { bg, puzzle, solution } = await generateCaptcha( await Deno.readFile(new URL('../../../captcha/tj-holowaychuk.jpg', import.meta.url)), await Deno.readFile(new URL('../../../captcha/puzzle.png', import.meta.url)), { @@ -39,10 +39,10 @@ export const captchaController: AppController = async (c) => { captchas.set(id, solution, { ttl }); return c.json({ - type: 'puzzle', id, + type: 'puzzle', + bg: bg.toDataURL(), puzzle: puzzle.toDataURL(), - piece: piece.toDataURL(), created_at: now.toISOString(), expires_at: new Date(now.getTime() + ttl).toISOString(), }); @@ -61,22 +61,22 @@ async function generateCaptcha( }, ) { const { pw, ph, cw, ch, alpha } = opts; - const puzzle = createCanvas(cw, ch); - const ctx = puzzle.getContext('2d'); + const bg = createCanvas(cw, ch); + const ctx = bg.getContext('2d'); const image = await loadImage(from); ctx.drawImage(image, 0, 0, image.width(), image.height(), 0, 0, cw, ch); - const piece = createCanvas(pw, ph); - const pctx = piece.getContext('2d'); + const puzzle = createCanvas(pw, ph); + const pctx = puzzle.getContext('2d'); - const solution = getPieceCoords(puzzle.width, puzzle.height, pw, ph); + const solution = getPieceCoords(bg.width, bg.height, pw, ph); // Draw the piece onto the puzzle piece canvas but only where the mask allows const maskImage = await loadImage(mask); pctx.globalCompositeOperation = 'source-over'; pctx.drawImage(maskImage, 0, 0, pw, ph); pctx.globalCompositeOperation = 'source-in'; - pctx.drawImage(puzzle, solution.x, solution.y, pw, ph, 0, 0, pw, ph); + pctx.drawImage(bg, solution.x, solution.y, pw, ph, 0, 0, pw, ph); // Reset composite operation pctx.globalCompositeOperation = 'source-over'; @@ -95,8 +95,8 @@ async function generateCaptcha( ctx.drawImage(tempCanvas, solution.x, solution.y, pw, ph); return { + bg, puzzle, - piece, solution, }; }