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

index.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, 'caption');
  43. }
  44. // Write the citation
  45. if (options.citation) {
  46. wrapText(context, options.citation, 'citation');
  47. }
  48. // Write the label
  49. if (options.label) {
  50. wrapText(context, options.label, 'label');
  51. }
  52. return this;
  53. };
  54. if (t) {
  55. renderer.theme(t);
  56. }
  57. return renderer;
  58. }