http_spin.js 986 B

123456789101112131415161718192021222324252627282930313233343536
  1. var http = require('http');
  2. var charmer = require('../');
  3. http.createServer(function (req, res) {
  4. res.setHeader('content-type', 'text/ansi');
  5. var charm = charmer();
  6. charm.pipe(res);
  7. charm.reset();
  8. var radius = 10;
  9. var theta = 0;
  10. var points = [];
  11. var iv = setInterval(function () {
  12. var x = 2 + (radius + Math.cos(theta) * radius) * 2;
  13. var y = 2 + radius + Math.sin(theta) * radius;
  14. points.unshift([ x, y ]);
  15. var colors = [ 'red', 'yellow', 'green', 'cyan', 'blue', 'magenta' ];
  16. points.forEach(function (p, i) {
  17. charm.position(p[0], p[1]);
  18. var c = colors[Math.floor(i / 12)];
  19. charm.background(c).write(' ')
  20. });
  21. points = points.slice(0, 12 * colors.length - 1);
  22. theta += Math.PI / 40;
  23. }, 50);
  24. req.connection.on('end', function () {
  25. clearInterval(iv);
  26. charm.end();
  27. });
  28. }).listen(8081);