]> git.example.dev Git - binbsis50.git/commitdiff
refactored the captcha module
authorLuigi Pinca <luigipinca@gmail.com>
Mon, 10 Mar 2014 16:30:37 +0000 (17:30 +0100)
committerLuigi Pinca <luigipinca@gmail.com>
Mon, 10 Mar 2014 16:39:38 +0000 (17:39 +0100)
lib/captcha.js

index 5c2b7c92b55616dd83753a7fcf59ea54a7b6b8ef..4b33747ba4de4bc6cfd4dfbae18e43b2905d3f00 100644 (file)
@@ -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;