Skip to content

Commit

Permalink
0.1: format codes
Browse files Browse the repository at this point in the history
ready for shipping
  • Loading branch information
guo-yu committed Sep 6, 2014
1 parent af28c7e commit f8a97ba
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 118 deletions.
21 changes: 9 additions & 12 deletions README.md
@@ -1,33 +1,30 @@
## ascii ![npm](https://badge.fury.io/js/ascii.png)
## Ascii ![npm](https://badge.fury.io/js/ascii.png)

convert jpg/png/gif to ascii arts based on node-canvas
![screenshot](http://ww3.sinaimg.cn/mw1024/61ff0de3gw1e903qhbyt8j20kl0f5768.jpg)

### Installation
````
```bash
$ npm install ascii
````
```

### Example
````javascript
```javascript
var Ascii = require('ascii');
var pic = new Ascii('demo.png');

// output in terminal (terminal mode)
pic.convert(function(err, result) {
console.log(result);
console.log(result);
});

// output as html strings with css style (html mode)
pic.convert('html', function(err, html){
console.log(html);
// then save it
require('fs').writeFileSync('demo.html',html);
console.log(html);
// then save it
require('fs').writeFileSync('demo.html',html);
});
````

### API
check this file: `index.js`
```

### Contributing
- Fork this repo
Expand Down
63 changes: 0 additions & 63 deletions ascii.js

This file was deleted.

File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
69 changes: 31 additions & 38 deletions index.js
Expand Up @@ -6,48 +6,41 @@
//
// @author: [turing](http://guoyu.me);
// @desc: convert pictures to ascii arts based on node-canvas

var fs = require('fs'),
Canvas = require('canvas'),
ascii = require('./ascii');

var Ascii = function(src, params) {
this.src = src;
var fs = require('fs');
var Canvas = require('canvas');
var ascii = require('./lib/ascii');

module.exports = Ascii;

function Ascii(src, params) {
this.src = src;
}

Ascii.prototype.load = function(callback) {
if (this.src) {
fs.readFile(this.src, callback);
} else {
callback(new Error('src picture required.'))
}
}
if (this.src)
return fs.readFile(this.src, callback);
return callback(new Error('src picture required.'))
};

Ascii.prototype.convert = function(type, callback) {
var t = (type && typeof(type) === 'string') ? type : 'cli',
cb = (typeof(type) === 'function' && !callback) ? type : callback;
this.load(function(err, img){
if (!err) {
var pic = new Canvas.Image;
pic.src = img;
var cv = new Canvas(pic.width, pic.height),
ctx = cv.getContext('2d');
ctx.drawImage(pic, 0, 0, pic.width, pic.height);
cb(null, ascii.init(t, ctx, pic));
} else {
cb(err);
}
});
}

Ascii.fromBuffer = function (buffer) {
var instance = new Ascii();

instance.load = function (callback) {
callback(null, buffer);
};

return instance;
}
var t = (type && typeof(type) === 'string') ? type : 'cli';
var cb = (typeof(type) === 'function' && !callback) ? type : callback;
this.load(function(err, img) {
if (err) return cb(err);
var pic = new Canvas.Image;
pic.src = img;
var cv = new Canvas(pic.width, pic.height);
var ctx = cv.getContext('2d');
ctx.drawImage(pic, 0, 0, pic.width, pic.height);
cb(null, ascii.init(t, ctx, pic));
});
};

module.exports = Ascii;
Ascii.fromBuffer = function(buffer) {
var instance = new Ascii();
instance.load = function(callback) {
callback(null, buffer);
};
return instance;
};
63 changes: 63 additions & 0 deletions lib/ascii.js
@@ -0,0 +1,63 @@
// 感谢这篇文章的启发 [基于canvas将图片转化成字符画](http://www.cssha.com/img2txt-canvas)

var Ascii = {
style: "<style type='text/css'>* {margin: 0;padding: 0;} .ascii {font-size: 12px;font-family: simsun;}</style>",
// 按照不同的终端输出
types: {
cli: {
br: '\n',
blank: ' '
},
html: {
br: '</br>',
blank: '&nbsp;'
}
},
// 根据灰度生成相应字符
toText: function(type, g) {
var self = this;
if (g <= 30) {
return '#';
} else if (g > 30 && g <= 60) {
return '&';
} else if (g > 60 && g <= 120) {
return '$';
} else if (g > 120 && g <= 150) {
return '*';
} else if (g > 150 && g <= 180) {
return 'o';
} else if (g > 180 && g <= 210) {
return '!';
} else if (g > 210 && g <= 240) {
return ';';
} else {
return self.types[type].blank;
}
},
// 根据rgb值计算灰度
getGray: function(r, g, b) {
return 0.299 * r + 0.578 * g + 0.114 * b;
},
// 初始化
init: function(type, ctx, pic) {
var self = this,
data = ctx.getImageData(0, 0, pic.width, pic.height),
text = '';
for (h = 0; h < data.height; h += 12) {
var p = '';
for (w = 0; w < data.width; w += 6) {
var index = (w + data.width * h) * 4;
var r = data.data[index + 0];
var g = data.data[index + 1];
var b = data.data[index + 2];
var gray = self.getGray(r, g, b);
p += self.toText(type, gray);
}
p += self.types[type].br;
text += p;
}
return (type === 'html') ? self.style + "<div class='ascii'>" + text + '</div>' : text;
}
}

module.exports = Ascii;
7 changes: 2 additions & 5 deletions package.json
@@ -1,11 +1,8 @@
{
"name": "ascii",
"version": "0.0.1",
"version": "0.1.0",
"description": "convert pictures to ascii arts based on node-canvas",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/turingou/ascii.git"
Expand All @@ -15,7 +12,7 @@
"ascii-arts",
"canvas"
],
"author": "turing",
"author": "turing <o.u.turing@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/turingou/ascii/issues"
Expand Down

0 comments on commit f8a97ba

Please sign in to comment.