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

index.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Default wave dimensions
  22. if (typeof theme.waveTop !== "number") theme.waveTop = 0;
  23. if (typeof theme.waveBottom !== "number") theme.waveBottom = theme.height;
  24. if (typeof theme.waveLeft !== "number") theme.waveLeft = 0;
  25. if (typeof theme.waveRight !== "number") theme.waveRight = theme.width;
  26. wrapText = textWrapper(theme);
  27. return this;
  28. };
  29. // Draw the frame
  30. renderer.drawFrame = function(context, options){
  31. context.patternQuality = "best";
  32. // Draw the background image and/or background color
  33. context.clearRect(0, 0, theme.width, theme.height);
  34. context.fillStyle = theme.backgroundColor;
  35. context.fillRect(0, 0, theme.width, theme.height);
  36. if (backgroundImage) {
  37. context.drawImage(backgroundImage, 0, 0, theme.width, theme.height);
  38. }
  39. if (!theme.noPattern) {
  40. patterns[theme.pattern || "wave"](context, options.waveform, theme);
  41. }
  42. // Write the caption
  43. if (options.caption) {
  44. wrapText(context, options.caption);
  45. }
  46. // Write subtitle
  47. if (options.subtitle) {
  48. wrapText(context, null, options.subtitle);
  49. }
  50. return this;
  51. };
  52. if (t) {
  53. renderer.theme(t);
  54. }
  55. return renderer;
  56. }