Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

multi output works

  • Loading branch information...
commit 8889bb954c7ab1f742420b35a3f677266986d21a 1 parent e9d56f2
@substack authored
View
2  example/multi/a.js
@@ -1,5 +1,5 @@
var n = 0;
-return function (t, i) {
+module.exports = function (t, i) {
if (i % 10 === 0) n = Math.random();
return t * 2 % 1/16 < 1/256
View
2  example/multi/b.js
@@ -1,4 +1,4 @@
-return function (t, i) {
+module.exports = function (t, i) {
// baseline
var f = 800 * Math.pow(2, Math.floor(t * 4 % 4) / 6);
return Math.sin(t * f * Math.PI)
View
2  example/multi/c.js
@@ -5,7 +5,7 @@ var freqs = [
0, 1600, 0, 1800
];
-return function (t, i) {
+module.exports = function (t, i) {
var f = freqs[Math.floor((t * 4) % freqs.length)];
return Math.sin(t * Math.PI * f);
};
View
54 index.js
@@ -1,21 +1,57 @@
var Canvas = require('canvas');
var falafel = require('falafel');
-module.exports = function (src, opts) {
+module.exports = function art (src, opts) {
if (!opts) opts = {};
if (!opts.height) opts.height = 12;
if (!opts.width) opts.width = 6;
if (!opts.colors) opts.colors = {};
+ if (!opts.left) opts.left = 0;
+ if (Array.isArray(src)) {
+ var size = src.slice(1).reduce(function (acc, s) {
+ var size = imageSize(s, opts);
+ return {
+ width : acc.width + size.width + 50,
+ height : Math.max(acc.height, size.height),
+ };
+ }, imageSize(src[0], opts));
+
+ var canvas = new Canvas(size.width, size.height);
+
+ var left = 0;
+ src.forEach(function (s) {
+ opts.left = left;
+ draw(canvas, s, opts);
+
+ var size = imageSize(s, opts);
+ left += size.width + 50;
+ });
+
+ return canvas.createPNGStream();
+ }
+ else {
+ var size = imageSize(src, opts);
+ var canvas = new Canvas(size.width, size.height);
+ draw(canvas, src, opts)
+ return canvas.createPNGStream();
+ }
+};
+
+function imageSize (src, opts) {
var lines = src.split('\n');
- var height = lines.length * opts.height;
- var width = Math.max.apply(null, lines.map(function (line) {
- return line.length;
- })) * opts.width;
- var left = opts.left || 0;
- var canvas = new Canvas(left + width, height);
+ return {
+ height : lines.length * opts.height,
+ width : Math.max.apply(null, lines.map(function (line) {
+ return line.length;
+ })) * opts.width,
+ };
+}
+
+function draw (canvas, src, opts) {
var ctx = canvas.getContext('2d');
+ var lines = src.split('\n');
var rects = [];
@@ -61,13 +97,11 @@ module.exports = function (src, opts) {
if (r.end !== undefined && col + c.length > r.end) return;
if (!/\s{2,}/.test(c)) {
- var x = left + col * opts.width;
+ var x = opts.left + col * opts.width;
var w = c.length * opts.width;
ctx.fillRect(x, y, w, opts.height);
}
col += c.length;
});
});
-
- return canvas.createPNGStream();
};
Please sign in to comment.
Something went wrong with that request. Please try again.