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

index.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var d3 = require("d3"),
  2. 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. patterns[theme.pattern || "wave"](context, options.waveform, theme);
  40. // Write the caption
  41. if (options.caption) {
  42. wrapText(context, options.caption);
  43. }
  44. return this;
  45. };
  46. if (t) {
  47. renderer.theme(t);
  48. }
  49. return renderer;
  50. }