Turn audio into a shareable video. forked from nypublicradio/audiogram

index.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // var d3 = require("d3"),
  2. var patterns = require("./patterns.js"),
  3. textWrapper = require("./text-wrapper.js");
  4. module.exports = function(t) {
  5. var renderer = {},
  6. backgroundImage,
  7. wrapText,
  8. theme;
  9. renderer.backgroundImage = function(_) {
  10. if (!arguments.length) return backgroundImage;
  11. backgroundImage = _;
  12. return this;
  13. };
  14. renderer.theme = function(_) {
  15. if (!arguments.length) return theme;
  16. theme = _;
  17. // Default colors
  18. theme.backgroundColor = theme.backgroundColor || "#fff";
  19. theme.waveColor = theme.waveColor || theme.foregroundColor || "#000";
  20. theme.captionColor = theme.captionColor || theme.foregroundColor || "#000";
  21. theme.subtitleColor = theme.subtitleColor || theme.foregroundColor || "#000";
  22. // Default wave dimensions
  23. if (typeof theme.waveTop !== "number") theme.waveTop = 0;
  24. if (typeof theme.waveBottom !== "number") theme.waveBottom = theme.height;
  25. if (typeof theme.waveLeft !== "number") theme.waveLeft = 0;
  26. if (typeof theme.waveRight !== "number") theme.waveRight = theme.width;
  27. wrapText = textWrapper(theme);
  28. return this;
  29. };
  30. // Draw the frame
  31. renderer.drawFrame = function(context, options){
  32. context.patternQuality = "best";
  33. // Draw the background image and/or background color
  34. context.clearRect(0, 0, theme.width, theme.height);
  35. context.fillStyle = theme.backgroundColor;
  36. context.fillRect(0, 0, theme.width, theme.height);
  37. if (backgroundImage) {
  38. context.drawImage(backgroundImage, 0, 0, theme.width, theme.height);
  39. }
  40. if (!theme.noPattern) {
  41. patterns[theme.pattern || "wave"](context, options.waveform, theme);
  42. }
  43. // Write the caption
  44. if (options.caption) {
  45. wrapText(context, options.caption);
  46. }
  47. // Write subtitle
  48. if (options.subtitle) {
  49. wrapText(context, null, options.subtitle);
  50. }
  51. return this;
  52. };
  53. if (t) {
  54. renderer.theme(t);
  55. }
  56. return renderer;
  57. }