From c2b89d2753ec984aaec83bd19dfce33fe5f3f135 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 10 Mar 2014 17:30:37 +0100 Subject: [PATCH] refactored the captcha module --- lib/captcha.js | 52 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/lib/captcha.js b/lib/captcha.js index 5c2b7c9..4b33747 100644 --- a/lib/captcha.js +++ b/lib/captcha.js @@ -6,32 +6,54 @@ var canvas = require('canvas') , characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; /** - * Expose the constructor. + * Captcha constructor. */ -module.exports = function() { - var code = '' - , _canvas = new canvas(64, 26) - , ctx = _canvas.getContext('2d'); +function Captcha() { + this.code = ''; + this.canvas = new canvas(64, 26); + this.initialize(); +} - while (code.length < 4) { - code += characters[Math.floor(Math.random() * characters.length)]; +/** + * Generate the captcha. + */ + +Captcha.prototype.initialize = function() { + while (this.code.length < 4) { + this.code += characters[Math.floor(Math.random() * characters.length)]; } + + var ctx = this.canvas.getContext('2d'); ctx.fillStyle = '#DDDDDD'; ctx.fillRect(0, 0, 64, 26); ctx.font = 'bold 20px Helvetica'; ctx.lineWidth = 1; - ctx.textAlign = "center"; + ctx.textAlign = 'center'; ctx.strokeStyle = '#080'; - ctx.strokeText(code, 31, 20); + ctx.strokeText(this.code, 31, 20); ctx.save(); +}; - this.getCode = function() { - return code; - }; +/** + * Return the captcha code. + */ - this.toDataURL = function() { - return _canvas.toDataURL(); - }; +Captcha.prototype.getCode = function() { + return this.code; }; + +/** + * Return the captcha image. + */ + +Captcha.prototype.toDataURL = function() { + return this.canvas.toDataURL(); +}; + +/** + * Expose the constructor. + */ + +module.exports = Captcha; -- 2.54.0