, 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;