badge.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const pug = require('pug');
  2. const title = 'slack';
  3. function width(str) {
  4. return 6 * str.length;
  5. }
  6. const svgTmpl =
  7. `svg(xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width=wd height="20")\n` +
  8. ` linearGradient(x2="0" y2="100%")#b\n` +
  9. ` stop(offset="0" stop-color="#bbb" stop-opacity=".1")\n` +
  10. ` stop(offset="1" stop-opacity=".1")\n` +
  11. ` clipPath#a\n` +
  12. ` rect(width=wd height="20" rx="3" fill="#fff")\n` +
  13. ` g(clip-path="url(#a)")\n` +
  14. ` path(fill='#' + colorA d='M0 0h' + left + 'v20H0z')\n` +
  15. ` path(fill='#' + colorB d='M'+left + ' 0h' + (wd-left) + 'v20H' + left +'z')\n` +
  16. ` path(fill="url(#b)" d='M0 0h' + wd + 'v20H0z')\n` +
  17. ` g(fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110")\n` +
  18. ` text(x="195" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="${title.length*54}") ${title}\n` +
  19. ` text(x="195" y="140" transform="scale(.1)" textLength="${title.length*54}") ${title}\n` +
  20. ` text(x=(wd * 5 + 195) y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength=(value.length*60)) #{value}\n` +
  21. ` text(x=(wd * 5 + 195) y="140" transform="scale(.1)" textLength=(value.length*60)) #{value}\n`;
  22. module.exports = {
  23. badge: function(presence, total, customColorA, customColorB) {
  24. const fn = pug.compile(svgTmpl);
  25. const value = `${presence}/${total}`;
  26. const left = width(title) + 8;
  27. const wd = left + width(value) + 22;
  28. const colorA = customColorA || '555';
  29. const colorB = customColorB || '39AE85';
  30. return fn({ value, wd, left, colorA, colorB });
  31. },
  32. };